From 995bb26a4decbf78cb2cbc96dbd6b9a9ab528be0 Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Thu, 13 Jul 2023 08:47:55 -0400 Subject: [PATCH] GP-3619: Updating python path more frequently to account for changes from bundle manager --- .../ghidra/app/plugin/core/osgi/BundleHost.java | 16 ++++++++++++++++ .../java/ghidra/app/script/GhidraScriptUtil.java | 11 +++++++++++ .../ghidra/python/GhidraPythonInterpreter.java | 6 +++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java index f760055fb9..597ef58b04 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java @@ -302,6 +302,22 @@ public class BundleHost { return bundleMap.getBundleFiles(); } + /** + * Return the list of currently managed enabled bundle files. + * + * @return all the enabled bundle files + */ + public Collection getEnabledBundleFiles() { + List enabledList = new ArrayList<>(); + for (ResourceFile bundleFile : bundleMap.getBundleFiles()) { + GhidraBundle bundle = bundleMap.get(bundleFile); + if (bundle.isEnabled()) { + enabledList.add(bundleFile); + } + } + return enabledList; + } + /** * Attempt to resolve a list of BundleRequirements with active Bundle capabilities. * diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java index 382b7c67aa..ea621c07aa 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java @@ -123,6 +123,17 @@ public class GhidraScriptUtil { .collect(Collectors.toList()); } + /** + * Returns a list of the current enabled script directories. + * @return a list of the current enabled script directories + */ + public static List getEnabledScriptSourceDirectories() { + return bundleHost.getEnabledBundleFiles() + .stream() + .filter(ResourceFile::isDirectory) + .collect(Collectors.toList()); + } + /** * Search the currently managed source directories for the given script file. * diff --git a/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java b/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java index 5584e61a86..f9377810f2 100644 --- a/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java +++ b/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java @@ -121,8 +121,6 @@ public class GhidraPythonInterpreter extends InteractiveInterpreter { // Add __builtin__ module for code completion builtinModule = (PyModule) imp.load("__builtin__"); - - initializePythonPath(); } /** @@ -134,7 +132,7 @@ public class GhidraPythonInterpreter extends InteractiveInterpreter { systemState.path.retainAll(defaultPythonPath); // Add in Ghidra script source directories - for (ResourceFile resourceFile : GhidraScriptUtil.getScriptSourceDirectories()) { + for (ResourceFile resourceFile : GhidraScriptUtil.getEnabledScriptSourceDirectories()) { systemState.path.append(Py.newStringOrUnicode(resourceFile.getFile(false).getAbsolutePath())); } @@ -168,6 +166,7 @@ public class GhidraPythonInterpreter extends InteractiveInterpreter { "Ghidra python interpreter has already been cleaned up."); } + initializePythonPath(); injectScriptHierarchy(script); if (buffer.length() > 0) { @@ -207,6 +206,7 @@ public class GhidraPythonInterpreter extends InteractiveInterpreter { "Ghidra python interpreter has already been cleaned up."); } + initializePythonPath(); injectScriptHierarchy(script); Py.getThreadState().tracefunc = interruptTraceFunction;