diff options
Diffstat (limited to 'jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils')
3 files changed, 147 insertions, 0 deletions
diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java new file mode 100644 index 0000000..44feaa3 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/JarExtractor.java @@ -0,0 +1,87 @@ +package org.openecomp.sdc.toscaparser.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.io.MoreFiles; +import com.google.common.io.RecursiveDeleteOption; + +public class JarExtractor { + + private static final Logger LOGGER = LoggerFactory.getLogger(JarExtractor.class); + private static final String PYTHON_DEPENDENCIES_PATH = "Lib/site-packages"; + private static final String TEMP_DIR_PREFIX = "__tosca__"; + + public Path extractPyhtonPackages() throws IOException { + String codePath = getCodePath(); + if (!isRunningInJar(codePath)) { + LOGGER.info("#extractPyhtonPackages - Nothing to extract, we're not running in a jar file."); + return null; + } + + Path tempDirPath = createTempDirectory(); + String tempDirName = tempDirPath.toString(); + extractJarDirectory(codePath, tempDirName); + LOGGER.info("#extractPyhtonPackages - End. Extracted python dependencies to {}", tempDirName); + return tempDirPath; + } + + private Path createTempDirectory() throws IOException { + Path tempDir = Files.createTempDirectory(TEMP_DIR_PREFIX); + LOGGER.debug("#createTempDirectory - tempDir created: {}", tempDir); + return tempDir; + } + + private void extractJarDirectory(String jarDir, String tempDir) throws IOException { + try (JarFile jarFile = new JarFile(jarDir)) { + Enumeration<JarEntry> entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + if (shouldExtract(entry)) { + extract(jarFile, entry, tempDir); + } + } + } + } + + private void extract(JarFile jarFile, JarEntry entry, String tempDirName) throws IOException { + try (InputStream source = jarFile.getInputStream(entry)) { + Path targetPath = Paths.get(tempDirName, entry.getName()); + Files.createDirectories(targetPath.getParent()); + Files.copy(source, targetPath); + } + } + + private boolean shouldExtract(JarEntry entry) { + return !entry.isDirectory() && entry.getName() + .startsWith(PYTHON_DEPENDENCIES_PATH); + } + + private String getCodePath() { + String codePath = this.getClass() + .getProtectionDomain() + .getCodeSource() + .getLocation() + .getFile(); + LOGGER.debug("#getCodePath - codePath: {}", codePath); + return codePath; + } + + private boolean isRunningInJar(String path) { + return path.endsWith(".jar"); + } + + public void deleteDirectory(Path path) throws IOException { + MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE); + LOGGER.info("#deleteDirectory - deleted temp directory: {}", path); + } +} diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonType.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonType.java new file mode 100644 index 0000000..f4d6939 --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonType.java @@ -0,0 +1,47 @@ +package org.openecomp.sdc.toscaparser.utils; + +import static java.util.stream.Collectors.toMap; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +public enum PythonType { + + INT("int", Integer.class), + FLOAT("float", Double.class), + BOOL("bool", Boolean.class), + STR("str", String.class), + LIST("list", List.class), + DICT("dict", Map.class), + UNKNOWN("", Object.class); + + private static final Map<String, PythonType> NAME_TO_TYPE; + private final String typeName; + private final Class<?> javaClass; + + static { + NAME_TO_TYPE = Arrays.stream(values()) + .filter(type -> type != UNKNOWN) + .collect(toMap(PythonType::getTypeName, Function.identity())); + } + + private PythonType(String typeName, Class<?> javaClass) { + this.typeName = typeName; + this.javaClass = javaClass; + } + + public static PythonType fromName(String name) { + PythonType pythonType = NAME_TO_TYPE.get(name); + return pythonType != null ? pythonType : UNKNOWN; + } + + public String getTypeName() { + return typeName; + } + + public Class<?> getJavaClass() { + return javaClass; + } +}
\ No newline at end of file diff --git a/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonUtils.java b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonUtils.java new file mode 100644 index 0000000..f73c92b --- /dev/null +++ b/jython-tosca-parser/src/main/java/org/openecomp/sdc/toscaparser/utils/PythonUtils.java @@ -0,0 +1,13 @@ +package org.openecomp.sdc.toscaparser.utils; + +public final class PythonUtils { + + private PythonUtils() { + // No instances allowed + } + + public static Object cast(Object object, String pythonTypeName) { + PythonType pythonType = PythonType.fromName(pythonTypeName); + return pythonType.getJavaClass().cast(object); + } +} |