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;
72 @Messages({
"JythonModuleLoader.pythonInterpreterError.title=Python Modules",
73 "JythonModuleLoader.pythonInterpreterError.msg=Failed to load python modules, See log for more details"})
75 List<T> objects =
new ArrayList<>();
76 Set<File> pythonModuleDirs =
new HashSet<>();
77 PythonInterpreter interpreter = null;
80 interpreter =
new PythonInterpreter();
81 }
catch (Exception ex) {
82 logger.log(Level.SEVERE,
"Failed to load python Intepreter. Cannot load python modules", ex);
90 for (File f : InstalledFileLocator.getDefault().locateAll(
"InternalPythonModules",
"org.sleuthkit.autopsy.core",
false)) {
91 Collections.addAll(pythonModuleDirs, f.listFiles());
96 for (File file : pythonModuleDirs) {
97 if (file.isDirectory()) {
99 for (File script : pythonScripts) {
100 try (Scanner fileScanner =
new Scanner(script)) {
101 while (fileScanner.hasNextLine()) {
102 String line = fileScanner.nextLine();
103 if (line.startsWith(
"class ") && filter.
accept(line)) {
104 String className = line.substring(6, line.indexOf(
"("));
107 }
catch (Exception ex) {
108 logger.log(Level.SEVERE, String.format(
"Failed to load %s from %s", className, script.getAbsolutePath()), ex);
110 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
111 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
112 NotifyDescriptor.ERROR_MESSAGE));
116 }
catch (FileNotFoundException ex) {
117 logger.log(Level.SEVERE, String.format(
"Failed to open %s", script.getAbsolutePath()), ex);
118 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
119 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
120 NotifyDescriptor.ERROR_MESSAGE));
128 private static <T> T
createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
132 interpreter.exec(
"import sys");
133 String path = Matcher.quoteReplacement(script.getParent());
134 interpreter.exec(
"sys.path.append('" + path +
"')");
135 String moduleName = script.getName().replaceAll(
"\\.py$",
"");
138 interpreter.exec(
"import " + moduleName);
139 interpreter.exec(
"reload(" + moduleName +
")");
142 interpreter.exec(
"from " + moduleName +
" import " + className);
143 interpreter.exec(
"obj = " + className +
"()");
145 T obj = interpreter.get(
"obj", interfaceClass);
149 interpreter.exec(
"sys.path.remove('" + path +
"')");
157 public boolean accept(File dir, String name) {
158 return name.endsWith(
".py");
164 boolean accept(String line);
171 return (line.contains(
"IngestModuleFactoryAdapter") || line.contains(
"IngestModuleFactory"));
179 return (line.contains(
"GeneralReportModuleAdapter") || line.contains(
"GeneralReportModule"));
boolean accept(String line)
static< T > List< T > getInterfaceImplementations(LineFilter filter, Class< T > interfaceClass)
static boolean runningWithGUI
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)
static void show(String title, String message, MessageType type, ActionListener actionListener)