Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Commit

Permalink
added option to delete temp files if processing were successful (fixe…
Browse files Browse the repository at this point in the history
…s issue #46)
  • Loading branch information
Joel Håkansson committed Apr 24, 2013
1 parent 6a9340f commit dd5447a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Dotify/src/org/daisy/dotify/Dotify.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public class Dotify {
}

private final boolean writeTempFiles;
private final boolean keepTempFilesOnSuccess;
// hide default constructor to disable instantiation.
private Dotify(Map<String, String> params) {
// get parameters
writeTempFiles = "true".equals(params.get(SystemKeys.WRITE_TEMP_FILES));
keepTempFilesOnSuccess = !("false".equals(params.get(SystemKeys.KEEP_TEMP_FILES_ON_SUCCESS)));
}

/**
Expand Down Expand Up @@ -129,6 +131,7 @@ public static void run(File input, File output, String setup, FilterLocale conte

TaskRunner tr = new TaskRunner();
tr.setWriteTempFiles(d.writeTempFiles);
tr.setKeepTempFilesOnSuccess(d.keepTempFilesOnSuccess);
if (tempFilesDirectory!=null && !"".equals(tempFilesDirectory)) {
tr.setTempFilesFolder(new File(tempFilesDirectory));
}
Expand Down
7 changes: 6 additions & 1 deletion Dotify/src/org/daisy/dotify/SystemKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ public interface SystemKeys {
* Corresponding value should be the string "true" or "false"
*/
public final static String WRITE_TEMP_FILES = "writeTempFiles";
/**
* Defines a key for keeping temp files on success
* Corresponding value should be the string "true" or "false"
*/
public final static String KEEP_TEMP_FILES_ON_SUCCESS = "keepTempFilesOnSuccess";
/**
* Defines a key for the temp files directory.
* Corresponding value should be a string containing a file path
* Corresponding value should be a string containing a file path
*/
public final static String TEMP_FILES_DIRECTORY = "tempFilesDirectory";
}
24 changes: 22 additions & 2 deletions Dotify/src/org/daisy/dotify/system/TaskRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
Expand All @@ -29,12 +30,14 @@ public class TaskRunner {
private final Logger logger;
private File tempFilesFolder;
private boolean writeTempFiles;
private boolean keepTempFilesOnSuccess;
private String identifier;

public TaskRunner() {
this.logger = Logger.getLogger(this.getClass().getCanonicalName());
this.tempFilesFolder = new File(TEMP_DIR);
this.writeTempFiles = false;
this.keepTempFilesOnSuccess = false;
this.identifier = "" + System.currentTimeMillis();
}

Expand All @@ -46,6 +49,13 @@ public void setWriteTempFiles(boolean writeTempFiles) {
this.writeTempFiles = writeTempFiles;
}

public boolean isKeepTempFilesOnSuccess() {
return keepTempFilesOnSuccess;
}

public void setKeepTempFilesOnSuccess(boolean keepTempFilesOnSuccess) {
this.keepTempFilesOnSuccess = keepTempFilesOnSuccess;
}

public File getTempFilesFolder() {
return tempFilesFolder;
Expand All @@ -72,12 +82,13 @@ public void runTasks(File input, File output, TaskSystem taskSystem, Map<String,
int i = 0;
NumberFormat nf = NumberFormat.getPercentInstance();
FileJuggler fj = new FileJuggler(input, output);
ArrayList<File> tempFiles = new ArrayList<File>();
for (InternalTask task : tasks) {
if (task instanceof ReadWriteTask) {
logger.info("Running (r/w) " + task.getName());
((ReadWriteTask)task).execute(fj.getInput(), fj.getOutput());
if (writeTempFiles) {
writeTempFile(fj.getOutput(), taskSystem.getName(), task.getName(), i);
tempFiles.add(writeTempFile(fj.getOutput(), taskSystem.getName(), task.getName(), i));
}
fj.swap();
} else if (task instanceof ReadOnlyTask) {
Expand All @@ -92,10 +103,18 @@ public void runTasks(File input, File output, TaskSystem taskSystem, Map<String,
//progress(i/tasks.size());
}
fj.close();
if (!keepTempFilesOnSuccess) {
// Process were successful, delete temp files
for (File f : tempFiles) {
if (!f.delete()) {
f.deleteOnExit();
}
}
}
logger.info("\"" + taskSystem.getName() + "\" finished in " + Math.round(progress.timeSinceStart()/100d)/10d + " s");
}

private void writeTempFile(File source, String taskSystemName, String taskName, int i) throws IOException {
private File writeTempFile(File source, String taskSystemName, String taskName, int i) throws IOException {
String it = ""+(i+1);
while (it.length()<3) {
it = "0" + it;
Expand All @@ -109,6 +128,7 @@ private void writeTempFile(File source, String taskSystemName, String taskName,
File f = new File(tempFilesFolder, fileName);
logger.fine("Writing debug file: " + f);
FileUtils.copyFile(source, f);
return f;
}

private String truncate(String str, int pos) {
Expand Down

0 comments on commit dd5447a

Please sign in to comment.