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.io.IOException;
48 import java.text.MessageFormat;
49 import java.util.Comparator;
50 import org.apache.commons.io.FileUtils;
107 if (!userDirInternalPython.exists()) {
108 userDirInternalPython.mkdirs();
110 File installInternalPython = InstalledFileLocator.getDefault().locate(INTERNAL_PYTHON_MODULES_FOLDER,
"org.sleuthkit.autopsy.core",
false);
111 if (installInternalPython.exists()) {
113 FileUtils.copyDirectory(installInternalPython, userDirInternalPython);
114 }
catch (IOException ex) {
115 logger.log(Level.WARNING, MessageFormat.format(
"There was an error copying internal python modules from {0} to {1}.",
116 installInternalPython, userDirInternalPython), ex);
122 @Messages({
"JythonModuleLoader.pythonInterpreterError.title=Python Modules",
123 "JythonModuleLoader.pythonInterpreterError.msg=Failed to load python modules, See log for more details"})
127 List<T> objects =
new ArrayList<>();
128 Set<File> pythonModuleDirs =
new HashSet<>();
129 PythonInterpreter interpreter = null;
132 interpreter =
new PythonInterpreter();
133 }
catch (Exception ex) {
134 logger.log(Level.SEVERE,
"Failed to load python Intepreter. Cannot load python modules", ex);
149 for (File file : pythonModuleDirs) {
150 if (file.isDirectory()) {
152 for (File script : pythonScripts) {
153 try (Scanner fileScanner =
new Scanner(
new BufferedReader(
new FileReader(script)))) {
154 while (fileScanner.hasNextLine()) {
155 String line = fileScanner.nextLine();
156 if (line.startsWith(
"class ") && filter.
accept(line)) {
157 String className = line.substring(6, line.indexOf(
"("));
160 }
catch (Exception ex) {
161 logger.log(Level.SEVERE, String.format(
"Failed to load %s from %s", className, script.getAbsolutePath()), ex);
163 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
164 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
165 NotifyDescriptor.ERROR_MESSAGE));
169 }
catch (FileNotFoundException ex) {
170 logger.log(Level.SEVERE, String.format(
"Failed to open %s", script.getAbsolutePath()), ex);
171 DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
172 NbBundle.getMessage(
JythonModuleLoader.class,
"JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
173 NotifyDescriptor.ERROR_MESSAGE));
179 Collections.sort(objects, Comparator.comparing((T obj) -> obj.getClass().getSimpleName(), (s1, s2) -> s1.compareToIgnoreCase(s2)));
183 private static <T> T
createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
187 interpreter.exec(
"import sys");
188 String path = Matcher.quoteReplacement(script.getParent());
189 interpreter.exec(
"sys.path.append('" + path +
"')");
190 String moduleName = script.getName().replaceAll(
"\\.py$",
"");
193 interpreter.exec(
"import " + moduleName);
194 interpreter.exec(
"reload(" + moduleName +
")");
197 interpreter.exec(
"from " + moduleName +
" import " + className);
198 interpreter.exec(
"obj = " + className +
"()");
200 T obj = interpreter.get(
"obj", interfaceClass);
204 interpreter.exec(
"sys.path.remove('" + path +
"')");
212 public boolean accept(File dir, String name) {
213 return name.endsWith(
".py");
219 boolean accept(String line);
229 return (line.contains(
"IngestModuleFactoryAdapter") || line.contains(
"IngestModuleFactory"));
240 return (line.contains(
"GeneralReportModuleAdapter") || line.contains(
"GeneralReportModule"));
251 return (line.contains(
"DataSourceProcessorAdapter") || line.contains(
"DataSourceProcessor"));
static File getUserDirInternalPython()
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 final String INTERNAL_PYTHON_MODULES_FOLDER
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)
static synchronized void copyInternalInstallToUserDir()
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()