From c5d44441284d17103428dbb3daaddeab73bd8205 Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Fri, 24 Jan 2025 15:19:40 -0500 Subject: [PATCH] GP-5298: PyGhidra can now find modules that live in directories specified by the Bundle Manager --- .../PyGhidra/src/main/py/src/pyghidra/launcher.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py index 9fc258d123..d7db030ebb 100644 --- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py +++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/launcher.py @@ -109,6 +109,17 @@ class _PyGhidraImportLoader: def exec_module(self, fullname): pass +class _GhidraBundleFinder(importlib.machinery.PathFinder): + """ (internal) Used to find modules in Ghidra bundle locations """ + + def find_spec(self, fullname, path=None, target=None): + from ghidra.app.script import GhidraScriptUtil + GhidraScriptUtil.acquireBundleHostReference() + for directory in GhidraScriptUtil.getEnabledScriptSourceDirectories(): + spec = super().find_spec(fullname, [directory.absolutePath], target) + if spec is not None: + return spec + return None @contextlib.contextmanager def _plugin_lock(): @@ -394,8 +405,9 @@ class PyGhidraLauncher: **jpype_kwargs ) - # Install hook into python importlib + # Install hooks into python importlib sys.meta_path.append(_PyGhidraImportLoader()) + sys.meta_path.append(_GhidraBundleFinder()) imports.registerDomain("ghidra")