Search in sources :

Example 6 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams 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 7 with ProcessExecutorParams

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

the class AppleCoreSimulatorServiceController method getCoreSimulatorServicePath.

/**
   * Returns the path on disk to the running Core Simulator service, if any.
   * Returns {@code Optional.empty()} unless exactly one Core Simulator service is running.
   */
public Optional<Path> getCoreSimulatorServicePath(UserIdFetcher userIdFetcher) throws IOException, InterruptedException {
    ImmutableSet<String> coreSimulatorServiceNames = getMatchingServiceNames(CORE_SIMULATOR_SERVICE_PATTERN);
    if (coreSimulatorServiceNames.size() != 1) {
        LOG.debug("Could not get core simulator service name (got %s)", coreSimulatorServiceNames);
        return Optional.empty();
    }
    String coreSimulatorServiceName = Iterables.getOnlyElement(coreSimulatorServiceNames);
    ImmutableList<String> launchctlPrintCommand = ImmutableList.of("launchctl", "print", String.format("user/%d/%s", userIdFetcher.getUserId(), coreSimulatorServiceName));
    LOG.debug("Getting status of service %s with %s", coreSimulatorServiceName, launchctlPrintCommand);
    ProcessExecutorParams launchctlPrintParams = ProcessExecutorParams.builder().setCommand(launchctlPrintCommand).build();
    ProcessExecutor.LaunchedProcess launchctlPrintProcess = processExecutor.launchProcess(launchctlPrintParams);
    Optional<Path> result = Optional.empty();
    try (InputStreamReader stdoutReader = new InputStreamReader(launchctlPrintProcess.getInputStream(), StandardCharsets.UTF_8);
        BufferedReader bufferedStdoutReader = new BufferedReader(stdoutReader)) {
        String line;
        while ((line = bufferedStdoutReader.readLine()) != null) {
            Matcher matcher = LAUNCHCTL_PRINT_PATH_PATTERN.matcher(line);
            if (matcher.matches()) {
                String path = matcher.group(1);
                LOG.debug("Found path of service %s: %s", coreSimulatorServiceName, path);
                result = Optional.of(Paths.get(path));
                break;
            }
        }
    } finally {
        processExecutor.destroyLaunchedProcess(launchctlPrintProcess);
        processExecutor.waitForLaunchedProcess(launchctlPrintProcess);
    }
    return result;
}
Also used : Path(java.nio.file.Path) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 8 with ProcessExecutorParams

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

the class AppleCoreSimulatorServiceController method getMatchingServiceNames.

private ImmutableSet<String> getMatchingServiceNames(Pattern serviceNamePattern) throws IOException, InterruptedException {
    ImmutableList<String> launchctlListCommand = ImmutableList.of("launchctl", "list");
    LOG.debug("Getting list of services with %s", launchctlListCommand);
    ProcessExecutorParams launchctlListParams = ProcessExecutorParams.builder().setCommand(launchctlListCommand).build();
    ProcessExecutor.LaunchedProcess launchctlListProcess = processExecutor.launchProcess(launchctlListParams);
    ImmutableSet.Builder<String> resultBuilder = ImmutableSet.builder();
    try (InputStreamReader stdoutReader = new InputStreamReader(launchctlListProcess.getInputStream(), StandardCharsets.UTF_8);
        BufferedReader bufferedStdoutReader = new BufferedReader(stdoutReader)) {
        String line;
        while ((line = bufferedStdoutReader.readLine()) != null) {
            Matcher launchctlListOutputMatcher = LAUNCHCTL_LIST_OUTPUT_PATTERN.matcher(line);
            if (launchctlListOutputMatcher.matches()) {
                String serviceName = launchctlListOutputMatcher.group(3);
                Matcher serviceNameMatcher = serviceNamePattern.matcher(serviceName);
                if (serviceNameMatcher.find()) {
                    LOG.debug("Found matching service name: %s", serviceName);
                    resultBuilder.add(serviceName);
                }
            }
        }
    } finally {
        processExecutor.destroyLaunchedProcess(launchctlListProcess);
        processExecutor.waitForLaunchedProcess(launchctlListProcess);
    }
    return resultBuilder.build();
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ImmutableSet(com.google.common.collect.ImmutableSet) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 9 with ProcessExecutorParams

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

the class AppleSimulatorController method launchSimulatorWithUdid.

private boolean launchSimulatorWithUdid(Path iosSimulatorPath, String simulatorUdid) throws IOException, InterruptedException {
    ImmutableList<String> command = ImmutableList.of("open", "-a", iosSimulatorPath.toString(), "--args", "-CurrentDeviceUDID", simulatorUdid);
    LOG.debug("Launching iOS simulator %s: %s", simulatorUdid, command);
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(command).build();
    ProcessExecutor.Result result = processExecutor.launchAndExecute(processExecutorParams);
    if (result.getExitCode() != 0) {
        LOG.error(result.getMessageForUnexpectedResult(command.toString()));
        return false;
    }
    return true;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 10 with ProcessExecutorParams

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

the class AppleSimulatorController method launchInstalledBundleInSimulator.

/**
   * Launches a previously-installed bundle in a started simulator.
   *
   * @return the process ID of the newly-launched process if successful,
   * an absent value otherwise.
   */
public Optional<Long> launchInstalledBundleInSimulator(String simulatorUdid, String bundleID, LaunchBehavior launchBehavior, List<String> args) throws IOException, InterruptedException {
    ImmutableList.Builder<String> commandBuilder = ImmutableList.builder();
    commandBuilder.add(simctlPath.toString(), "launch");
    if (launchBehavior == LaunchBehavior.WAIT_FOR_DEBUGGER) {
        commandBuilder.add("-w");
    }
    commandBuilder.add(simulatorUdid, bundleID);
    commandBuilder.addAll(args);
    ImmutableList<String> command = commandBuilder.build();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(command).build();
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);
    String message = String.format("Launching bundle ID %s in simulator %s via command %s", bundleID, simulatorUdid, command);
    LOG.debug(message);
    ProcessExecutor.Result result = processExecutor.launchAndExecute(processExecutorParams, options, /* stdin */
    Optional.empty(), /* timeOutMs */
    Optional.empty(), /* timeOutHandler */
    Optional.empty());
    if (result.getExitCode() != 0) {
        LOG.error(result.getMessageForResult(message));
        return Optional.empty();
    }
    Preconditions.checkState(result.getStdout().isPresent());
    String trimmedStdout = result.getStdout().get().trim();
    Matcher stdoutMatcher = SIMCTL_LAUNCH_OUTPUT_PATTERN.matcher(trimmedStdout);
    if (!stdoutMatcher.find()) {
        LOG.error("Could not parse output from %s: %s", command, trimmedStdout);
        return Optional.empty();
    }
    return Optional.of(Long.parseLong(stdoutMatcher.group(1), 10));
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

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