Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
JythonModuleLoader.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014-2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.python;
20 
21 import java.io.File;
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;
29 import java.util.Set;
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;
48 
53 public final class JythonModuleLoader {
54 
55  private static final Logger logger = Logger.getLogger(JythonModuleLoader.class.getName());
56 
63  public static synchronized List<IngestModuleFactory> getIngestModuleFactories() {
65  }
66 
73  public static synchronized List<GeneralReportModule> getGeneralReportModules() {
75  }
76 
83  public static synchronized List<DataSourceProcessor> getDataSourceProcessorModules() {
85  }
86 
87  @Messages({"JythonModuleLoader.pythonInterpreterError.title=Python Modules",
88  "JythonModuleLoader.pythonInterpreterError.msg=Failed to load python modules, See log for more details"})
89  private static <T> List<T> getInterfaceImplementations(LineFilter filter, Class<T> interfaceClass) {
90  List<T> objects = new ArrayList<>();
91  Set<File> pythonModuleDirs = new HashSet<>();
92  PythonInterpreter interpreter = null;
93  // This method has previously thrown unchecked exceptions when it could not load because of non-latin characters.
94  try {
95  interpreter = new PythonInterpreter();
96  } catch (Exception ex) {
97  logger.log(Level.SEVERE, "Failed to load python Intepreter. Cannot load python modules", ex);
99  MessageNotifyUtil.Notify.show(Bundle.JythonModuleLoader_pythonInterpreterError_title(),Bundle.JythonModuleLoader_pythonInterpreterError_msg(), MessageNotifyUtil.MessageType.ERROR);
100  }
101  return objects;
102  }
103  // add python modules from 'autospy/build/cluster/InternalPythonModules' folder
104  // which are copied from 'autopsy/*/release/InternalPythonModules' folders.
105  for (File f : InstalledFileLocator.getDefault().locateAll("InternalPythonModules", "org.sleuthkit.autopsy.core", false)) { //NON-NLS
106  Collections.addAll(pythonModuleDirs, f.listFiles());
107  }
108  // add python modules from 'testuserdir/python_modules' folder
109  Collections.addAll(pythonModuleDirs, new File(PlatformUtil.getUserPythonModulesPath()).listFiles());
110 
111  for (File file : pythonModuleDirs) {
112  if (file.isDirectory()) {
113  File[] pythonScripts = file.listFiles(new PythonScriptFileFilter());
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)) { //NON-NLS
119  String className = line.substring(6, line.indexOf("("));
120  try {
121  objects.add(createObjectFromScript(interpreter, script, className, interfaceClass));
122  } catch (Exception ex) {
123  logger.log(Level.SEVERE, String.format("Failed to load %s from %s", className, script.getAbsolutePath()), ex); //NON-NLS
124  // NOTE: using ex.toString() because the current version is always returning null for ex.getMessage().
125  DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
126  NbBundle.getMessage(JythonModuleLoader.class, "JythonModuleLoader.errorMessages.failedToLoadModule", className, ex.toString()),
127  NotifyDescriptor.ERROR_MESSAGE));
128  }
129  }
130  }
131  } catch (FileNotFoundException ex) {
132  logger.log(Level.SEVERE, String.format("Failed to open %s", script.getAbsolutePath()), ex); //NON-NLS
133  DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
134  NbBundle.getMessage(JythonModuleLoader.class, "JythonModuleLoader.errorMessages.failedToOpenModule", script.getAbsolutePath()),
135  NotifyDescriptor.ERROR_MESSAGE));
136  }
137  }
138  }
139  }
140 
141  Collections.sort(objects, Comparator.comparing((T obj) -> obj.getClass().getSimpleName(), (s1, s2) -> s1.compareToIgnoreCase(s2)));
142  return objects;
143  }
144 
145  private static <T> T createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class<T> interfaceClass) {
146  // Add the directory where the Python script resides to the Python
147  // module search path to allow the script to use other scripts bundled
148  // with it.
149  interpreter.exec("import sys"); //NON-NLS
150  String path = Matcher.quoteReplacement(script.getParent());
151  interpreter.exec("sys.path.append('" + path + "')"); //NON-NLS
152  String moduleName = script.getName().replaceAll("\\.py$", ""); //NON-NLS
153 
154  // reload the module so that the changes made to it can be loaded.
155  interpreter.exec("import " + moduleName); //NON-NLS
156  interpreter.exec("reload(" + moduleName + ")"); //NON-NLS
157 
158  // Importing the appropriate class from the Py Script which contains multiple classes.
159  interpreter.exec("from " + moduleName + " import " + className); //NON-NLS
160  interpreter.exec("obj = " + className + "()"); //NON-NLS
161 
162  T obj = interpreter.get("obj", interfaceClass); //NON-NLS
163 
164  // Remove the directory where the Python script resides from the Python
165  // module search path.
166  interpreter.exec("sys.path.remove('" + path + "')"); //NON-NLS
167 
168  return obj;
169  }
170 
171  private static class PythonScriptFileFilter implements FilenameFilter {
172 
173  @Override
174  public boolean accept(File dir, String name) {
175  return name.endsWith(".py"); //NON-NLS
176  } //NON-NLS
177  }
178 
179  private static interface LineFilter {
180 
181  boolean accept(String line);
182  }
183 
187  private static class IngestModuleFactoryDefFilter implements LineFilter {
188 
189  @Override
190  public boolean accept(String line) {
191  return (line.contains("IngestModuleFactoryAdapter") || line.contains("IngestModuleFactory")); //NON-NLS
192  }
193  }
194 
198  private static class GeneralReportModuleDefFilter implements LineFilter {
199 
200  @Override
201  public boolean accept(String line) {
202  return (line.contains("GeneralReportModuleAdapter") || line.contains("GeneralReportModule")); //NON-NLS
203  }
204  }
205 
209  private static class DataSourceProcessorDefFilter implements LineFilter {
210 
211  @Override
212  public boolean accept(String line) {
213  return (line.contains("DataSourceProcessorAdapter") || line.contains("DataSourceProcessor")); //NON-NLS
214  }
215  }
216 }
static< T > List< T > getInterfaceImplementations(LineFilter filter, Class< T > interfaceClass)
static synchronized List< IngestModuleFactory > getIngestModuleFactories()
static< T > T createObjectFromScript(PythonInterpreter interpreter, File script, String className, Class< T > interfaceClass)
static synchronized List< GeneralReportModule > getGeneralReportModules()
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void show(String title, String message, MessageType type, ActionListener actionListener)
static synchronized List< DataSourceProcessor > getDataSourceProcessorModules()

Copyright © 2012-2022 Basis Technology. Generated on: Sun Oct 2 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.