Skip to content

Commit

Permalink
Extension to the temp directory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeByDrescher committed Oct 30, 2024
1 parent a8c4621 commit 2205d86
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import org.vcell.cli.CLIRecorder;
import org.vcell.cli.run.ExecuteImpl;
import org.vcell.cli.trace.Tracer;
import org.vcell.util.FileUtils;
import org.vcell.util.exe.Executable;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.io.File;
import java.nio.file.Files;
import java.util.Date;
import java.util.concurrent.Callable;

@Command(name = "biosimulations", description = "BioSimulators-compliant command-line interface to the vcell simulation program <https://vcell.org>.")
Expand Down Expand Up @@ -90,18 +93,17 @@ 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, OUT_DIR, cliRecorder, true);
if (Tracer.hasErrors()){
if (!bQuiet) {
logger.error("Errors occurred during execution");
Tracer.reportErrors(bDebug);
}
return 1;
} else {
return 0;
ExecuteImpl.singleMode(ARCHIVE, tmpDir, cliRecorder, true);
FileUtils.copyDirectoryContents(tmpDir, OUT_DIR, true, null);
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
Expand Down
30 changes: 15 additions & 15 deletions vcell-cli/src/main/java/org/vcell/cli/run/RunUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,22 +493,22 @@ public static void drawBreakLine(String breakString, int times) {


public static boolean removeAndMakeDirs(File f) {
boolean removalSuccess = true;
if (f.exists()) {
boolean isRemoved = CLIUtils.removeDirs(f);
if (!isRemoved)
removalSuccess = false;
// we don't throw an exception up the stack here for two reasons:
// 1) deletion of files differs on different OS. Punishing users for their OS or their understanding thereof seems wrong.
// 2) the user may be using software that is necessarily holding resources; by blocking on failed deletion, we are
// denying the potential use cases of VCell.
try{
throw new Exception("File '" + f.getCanonicalPath() + "' could not be deleted!");
} catch (Exception e){
logger.error(e);
}
if (!f.exists()) return f.mkdirs();
String filePath = "";
try {
filePath += f.getCanonicalPath();
org.vcell.util.FileUtils.deleteDirectoryContents(f, true, null);
} catch (IOException e) {
// if we got to here, we've tried to clear the contents of a directory a failed.
// we don't throw an exception up the stack here for two reasons:
// 1) deletion of files differs on different OS. Punishing users for their OS or their understanding thereof seems wrong.
// 2) the user may be using software that is necessarily holding resources; by blocking on failed deletion, we are
// denying the potential use cases of VCell.
String msg = "File '" + filePath + "' could not be deleted!";
logger.error(msg, e);
return false;
}
return f.mkdirs() && removalSuccess;
return true;
}

public static void createCSVFromODEResultSet(ODESolverResultSet resultSet, File f) throws ExpressionException {
Expand Down
79 changes: 49 additions & 30 deletions vcell-util/src/main/java/org/vcell/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -44,7 +45,7 @@ public final class FileUtils {
* @author Logan Drescher
*/
public static void copyDirectoryContents(File sourceDir, File destDir, boolean performRecursive, FileFilter filter) throws IOException {
if (!(sourceDir.isDirectory())){
if (!sourceDir.isDirectory()){
throw new IOException("Arguments for FileUtils.copyDirectory must be directories. Problem with Source dir: "+sourceDir.getCanonicalPath());
}
if ((destDir.exists()) && !(destDir.isDirectory())) {
Expand All @@ -70,7 +71,40 @@ public static void copyDirectoryContents(File sourceDir, File destDir, boolean p
}
}
}
}
}

/**
* Convenience method to copy a directory from a source to a destination.
* Overwrite is allowed, and the last modified is kept, 4k buffer used.
* @param targetDir the directory to delete the files inside of
* @param performRecursive whether to recursively copy directory contents, or keep surface level
* @param filter FileFilter settings to selectively filter types of files
* @throws IOException If the files are not the correct types, or an IO problem was encountered
* @author Ed Boyce
* @author Logan Drescher
*/
public static void deleteDirectoryContents(File targetDir, boolean performRecursive, FileFilter filter) throws IOException {
if (!targetDir.isDirectory()){
throw new IOException("Argument for parameter `targetDir`" + targetDir.getCanonicalPath() + " is not a directory");
}

// We need all 3 to delete!
if (!targetDir.canWrite() || !targetDir.canExecute() || !targetDir.canRead()) {
throw new IOException("Cannot write to directory `" + targetDir.getCanonicalPath() + "`");
}

for (File currentFile : targetDir.listFiles()) { // Will not return a null because we already checked for directory!
if (filter != null && !filter.accept(currentFile)) continue;
if (currentFile.isDirectory() && performRecursive) {
deleteDirectoryContents(currentFile, true, filter);
if (!currentFile.delete()) throw new IOException("Failed to delete empty directory " + currentFile.getCanonicalPath());
continue;
}
if (currentFile.isFile()) {
deleteFile(currentFile);
}
}
}

/**
* Convenience method to copy a file from a source to a destination.
Expand Down Expand Up @@ -243,49 +277,34 @@ public static void saveUrlToFile(File file, String urlString) throws MalformedUR
}
}

public static void deleteFile(File f) throws IOException {
if (!f.exists()) throw new IOException("File \"" + f.getCanonicalPath() + "\" does not exist.");
if (!f.canWrite()) throw new IOException("File \"" + f.getCanonicalPath() + "\" is write protected.");
if (f.isDirectory()) throw new IOException("File \"" + f.getCanonicalPath() + "\" is a directory, not a single file.");
if (!f.delete()) throw new IOException("File \"" + f.getCanonicalPath() + "\" deletion attempt failed.");
}

public static void deleteFile(String filePath) throws IOException {
File f = new File(filePath);
if (!f.exists()) {
throw new IOException("File \""+filePath+"\" does not exist.");
}
if (!f.canWrite()) {
throw new IOException("File \""+filePath+"\" is write protected.");
}
if (f.isDirectory()) {
throw new IOException("File \""+filePath+"\" is a directory, and I'm currently programmed to balk at deleting whole directories.");
}
boolean bSuccess = f.delete();
if (!bSuccess) {
throw new IOException("File \""+filePath+"\" deletion attempt failed.");
}
deleteFile(new File(filePath));
}

public static void writeByteArrayToFile(byte[] byteArray, File outputFile) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
try {
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
byteBuffer.put(byteArray);
byteBuffer.flip();
FileChannel fileChannel = fileOutputStream.getChannel();
fileChannel.write(byteBuffer);
} finally {
fileOutputStream.close();
}
}


public static byte[] readByteArrayFromFile(File inputFile)throws IOException {
byte [] inputFileByteArrayBuffer = new byte[(int) inputFile.length()];
InputStream inputStream = null;
try {
inputStream = new FileInputStream(inputFile);
if ( inputStream.read(inputFileByteArrayBuffer) == -1 ) {
throw new IOException("EOF character reached while trying to read the whole file:"+inputFile.getAbsolutePath());
}
} finally {
if ( inputStream != null ) {
inputStream.close();
}
try (InputStream inputStream = new FileInputStream(inputFile)) {
if (inputStream.read(inputFileByteArrayBuffer) == -1) {
throw new IOException("EOF character reached while trying to read the whole file:" + inputFile.getAbsolutePath());
}
}
return inputFileByteArrayBuffer;
}
Expand Down

0 comments on commit 2205d86

Please sign in to comment.