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;
}
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;
}
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();
}
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;
}
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));
}
Aggregations