use of com.facebook.buck.util.ListeningProcessExecutor in project buck by facebook.
the class SwiftStdlibStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
ListeningProcessExecutor executor = new ListeningProcessExecutor();
ProcessExecutorParams params = makeProcessExecutorParams();
SimpleProcessListener listener = new SimpleProcessListener();
// TODO(ryu2): parse output as needed
try {
LOG.debug("%s", command);
ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener);
int result = executor.waitForProcess(process);
if (result != 0) {
LOG.error("Error running %s: %s", getDescription(context), listener.getStderr());
return StepExecutionResult.of(result);
}
} catch (IOException e) {
LOG.error(e, "Could not execute command %s", command);
return StepExecutionResult.ERROR;
}
// Copy from temp to destinationDirectory if we wrote files.
if (Files.notExists(temp)) {
return StepExecutionResult.SUCCESS;
}
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(temp)) {
if (dirStream.iterator().hasNext()) {
Files.createDirectories(destinationDirectory);
MoreFiles.copyRecursively(temp, destinationDirectory);
}
} catch (IOException e) {
LOG.error(e, "Could not copy to %s", destinationDirectory);
return StepExecutionResult.ERROR;
}
return StepExecutionResult.SUCCESS;
}
use of com.facebook.buck.util.ListeningProcessExecutor in project buck by facebook.
the class Watchman method execute.
@SuppressWarnings("unchecked")
private static Optional<Map<String, Object>> execute(ListeningProcessExecutor executor, Console console, Clock clock, long commandTimeoutMillis, long timeoutNanos, Path watchmanPath, String... args) throws InterruptedException, IOException {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
ForwardingProcessListener listener = new ForwardingProcessListener(Channels.newChannel(stdout), Channels.newChannel(stderr));
ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(ProcessExecutorParams.builder().addCommand(watchmanPath.toString(), "--output-encoding=bser").addCommand(args).build(), listener);
long startTimeNanos = clock.nanoTime();
int exitCode = executor.waitForProcess(process, Math.min(timeoutNanos, POLL_TIME_NANOS), TimeUnit.NANOSECONDS);
if (exitCode == Integer.MIN_VALUE) {
// Let the user know we're still here waiting for Watchman, then wait the
// rest of the timeout period.
long remainingNanos = timeoutNanos - (clock.nanoTime() - startTimeNanos);
if (remainingNanos > 0) {
console.getStdErr().getRawStream().format("Waiting for Watchman command [%s]...\n", Joiner.on(" ").join(args));
exitCode = executor.waitForProcess(process, remainingNanos, TimeUnit.NANOSECONDS);
}
}
LOG.debug("Waited %d ms for Watchman command %s, exit code %d", TimeUnit.NANOSECONDS.toMillis(clock.nanoTime() - startTimeNanos), Joiner.on(" ").join(args), exitCode);
if (exitCode == Integer.MIN_VALUE) {
LOG.warn("Watchman did not respond within %d ms, disabling.", commandTimeoutMillis);
console.getStdErr().getRawStream().format("Timed out after %d ms waiting for Watchman command [%s]. Disabling Watchman.\n", commandTimeoutMillis, Joiner.on(" ").join(args));
return Optional.empty();
}
if (exitCode != 0) {
LOG.debug("Watchman's stderr: %s", new String(stderr.toByteArray(), Charsets.UTF_8));
LOG.error("Error %d executing %s", exitCode, Joiner.on(" ").join(args));
return Optional.empty();
}
Object response = new BserDeserializer(BserDeserializer.KeyOrdering.UNSORTED).deserializeBserValue(new ByteArrayInputStream(stdout.toByteArray()));
LOG.debug("stdout of command: " + response);
if (!(response instanceof Map<?, ?>)) {
LOG.error("Unexpected response from Watchman: %s", response);
return Optional.empty();
}
return Optional.of((Map<String, Object>) response);
}
use of com.facebook.buck.util.ListeningProcessExecutor in project buck by facebook.
the class SwiftCompileStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
ListeningProcessExecutor executor = new ListeningProcessExecutor();
ProcessExecutorParams params = makeProcessExecutorParams();
SimpleProcessListener listener = new SimpleProcessListener();
// TODO(ryu2): parse the output, print build failure errors, etc.
try {
LOG.debug("%s", compilerCommand);
ListeningProcessExecutor.LaunchedProcess process = executor.launchProcess(params, listener);
int result = executor.waitForProcess(process);
if (result != 0) {
LOG.error("Error running %s: %s", getDescription(context), listener.getStderr());
}
return StepExecutionResult.of(result);
} catch (IOException e) {
LOG.error(e, "Could not execute command %s", compilerCommand);
return StepExecutionResult.ERROR;
}
}
use of com.facebook.buck.util.ListeningProcessExecutor in project buck by facebook.
the class MacNetworkConfiguration method runNetworkSetupCommand.
/**
* Naive `networksetup` invocation; returns non-empty string of stdout if all went well.
*/
private static String runNetworkSetupCommand(String subCommand, String argument) throws InterruptedException {
ListeningProcessExecutor executor = new ListeningProcessExecutor();
SimpleProcessListener listener = new SimpleProcessListener();
ProcessExecutorParams params = ProcessExecutorParams.builder().addCommand("networksetup").addCommand(String.format("-%s", subCommand)).addCommand(argument).build();
ListeningProcessExecutor.LaunchedProcess process = null;
try {
process = executor.launchProcess(params, listener);
if (executor.waitForProcess(process, COMMAND_TIMEOUT_MS, TimeUnit.MILLISECONDS) != 0) {
return "";
}
return listener.getStdout();
} catch (IOException e) {
LOG.debug(e, "Exception while running networksetup command");
return "";
} finally {
if (process != null) {
executor.destroyProcess(process, /* force */
true);
}
}
}
use of com.facebook.buck.util.ListeningProcessExecutor in project buck by facebook.
the class ProjectCommand method runPreprocessScriptIfNeeded.
private int runPreprocessScriptIfNeeded(CommandRunnerParams params) throws IOException, InterruptedException {
Optional<String> pathToPreProcessScript = getPathToPreProcessScript(params.getBuckConfig());
if (!pathToPreProcessScript.isPresent()) {
return 0;
}
String pathToScript = pathToPreProcessScript.get();
if (!Paths.get(pathToScript).isAbsolute()) {
pathToScript = params.getCell().getFilesystem().getPathForRelativePath(pathToScript).toAbsolutePath().toString();
}
ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addCommand(pathToScript).setEnvironment(ImmutableMap.<String, String>builder().putAll(params.getEnvironment()).put("BUCK_PROJECT_TARGETS", Joiner.on(" ").join(getArguments())).build()).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
ForwardingProcessListener processListener = new ForwardingProcessListener(// because this process finishes before we start parsing process.
Channels.newChannel(params.getConsole().getStdOut().getRawStream()), Channels.newChannel(params.getConsole().getStdErr().getRawStream()));
ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
try {
return processExecutor.waitForProcess(process);
} finally {
processExecutor.destroyProcess(process, /* force */
false);
processExecutor.waitForProcess(process);
}
}
Aggregations