Search in sources :

Example 1 with ListeningProcessExecutor

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;
}
Also used : Path(java.nio.file.Path) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) SimpleProcessListener(com.facebook.buck.util.SimpleProcessListener) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) IOException(java.io.IOException)

Example 2 with ListeningProcessExecutor

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) BserDeserializer(com.facebook.buck.bser.BserDeserializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with ListeningProcessExecutor

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;
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) SimpleProcessListener(com.facebook.buck.util.SimpleProcessListener) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) IOException(java.io.IOException)

Example 4 with ListeningProcessExecutor

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);
        }
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) SimpleProcessListener(com.facebook.buck.util.SimpleProcessListener) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) IOException(java.io.IOException)

Example 5 with ListeningProcessExecutor

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);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener)

Aggregations

ListeningProcessExecutor (com.facebook.buck.util.ListeningProcessExecutor)7 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)6 ForwardingProcessListener (com.facebook.buck.util.ForwardingProcessListener)4 SimpleProcessListener (com.facebook.buck.util.SimpleProcessListener)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)2 BserDeserializer (com.facebook.buck.bser.BserDeserializer)1 Build (com.facebook.buck.command.Build)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)1 BinaryBuildRule (com.facebook.buck.rules.BinaryBuildRule)1 BuildRule (com.facebook.buck.rules.BuildRule)1 ExternalTestRunnerRule (com.facebook.buck.rules.ExternalTestRunnerRule)1 ExternalTestRunnerTestSpec (com.facebook.buck.rules.ExternalTestRunnerTestSpec)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 TestRule (com.facebook.buck.rules.TestRule)1 Tool (com.facebook.buck.rules.Tool)1 TestRunningOptions (com.facebook.buck.test.TestRunningOptions)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1