Skip to content

Commit

Permalink
Removed Python from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeByDrescher committed Jan 21, 2025
1 parent bc846c5 commit d1de15a
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 93 deletions.
27 changes: 27 additions & 0 deletions vcell-cli/src/main/java/org/vcell/cli/CLIPythonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public class CLIPythonManager {
/**
* Retrieve the Python Manager, or create and return if it doesn't exist.
* @return the manager
* @deprecated CLIPythonManager is no longer used in CLI, and has no current use; should this be used again
* you need to check whether there are still bugs with Arm Macs, and whether single-instance python shells are not a viable alternative
*/
@Deprecated
public static CLIPythonManager getInstance(){
lg.trace("Getting Python instance");
if (instance == null){
Expand All @@ -62,7 +65,9 @@ public static CLIPythonManager getInstance(){
* @param arguments the arguments to provide to the function call, each as their own string, in the correct order
* @return the return response from Python
* @throws PythonStreamException if there is any exception encountered in this exchange.
* @deprecated See getInstance()
*/
@Deprecated
public String callPython(String functionName, String... arguments) throws PythonStreamException {
String command = this.formatPythonFunctionCall(functionName, arguments);
try {
Expand All @@ -82,7 +87,9 @@ public String callPython(String functionName, String... arguments) throws Python
* @param cliCommand the command to run
* @throws InterruptedException if the python process was interrupted
* @throws IOException if there was a system IO failure
* @deprecated See getInstance()
*/
@Deprecated
private static String callNonSharedPython(String cliCommand)
throws InterruptedException, IOException, PythonStreamException {
Path cliWorkingDir = Paths.get(PropertyLoader.getRequiredProperty(PropertyLoader.cliWorkingDir));
Expand All @@ -96,7 +103,9 @@ private static String callNonSharedPython(String cliCommand)
* Shuts down the python session and cleans up.
*
* @throws IOException if there is a system IO issue.
* @deprecated See getInstance()
*/
@Deprecated
public void closePythonProcess() throws IOException {
// Exit the living Python Process
lg.debug("Closing Python Instance");
Expand All @@ -122,7 +131,9 @@ public void closePythonProcess() throws IOException {
* as means of installation verification.
*
* @throws IOException if there is a problem with System I/O
* @deprecated this entire system is unstable on ARM Macs, and still a bit slow.
*/
@Deprecated
public void instantiatePythonProcess() throws IOException, PythonStreamException {
if (this.pythonProcess != null) return; // prevent override
lg.info("Initializing Python...");
Expand Down Expand Up @@ -338,10 +349,26 @@ private static String runAndPrintProcessStreams(ProcessBuilder pb, String outStr
return os;
}

/**
* Checks whether python returned successfully or not
* @param returnedString
* @throws PythonStreamException
* @deprecated See getInstance()
*/
@Deprecated
public void parsePythonReturn(String returnedString) throws PythonStreamException {
this.parsePythonReturn(returnedString, null, null);
}

/**
* Checks whether python returned successfully or not
* @param returnedString
* @param outString
* @param errString
* @throws PythonStreamException
* @deprecated See getInstance()
*/
@Deprecated
public void parsePythonReturn(String returnedString, String outString, String errString) throws PythonStreamException {
boolean DEBUG_NORMAL_OUTPUT = lg.isTraceEnabled(); // Consider getting rid of this, currently redundant
String ERROR_PHRASE1 = "Traceback", ERROR_PHRASE2 = "File \"<stdin>\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class BiosimulationsCommand implements Callable<Integer> {
private boolean help;

public Integer call() {
CLIRecorder cliRecorder = null;
CLIRecorder cliRecorder;
int returnCode;

if ((returnCode = this.noFurtherActionNeeded(bQuiet, bDebug, bVersion)) != -1)
if ((returnCode = BiosimulationsCommand.noFurtherActionNeeded(bQuiet, bDebug, bVersion)) != -1)
return returnCode;

try {
Expand Down Expand Up @@ -94,21 +94,14 @@ public Integer call() {
logger.info("Beginning execution");
File tmpDir = Files.createTempDirectory("VCell_CLI_" + Long.toHexString(new Date().getTime())).toFile();
try {
CLIPythonManager.getInstance().instantiatePythonProcess();
ExecuteImpl.singleMode(ARCHIVE, tmpDir, cliRecorder, true);
CLIPythonManager.getInstance().closePythonProcess(); // Give the process time to finish
if (!Tracer.hasErrors()) return 0;
if (!bQuiet) {
logger.error("Errors occurred during execution");
Tracer.reportErrors(bDebug);
}
return 1;
} finally {
try {
CLIPythonManager.getInstance().closePythonProcess(); // WARNING: Python will need reinstantiation after this is called
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
logger.debug("Finished all execution.");
FileUtils.copyDirectoryContents(tmpDir, OUT_DIR, true, null);
}
Expand All @@ -121,7 +114,7 @@ public Integer call() {
}
}

private int noFurtherActionNeeded(boolean bQuiet, boolean bDebug, boolean bVersion){
private static int noFurtherActionNeeded(boolean bQuiet, boolean bDebug, boolean bVersion){
logger.debug("Validating CLI arguments");
if (bVersion) {
String version = PropertyLoader.getRequiredProperty(PropertyLoader.vcellSoftwareVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public Integer call() {
System.err.println("cannot specify both debug and quiet, try --help for usage");
return 1;
}

CLIPythonManager.getInstance().instantiatePythonProcess();


Executable.setGlobalTimeoutMS(EXECUTABLE_MAX_WALLCLOCK_MILLIS);
Expand All @@ -132,7 +130,7 @@ public Integer call() {
bEncapsulateOutput, bSmallMeshOverride);
}
}
CLIPythonManager.getInstance().closePythonProcess();

// WARNING: Python needs re-instantiation once the above line is called!
FileUtils.copyDirectoryContents(tmpDir, outputFilePath, true, null);
return 0;
Expand Down
3 changes: 2 additions & 1 deletion vcell-cli/src/main/java/org/vcell/cli/run/ExecuteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public static void batchMode(File dirOfArchivesToProcess, File outputDir, CLIRec

private static void runSingleExecOmex(File inputFile, File outputDir, CLIRecordable cliLogger, boolean bKeepTempFiles,
boolean bExactMatchOnly, boolean bSmallMeshOverride)
throws IOException, ExecutionException, PythonStreamException, InterruptedException, BiosimulationsHdfWriterException {
throws IOException, ExecutionException, PythonStreamException, BiosimulationsHdfWriterException {

String bioModelBaseName = inputFile.getName().substring(0, inputFile.getName().indexOf(".")); // ".omex"??
Files.createDirectories(Paths.get(outputDir.getAbsolutePath() + File.separator + bioModelBaseName)); // make output subdir
final boolean bEncapsulateOutput = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,14 @@ public Integer call() {
config.updateLoggers();


CLIPythonManager.getInstance().instantiatePythonProcess();

Executable.setGlobalTimeoutMS(EXECUTABLE_MAX_WALLCLOCK_MILLIS);
logger.info("Beginning execution");
File tmpDir = Files.createTempDirectory("VCell_CLI_" + Long.toHexString(new Date().getTime())).toFile();

Tracer.clearTraceEvents();
ExecuteImpl.singleMode(inputFilePath, tmpDir, cliTracer, bKeepTempFiles, bExactMatchOnly,
bEncapsulateOutput, bSmallMeshOverride);
CLIPythonManager.getInstance().closePythonProcess();

// WARNING: Python needs re-instantiation once the above line is called!
FileUtils.copyDirectoryContents(tmpDir, outputFilePath, true, null);
final OmexExecSummary omexExecSummary;
Expand Down
18 changes: 8 additions & 10 deletions vcell-cli/src/main/java/org/vcell/cli/run/ExecutionJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public class ExecutionJob {

private long startTime_ms, endTime_ms;
private boolean bExactMatchOnly, bSmallMeshOverride, bKeepTempFiles;
private StringBuilder logOmexMessage;
private String inputFilePath;
private final StringBuilder logOmexMessage;
private String bioModelBaseName;
private String outputDir;
private boolean anySedmlDocumentHasSucceeded = false; // set to true if at least one sedml document run is successful
Expand Down Expand Up @@ -56,8 +55,7 @@ public ExecutionJob(File inputFile, File rootOutputDir, CLIRecordable cliRecorde
this();
this.inputFile = inputFile;
this.cliRecorder = cliRecorder;

this.inputFilePath = inputFile.getAbsolutePath();

this.bioModelBaseName = FileUtils.getBaseName(inputFile.getName()); // input file without the path
String outputBaseDir = rootOutputDir.getAbsolutePath();
this.outputDir = bEncapsulateOutput ? Paths.get(outputBaseDir, bioModelBaseName).toString() : outputBaseDir;
Expand All @@ -67,7 +65,7 @@ public ExecutionJob(File inputFile, File rootOutputDir, CLIRecordable cliRecorde
}

private ExecutionJob(){
this.logOmexMessage = new StringBuilder("");
this.logOmexMessage = new StringBuilder();
}

/**
Expand All @@ -77,29 +75,29 @@ private ExecutionJob(){
* @throws PythonStreamException if calls to the python-shell instance are not working correctly
* @throws IOException if there are system I/O issues.
*/
public void preprocessArchive() throws PythonStreamException, IOException {
public void preprocessArchive() throws IOException {
// Start the clock
this.startTime_ms = System.currentTimeMillis();

// Beginning of Execution
logger.info("Executing OMEX archive `{}`", this.inputFile.getName());
logger.info("Archive location: {}", this.inputFilePath);
logger.info("Archive location: {}", this.inputFile.getAbsolutePath());
RunUtils.drawBreakLine("-", 100);

// Unpack the Omex Archive
try { // It's unlikely, but if we get errors here they're fatal.
this.sedmlPath2d3d = Paths.get(this.outputDir, "temp");
this.omexHandler = new OmexHandler(this.inputFilePath, this.outputDir);
this.omexHandler = new OmexHandler(this.inputFile.getAbsolutePath(), this.outputDir);
this.omexHandler.extractOmex();
this.sedmlLocations = this.omexHandler.getSedmlLocationsAbsolute();
} catch (IOException e){
String error = e.getMessage() + ", error for OmexHandler with " + this.inputFilePath;
String error = e.getMessage() + ", error for OmexHandler with " + this.inputFile.getAbsolutePath();
this.cliRecorder.writeErrorList(e, this.bioModelBaseName);
this.cliRecorder.writeDetailedResultList(this.bioModelBaseName + ", " + "IO error with OmexHandler");
logger.error(error);
throw new RuntimeException(error, e);
} catch (Exception e) {
String error = e.getMessage() + ", error for archive " + this.inputFilePath;
String error = e.getMessage() + ", error for archive " + this.inputFile.getAbsolutePath();
logger.error(error);
if (this.omexHandler != null) this.omexHandler.deleteExtractedOmex();
this.cliRecorder.writeErrorList(e, this.bioModelBaseName);
Expand Down
55 changes: 0 additions & 55 deletions vcell-cli/src/main/java/org/vcell/cli/run/PythonCalls.java

This file was deleted.

4 changes: 2 additions & 2 deletions vcell-cli/src/main/java/org/vcell/cli/run/SedmlJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public boolean preProcessDoc() throws PythonStreamException, InterruptedExceptio
Path path = Paths.get(this.plotFile.getAbsolutePath());
if (!Files.exists(path)){
// SED-ML file generated by python VCell_cli_util
PythonCalls.genSedmlForSed2DAnd3D(this.MASTER_OMEX_ARCHIVE.getAbsolutePath(), this.RESULTS_DIRECTORY_PATH);
//PythonCalls.genSedmlForSed2DAnd3D(this.MASTER_OMEX_ARCHIVE.getAbsolutePath(), this.RESULTS_DIRECTORY_PATH);
}
if (!Files.exists(path)) {
String message = "Failed to create plot file " + this.plotFile.getAbsolutePath();
Expand Down Expand Up @@ -379,7 +379,7 @@ private void generateCSV(SolverHandler solverHandler) throws DataAccessException

private void generatePlots() throws PythonStreamException, InterruptedException, IOException {
logger.info("Generating Plots... ");
PythonCalls.genPlotsPseudoSedml(this.SEDML_LOCATION, this.OUTPUT_DIRECTORY_FOR_CURRENT_SEDML.toString()); // generate the plots
//PythonCalls.genPlotsPseudoSedml(this.SEDML_LOCATION, this.OUTPUT_DIRECTORY_FOR_CURRENT_SEDML.toString()); // generate the plots
// We assume if no exception is returned that the plots pass
for (Output output : this.sedml.getOutputs()){
if (!(output instanceof Plot2D plot)) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public static void setup() throws PythonStreamException, IOException {
PropertyLoader.setProperty(PropertyLoader.cliWorkingDir, new File("../vcell-cli-utils").getAbsolutePath());
VCMongoMessage.enabled = false;

CLIPythonManager.getInstance().instantiatePythonProcess();
omexTestCases = OmexTestingDatabase.loadOmexTestCases();
}

@AfterAll
public static void teardown() throws Exception {
CLIPythonManager.getInstance().closePythonProcess();
VCellUtilityHub.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public void test_singleExecOmex() throws Exception {
PropertyLoader.setProperty(PropertyLoader.cliWorkingDir, new File("../vcell-cli-utils").getAbsolutePath());
VCMongoMessage.enabled = false;
try {
CLIPythonManager.getInstance().instantiatePythonProcess();

InputStream omexInputStream = ExecuteImplTest.class.getResourceAsStream("/BioModel1.omex");
File tempOutputDir = Files.createTempDirectory("ExecuteImplTest_temp").toFile();
Expand All @@ -46,7 +45,6 @@ public void test_singleExecOmex() throws Exception {
tempOmexFile.delete();

} finally {
CLIPythonManager.getInstance().closePythonProcess();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public static void setup() throws PythonStreamException, IOException {
PropertyLoader.setProperty(PropertyLoader.cliWorkingDir, new File("../vcell-cli-utils").getAbsolutePath());
VCMongoMessage.enabled = false;

CLIPythonManager.getInstance().instantiatePythonProcess();
}

@AfterAll
public static void teardown() throws Exception {
CLIPythonManager.getInstance().closePythonProcess();
VCellUtilityHub.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,10 @@ public static void setup() throws PythonStreamException, IOException {
config.updateLoggers();
config.getConfiguration().getLoggerConfig(LogManager.getLogger("io.jhdf").getName()).setLevel(Level.WARN);
config.updateLoggers();

CLIPythonManager.getInstance().instantiatePythonProcess();
}

@AfterAll
public static void teardown() throws Exception {
CLIPythonManager.getInstance().closePythonProcess();
VCellUtilityHub.shutdown();
}

Expand Down

0 comments on commit d1de15a

Please sign in to comment.