Search in sources :

Example 1 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class XctoolRunTestsStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
    ImmutableMap<String, String> env = getEnv(context);
    ProcessExecutorParams.Builder processExecutorParamsBuilder = ProcessExecutorParams.builder().addAllCommand(command).setDirectory(filesystem.getRootPath().toAbsolutePath()).setRedirectOutput(ProcessBuilder.Redirect.PIPE).setEnvironment(env);
    if (!testSelectorList.isEmpty()) {
        try {
            ImmutableList.Builder<String> xctoolFilterParamsBuilder = ImmutableList.builder();
            int returnCode = listAndFilterTestsThenFormatXctoolParams(context.getProcessExecutor(), context.getConsole(), testSelectorList, // Copy the entire xctool command and environment but add a -listTestsOnly arg.
            ProcessExecutorParams.builder().from(processExecutorParamsBuilder.build()).addCommand("-listTestsOnly").build(), xctoolFilterParamsBuilder);
            if (returnCode != 0) {
                context.getConsole().printErrorText("Failed to query tests with xctool");
                return StepExecutionResult.of(returnCode);
            }
            ImmutableList<String> xctoolFilterParams = xctoolFilterParamsBuilder.build();
            if (xctoolFilterParams.isEmpty()) {
                context.getConsole().printBuildFailure(String.format(Locale.US, "No tests found matching specified filter (%s)", testSelectorList.getExplanation()));
                return StepExecutionResult.SUCCESS;
            }
            processExecutorParamsBuilder.addAllCommand(xctoolFilterParams);
        } catch (IOException e) {
            context.getConsole().printErrorText("Failed to get list of tests from test bundle");
            context.getConsole().printBuildFailureWithStacktrace(e);
            return StepExecutionResult.ERROR;
        }
    }
    ProcessExecutorParams processExecutorParams = processExecutorParamsBuilder.build();
    // Only launch one instance of xctool at the time
    final AtomicBoolean stutterLockIsNotified = new AtomicBoolean(false);
    try {
        LOG.debug("Running command: %s", processExecutorParams);
        try {
            acquireStutterLock(stutterLockIsNotified);
            // Start the process.
            ProcessExecutor.LaunchedProcess launchedProcess = context.getProcessExecutor().launchProcess(processExecutorParams);
            int exitCode = -1;
            String stderr = "Unexpected termination";
            try {
                ProcessStdoutReader stdoutReader = new ProcessStdoutReader(launchedProcess);
                ProcessStderrReader stderrReader = new ProcessStderrReader(launchedProcess);
                Thread stdoutReaderThread = new Thread(stdoutReader);
                Thread stderrReaderThread = new Thread(stderrReader);
                stdoutReaderThread.start();
                stderrReaderThread.start();
                exitCode = waitForProcessAndGetExitCode(context.getProcessExecutor(), launchedProcess, timeoutInMs);
                stdoutReaderThread.join(timeoutInMs.orElse(1000L));
                stderrReaderThread.join(timeoutInMs.orElse(1000L));
                Optional<IOException> exception = stdoutReader.getException();
                if (exception.isPresent()) {
                    throw exception.get();
                }
                stderr = stderrReader.getStdErr();
                LOG.debug("Finished running command, exit code %d, stderr %s", exitCode, stderr);
            } finally {
                context.getProcessExecutor().destroyLaunchedProcess(launchedProcess);
                context.getProcessExecutor().waitForLaunchedProcess(launchedProcess);
            }
            if (exitCode != 0) {
                if (!stderr.isEmpty()) {
                    context.getConsole().printErrorText(String.format(Locale.US, "xctool failed with exit code %d: %s", exitCode, stderr));
                } else {
                    context.getConsole().printErrorText(String.format(Locale.US, "xctool failed with exit code %d", exitCode));
                }
            }
            return StepExecutionResult.of(exitCode);
        } catch (Exception e) {
            LOG.error(e, "Exception while running %s", processExecutorParams.getCommand());
            MoreThrowables.propagateIfInterrupt(e);
            context.getConsole().printBuildFailureWithStacktrace(e);
            return StepExecutionResult.ERROR;
        }
    } finally {
        releaseStutterLock(stutterLockIsNotified);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 2 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class AppleDeviceHelper method installBundleOnDevice.

/**
   * Attempts to install a bundle on the device.  The bundle must be code-signed already.
   *
   * @param udid        The identifier of the device
   * @param bundlePath  The path to the bundle root (e.g. {@code /path/to/Example.app/})
   * @return            true if successful, false otherwise.
   */
public boolean installBundleOnDevice(String udid, Path bundlePath) {
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of(deviceHelperPath.toString(), "--json", udid, "install", bundlePath.toString())).build();
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.PRINT_STD_OUT, ProcessExecutor.Option.PRINT_STD_ERR);
    ProcessExecutor.Result result;
    try {
        result = processExecutor.launchAndExecute(processExecutorParams, options, /* stdin */
        Optional.empty(), /* timeOutMs */
        Optional.of((long) 60000), /* timeOutHandler */
        Optional.empty());
    } catch (InterruptedException | IOException e) {
        LOG.warn("Could not execute device helper.");
        return false;
    }
    if (result.isTimedOut()) {
        throw new RuntimeException("Device helper failed: timed out");
    }
    return (result.getExitCode() == 0);
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 3 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class AppleDeviceHelper method runBundleOnDevice.

