Skip to content

Ghidra is a software reverse engineering (SRE) framework

Close PyGhidra compiler file manager on success and failure

Rudy Celekli2c8ec31b40bf
diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/javac.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/javac.py
index 73c07b3b01..1d680cd50c 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/javac.py
+++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/javac.py
@@ -79,28 +79,28 @@ def java_compile(src_path: Path, jar_path: Path):
with tempfile.TemporaryDirectory() as out:
outdir = Path(out).resolve()
compiler = ToolProvider.getSystemJavaCompiler()
- fman = compiler.getStandardFileManager(None, None, None)
- cp = [JPath @ (Path(p)) for p in System.getProperty("java.class.path").split(pathsep)]
- fman.setLocationFromPaths(StandardLocation.CLASS_PATH, cp)
- if src_path.is_dir():
- fman.setLocationFromPaths(StandardLocation.SOURCE_PATH, [JPath @ (src_path.resolve())])
- fman.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, [JPath @ (outdir)])
- sources = None
- if src_path.is_file():
- sources = fman.getJavaFileObjectsFromPaths([JPath @ (src_path)])
- else:
- glob = src_path.glob("**/*.java")
- sources = fman.getJavaFileObjectsFromPaths([JPath @ (p) for p in glob])
-
- diagnostics = _CompilerDiagnosticListener()
- task = compiler.getTask(Writer.nullWriter(), fman, diagnostics, COMPILER_OPTIONS, None, sources)
-
- if not task.call():
- msg = "\n".join([str(error) for error in diagnostics.errors])
- raise ValueError(msg)
-
- if jar_path.suffix == '.jar':
- jar_path.parent.mkdir(exist_ok=True, parents=True)
- _to_jar_(jar_path, outdir)
- else:
- shutil.copytree(outdir, jar_path, dirs_exist_ok=True)
+ with compiler.getStandardFileManager(None, None, None) as fman:
+ cp = [JPath @ (Path(p)) for p in System.getProperty("java.class.path").split(pathsep)]
+ fman.setLocationFromPaths(StandardLocation.CLASS_PATH, cp)
+ if src_path.is_dir():
+ fman.setLocationFromPaths(StandardLocation.SOURCE_PATH, [JPath @ (src_path.resolve())])
+ fman.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, [JPath @ (outdir)])
+ sources = None
+ if src_path.is_file():
+ sources = fman.getJavaFileObjectsFromPaths([JPath @ (src_path)])
+ else:
+ glob = src_path.glob("**/*.java")
+ sources = fman.getJavaFileObjectsFromPaths([JPath @ (p) for p in glob])
+
+ diagnostics = _CompilerDiagnosticListener()
+ task = compiler.getTask(Writer.nullWriter(), fman, diagnostics, COMPILER_OPTIONS, None, sources)
+
+ if not task.call():
+ msg = "\n".join([str(error) for error in diagnostics.errors])
+ raise ValueError(msg)
+
+ if jar_path.suffix == '.jar':
+ jar_path.parent.mkdir(exist_ok=True, parents=True)
+ _to_jar_(jar_path, outdir)
+ else:
+ shutil.copytree(outdir, jar_path, dirs_exist_ok=True)