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;
 
   44 import java.io.BufferedReader;
 
   45 import java.io.FileReader;
 
   74     @Messages({
"JythonModuleLoader.pythonInterpreterError.title=Python Modules",
 
   75                 "JythonModuleLoader.pythonInterpreterError.msg=Failed to load python modules, See log for more details"})
 
   77         List<T> objects = 
new ArrayList<>();
 
   78         Set<File> pythonModuleDirs = 
new HashSet<>();
 
   79         PythonInterpreter interpreter = null;
 
   82             interpreter = 
new PythonInterpreter();
 
   83         } 
catch (Exception ex) {
 
   84             logger.log(Level.SEVERE, 
"Failed to load python Intepreter. Cannot load python modules", ex);
 
   92         for (File f : InstalledFileLocator.getDefault().locateAll(
"InternalPythonModules", 
"org.sleuthkit.autopsy.core", 
false)) { 
 
   93             Collections.addAll(pythonModuleDirs, f.listFiles());
 
   98         for (File file : pythonModuleDirs) {
 
   99             if (file.isDirectory()) {
 
  101                 for (File script : pythonScripts) {
 
  102                         try (Scanner fileScanner = 
new Scanner(
new BufferedReader(
new FileReader(script)))) {
 
  103                         while (fileScanner.hasNextLine()) {
 
  104                             String line = fileScanner.nextLine();
 
  105                             if (line.startsWith(
"class ") && filter.
accept(line)) { 
 
  106                                 String className = line.substring(6, line.indexOf(
"("));
 
  109                                 } 
catch (Exception ex) {
 
  110                                     logger.log(Level.SEVERE, String.format(
"Failed to load %s from %s", className, script.getAbsolutePath()), ex); 
 
  112                                     DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
 
  113                                             NbBundle.getMessage(
JythonModuleLoader.class, 
"JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
 
  114                                             NotifyDescriptor.ERROR_MESSAGE));
 
  118                     } 
catch (FileNotFoundException ex) {
 
  119                         logger.log(Level.SEVERE, String.format(
"Failed to open %s", script.getAbsolutePath()), ex); 
 
  120                         DialogDisplayer.getDefault().notify(
new NotifyDescriptor.Message(
 
  121                                 NbBundle.getMessage(
JythonModuleLoader.class, 
"JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
 
  122                                 NotifyDescriptor.ERROR_MESSAGE));
 
  130     private static <T> T 
createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
 
  134         interpreter.exec(
"import sys"); 
 
  135         String path = Matcher.quoteReplacement(script.getParent());
 
  136         interpreter.exec(
"sys.path.append('" + path + 
"')"); 
 
  137         String moduleName = script.getName().replaceAll(
"\\.py$", 
""); 
 
  140         interpreter.exec(
"import " + moduleName); 
 
  141         interpreter.exec(
"reload(" + moduleName + 
")"); 
 
  144         interpreter.exec(
"from " + moduleName + 
" import " + className); 
 
  145         interpreter.exec(
"obj = " + className + 
"()"); 
 
  147         T obj = interpreter.get(
"obj", interfaceClass); 
 
  151         interpreter.exec(
"sys.path.remove('" + path + 
"')"); 
 
  159         public boolean accept(File dir, String name) {
 
  160             return name.endsWith(
".py"); 
 
  166         boolean accept(String line);
 
  173             return (line.contains(
"IngestModuleFactoryAdapter") || line.contains(
"IngestModuleFactory")); 
 
  181             return (line.contains(
"GeneralReportModuleAdapter") || line.contains(
"GeneralReportModule")); 
 
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)