19 package org.sleuthkit.autopsy.python;
22 import java.io.FileNotFoundException;
23 import java.io.FilenameFilter;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Scanner;
30 import java.util.logging.Level;
31 import java.util.regex.Matcher;
32 import org.openide.DialogDisplayer;
33 import org.openide.NotifyDescriptor;
34 import org.openide.modules.InstalledFileLocator;
35 import org.openide.util.NbBundle;
36 import org.python.util.PythonInterpreter;
71 List<T> objects =
new ArrayList<>();
72 Set<File> pythonModuleDirs =
new HashSet<>();
73 PythonInterpreter interpreter =
new PythonInterpreter();
77 for (File f : InstalledFileLocator.getDefault().locateAll(
"InternalPythonModules",
JythonModuleLoader.class.getPackage().getName(),
false)) {
78 Collections.addAll(pythonModuleDirs, f.listFiles());
83 for (File file : pythonModuleDirs) {
84 if (file.isDirectory()) {
86 for (File script : pythonScripts) {
88 Scanner fileScanner =
new Scanner(script);
89 while (fileScanner.hasNextLine()) {
90 String line = fileScanner.nextLine();
91 if (line.startsWith(
"class ") && filter.
accept(line)) {
92 String className = line.substring(6, line.indexOf(
"("));
95 }
catch (Exception ex) {
96 logger.log(Level.SEVERE, String.format(
"Failed to load %s from %s", className, script.getAbsolutePath()), ex);
98 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
99 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
100 NotifyDescriptor.ERROR_MESSAGE));
104 }
catch (FileNotFoundException ex) {
105 logger.log(Level.SEVERE, String.format(
"Failed to open %s", script.getAbsolutePath()), ex);
106 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
107 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
108 NotifyDescriptor.ERROR_MESSAGE));
116 private static <T> T
createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
120 interpreter.exec(
"import sys");
121 String path = Matcher.quoteReplacement(script.getParent());
122 interpreter.exec(
"sys.path.append('" + path +
"')");
123 String moduleName = script.getName().replaceAll(
"\\.py$",
"");
126 interpreter.exec(
"import " + moduleName);
127 interpreter.exec(
"reload(" + moduleName +
")");
130 interpreter.exec(
"from " + moduleName +
" import " + className);
131 interpreter.exec(
"obj = " + className +
"()");
133 T obj = interpreter.get(
"obj", interfaceClass);
137 interpreter.exec(
"sys.path.remove('" + path +
"')");
145 public boolean accept(File dir, String name) {
146 return name.endsWith(
".py");
152 boolean accept(String line);
159 return (line.contains(
"IngestModuleFactoryAdapter") || line.contains(
"IngestModuleFactory"));
167 return (line.contains(
"GeneralReportModuleAdapter") || line.contains(
"GeneralReportModule"));
boolean accept(String line)
static< T > List< T > getInterfaceImplementations(LineFilter filter, Class< T > interfaceClass)
static List< IngestModuleFactory > getIngestModuleFactories()
boolean accept(String line)
static List< GeneralReportModule > getGeneralReportModules()
static final Logger logger
static< T > T createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class< T > interfaceClass)
boolean accept(File dir, String name)
boolean accept(String line)
synchronized static Logger getLogger(String name)