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.openide.util.NbBundle.Messages;
37 import org.python.util.PythonInterpreter;
45 import java.io.BufferedReader;
46 import java.io.FileReader;
47 import java.util.Comparator;
87 @Messages({
"JythonModuleLoader.pythonInterpreterError.title=Python Modules",
88 "JythonModuleLoader.pythonInterpreterError.msg=Failed to load python modules, See log for more details"})
90 List<T> objects =
new ArrayList<>();
91 Set<File> pythonModuleDirs =
new HashSet<>();
92 PythonInterpreter interpreter = null;
95 interpreter =
new PythonInterpreter();
96 }
catch (Exception ex) {
97 logger.log(Level.SEVERE,
"Failed to load python Intepreter. Cannot load python modules", ex);
105 for (File f : InstalledFileLocator.getDefault().locateAll(
"InternalPythonModules",
"org.sleuthkit.autopsy.core",
false)) {
106 Collections.addAll(pythonModuleDirs, f.listFiles());
111 for (File file : pythonModuleDirs) {
112 if (file.isDirectory()) {
114 for (File script : pythonScripts) {
115 try (Scanner fileScanner =
new Scanner(
new BufferedReader(
new FileReader(script)))) {
116 while (fileScanner.hasNextLine()) {
117 String line = fileScanner.nextLine();
118 if (line.startsWith(
"class ") && filter.
accept(line)) {
119 String className = line.substring(6, line.indexOf(
"("));
122 }
catch (Exception ex) {
123 logger.log(Level.SEVERE, String.format(
"Failed to load %s from %s", className, script.getAbsolutePath()), ex);
125 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
126 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
127 NotifyDescriptor.ERROR_MESSAGE));
131 }
catch (FileNotFoundException ex) {
132 logger.log(Level.SEVERE, String.format(
"Failed to open %s", script.getAbsolutePath()), ex);
133 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
134 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
135 NotifyDescriptor.ERROR_MESSAGE));
141 Collections.sort(objects, Comparator.comparing((T obj) -> obj.getClass().getSimpleName(), (s1, s2) -> s1.compareToIgnoreCase(s2)));
145 private static <T> T
createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
149 interpreter.exec(
"import sys");
150 String path = Matcher.quoteReplacement(script.getParent());
151 interpreter.exec(
"sys.path.append('" + path +
"')");
152 String moduleName = script.getName().replaceAll(
"\\.py$",
"");
155 interpreter.exec(
"import " + moduleName);
156 interpreter.exec(
"reload(" + moduleName +
")");
159 interpreter.exec(
"from " + moduleName +
" import " + className);
160 interpreter.exec(
"obj = " + className +
"()");
162 T obj = interpreter.get(
"obj", interfaceClass);
166 interpreter.exec(
"sys.path.remove('" + path +
"')");
174 public boolean accept(File dir, String name) {
175 return name.endsWith(
".py");
181 boolean accept(String line);
191 return (line.contains(
"IngestModuleFactoryAdapter") || line.contains(
"IngestModuleFactory"));
202 return (line.contains(
"GeneralReportModuleAdapter") || line.contains(
"GeneralReportModule"));
213 return (line.contains(
"DataSourceProcessorAdapter") || line.contains(
"DataSourceProcessor"));
boolean accept(String line)
static< T > List< T > getInterfaceImplementations(LineFilter filter, Class< T > interfaceClass)
static synchronized List< IngestModuleFactory > getIngestModuleFactories()
static boolean runningWithGUI
boolean accept(String line)
static final Logger logger
static< T > T createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class< T > interfaceClass)
static synchronized List< GeneralReportModule > getGeneralReportModules()
boolean accept(File dir, String name)
boolean accept(String line)
synchronized static Logger getLogger(String name)
static void show(String title, String message, MessageType type, ActionListener actionListener)
boolean accept(String line)
static synchronized List< DataSourceProcessor > getDataSourceProcessorModules()