/**
   * Attempts to run a bundle on the device.  The bundle must be installed already.
   *
   * @param udid        The identifier of the device
   * @param bundleID    The bundle ID (e.g. {@code com.example.DemoApp})
   * @return            true if successful, false otherwise.
   */
public boolean runBundleOnDevice(String udid, String bundleID) {
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of(deviceHelperPath.toString(), "--json", udid, "launch", bundleID)).build();
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.PRINT_STD_OUT, ProcessExecutor.Option.PRINT_STD_ERR);
    ProcessExecutor.Result result;
    try {
        result = processExecutor.launchAndExecute(processExecutorParams, options, /* stdin */
        Optional.empty(), /* timeOutMs */
        Optional.of((long) 60000), /* timeOutHandler */
        Optional.empty());
    } catch (InterruptedException | IOException e) {
        LOG.warn("Could not execute device helper.");
        return false;
    }
    if (result.isTimedOut()) {
        throw new RuntimeException("Device helper failed: timed out");
    }
    return (result.getExitCode() == 0);
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 4 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class CodeSigning method hasValidSignature.

/**
   * Checks whether a binary or bundle already has a valid code signature.
   *
   * @param path Resolved path to the binary or bundle.
   * @return Whether the binary or bundle has a valid code signature.
   */
public static boolean hasValidSignature(ProcessExecutor processExecutor, Path path) throws InterruptedException, IOException {
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of("codesign", "--verify", "-v", path.toString())).build();
    // Specify that stdout is expected, or else output may be wrapped in Ansi escape chars.
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT, ProcessExecutor.Option.IS_SILENT);
    ProcessExecutor.Result result = processExecutor.launchAndExecute(processExecutorParams, options, /* stdin */
    Optional.empty(), /* timeOutMs */
    Optional.empty(), /* timeOutHandler */
    Optional.empty());
    return result.getExitCode() == 0 && result.getStderr().isPresent() && result.getStderr().get().contains(": satisfies its Designated Requirement");
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 5 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class RegisterDebugSymbolsStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
    ImmutableList<String> lldbCommandPrefix = lldb.getCommandPrefix(resolver);
    ProcessExecutorParams params = ProcessExecutorParams.builder().addCommand(lldbCommandPrefix.toArray(new String[lldbCommandPrefix.size()])).build();
    return StepExecutionResult.of(context.getProcessExecutor().launchAndExecute(params, ImmutableSet.of(), Optional.of(String.format("target create %s\ntarget symbols add %s", resolver.getAbsolutePath(binary), dsymPath)), Optional.empty(), Optional.empty()));
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams)

Aggregations

ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)72 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)32 FakeProcess (com.facebook.buck.util.FakeProcess)30 Test (org.junit.Test)30 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)21 ExecutionContext (com.facebook.buck.step.ExecutionContext)19 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)19 IOException (java.io.IOException)18 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)12 TestConsole (com.facebook.buck.testutil.TestConsole)12 Path (java.nio.file.Path)11 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)8 ListeningProcessExecutor (com.facebook.buck.util.ListeningProcessExecutor)6 HumanReadableException (com.facebook.buck.util.HumanReadableException)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 Matcher (java.util.regex.Matcher)4