Skip to content

Commit

Permalink
add check for jep version
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-hunhoff committed Jan 25, 2024
1 parent be96ccb commit 84b80c1
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/main/java/ghidrathon/interpreter/GhidrathonInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
/** Utility class used to configure a Jep instance to access Ghidra */
public class GhidrathonInterpreter {

private static final String GHIDRATHON_SAVE_FILENAME = "ghidrathon.save";
private static final String SUPPORTED_JEP_VERSION = "4.2.0";

private Jep jep_ = null;
private PrintWriter out = null;
private PrintWriter err = null;
Expand Down Expand Up @@ -87,6 +90,27 @@ private GhidrathonInterpreter(GhidrathonConfig config) throws JepException, IOEx
"sys.path.extend([r\"%s\"])",
Application.getModuleDataSubDirectory(GhidrathonUtils.THIS_EXTENSION_NAME, "python")
.getAbsolutePath()));

// print embedded interpreter configuration to application.log
Msg.info(GhidrathonInterpreter.class, "Embedded Python configuration:");
Msg.info(
GhidrathonInterpreter.class, String.format("Python %s", jep_.getValue("sys.version")));

String[] sysVars = {
"sys.executable",
"sys._base_executable",
"sys.prefix",
"sys.base_prefix",
"sys.exec_prefix",
"sys.base_exec_prefix"
};

for (String sysVar : sysVars) {
Msg.info(
GhidrathonInterpreter.class,
String.format("%s = \"%s\"", sysVar, jep_.getValue(sysVar)));
}

jepPythonSysModuleInitialized.set(true);
}

Expand Down Expand Up @@ -155,7 +179,7 @@ private void configureJepMainInterpreter() throws JepException, FileNotFoundExce
File ghidrathonSaveFile =
new File(
Application.getApplicationRootDirectory().getParentFile().getFile(false),
"ghidrathon.save");
GhidrathonInterpreter.GHIDRATHON_SAVE_FILENAME);
if (!(ghidrathonSaveFile.exists() && ghidrathonSaveFile.isFile())) {
throw new JepException(
String.format(
Expand Down Expand Up @@ -236,6 +260,40 @@ private void configureJepMainInterpreter() throws JepException, FileNotFoundExce
GhidrathonInterpreter.class,
String.format("Using Jep native file at %s.", this.jepNativeFile.getAbsolutePath()));

File jepVersionFile = new File(this.jepPythonPackageDir, "version.py");

if (!(jepVersionFile.exists() && jepVersionFile.isFile())) {
throw new JepException(
String.format(
"%s is not valid - could not check Jep version. Please verify your jep installation"
+ " works before running Ghidrathon.",
jepVersionFile.getAbsolutePath()));
}

boolean isCorrectJepVersion = false;
try (BufferedReader br = new BufferedReader(new FileReader(jepVersionFile))) {
for (String line; (line = br.readLine()) != null; ) {
if (line.contains(GhidrathonInterpreter.SUPPORTED_JEP_VERSION)) {
isCorrectJepVersion = true;
break;
}
}
} catch (IOException e) {
throw new JepException(
String.format("Failed to read %s (%s).", jepVersionFile.getAbsolutePath(), e));
}

if (!isCorrectJepVersion) {
throw new JepException(
String.format(
"Please install Jep version %s before running Ghidrathon.",
GhidrathonInterpreter.SUPPORTED_JEP_VERSION));
}

Msg.info(
GhidrathonInterpreter.class,
String.format("Using Jep version %s.", GhidrathonInterpreter.SUPPORTED_JEP_VERSION));

try {
MainInterpreter.setJepLibraryPath(this.jepNativeFile.getAbsolutePath());

Expand Down

0 comments on commit 84b80c1

Please sign in to comment.