Move execAndCheck() to ProcessController

This commit is contained in:
Joel Thibault 2014-04-09 14:29:26 -04:00
parent b197618d13
commit ce770b032a
2 changed files with 22 additions and 20 deletions

View File

@ -29,6 +29,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.broadinstitute.sting.utils.Utils;
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;
import org.broadinstitute.sting.utils.exceptions.UserException;
@ -267,6 +268,25 @@ public class ProcessController {
return new ProcessOutput(exitCode, stdout, stderr);
}
/**
* Executes a command line program with the settings and waits for it to return,
* processing the output on a background thread.
*
* Throws an IOException if the ProcessOutput exit code is nonzero
*
* @param settings Settings to be run.
*/
public ProcessOutput execAndCheck(ProcessSettings settings) throws IOException {
ProcessOutput po = exec(settings);
if (po.getExitValue() != 0) {
String message = String.format("Process exited with %d\nCommand Line: %s",
po.getExitValue(),
Utils.join(" ", settings.getCommand()));
throw new IOException(message);
}
return po;
}
/**
* @return The set of still running processes.
*/

View File

@ -40,7 +40,6 @@ import org.broadinstitute.sting.BaseTest;
import org.broadinstitute.sting.MD5DB;
import org.broadinstitute.sting.MD5Mismatch;
import org.broadinstitute.sting.gatk.CommandLineGATK;
import org.broadinstitute.sting.utils.Utils;
import org.broadinstitute.sting.utils.runtime.*;
public class LoggingIntegrationTest {
@ -100,14 +99,14 @@ public class LoggingIntegrationTest {
// output argument
ProcessSettings ps = new ProcessSettings(cfg.getCmdLine(false).split("\\s+"));
execAndCheck(pc, ps);
pc.execAndCheck(ps);
String output_argument_md5 = md5db.calculateFileMD5(cfg.argumentOutputFile);
// pipe to stdout
ps = new ProcessSettings(cfg.getCmdLine(true).split("\\s+"));
ps.setStdoutSettings(new OutputStreamSettings(cfg.pipedOutputFile));
execAndCheck(pc, ps);
pc.execAndCheck(ps);
MD5DB.MD5Match result = md5db.testFileMD5("LoggingIntegrationTest", "LoggingIntegrationTest", cfg.pipedOutputFile, output_argument_md5, false);
if(result.failed) {
@ -115,21 +114,4 @@ public class LoggingIntegrationTest {
Assert.fail(failure.toString());
}
}
/**
* Execute a process, and throw an IOException if the exit code is nonzero
*
* @param pc
* @param ps
* @throws IOException
*/
private void execAndCheck(ProcessController pc, ProcessSettings ps) throws IOException {
ProcessOutput po = pc.exec(ps);
if (po.getExitValue() != 0) {
String message = String.format("Process exited with %d\nCommand Line: %s",
po.getExitValue(),
Utils.join(" ", ps.getCommand()));
throw new IOException(message);
}
}
}