use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class AppleDeviceHelper method getConnectedDevices.
/**
* Runs the helper program to enumerate all currently-connected devices.
*
* @return A ImmutableMap with entries where the key is the UDID of the device, and the value
* is a human readable description (e.g. "iPhone (iPhone 5S) (USB)")
* @throws IOException
* @throws InterruptedException
*/
public ImmutableMap<String, String> getConnectedDevices() {
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of(deviceHelperPath.toString(), "list")).build();
// Must 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.Result result;
try {
result = processExecutor.launchAndExecute(processExecutorParams, options, /* stdin */
Optional.empty(), /* timeOutMs */
Optional.of((long) 5000), /* timeOutHandler */
Optional.empty());
} catch (InterruptedException | IOException e) {
LOG.warn("Could not execute device helper.");
return ImmutableMap.of();
}
if (result.isTimedOut()) {
throw new RuntimeException("Device helper failed: timed out");
}
if (result.getExitCode() != 0) {
throw new RuntimeException(result.getMessageForUnexpectedResult("Device helper"));
}
Matcher matcher = DEVICE_DESCRIPTION_PATTERN.matcher(result.getStdout().get());
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
while (matcher.find()) {
String udid = matcher.group(1);
String description = matcher.group(2);
builder.put(udid, description);
LOG.debug("Found device: " + udid + ", " + description);
}
return builder.build();
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class XctestRunTestsStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
ProcessExecutorParams.Builder builder = ProcessExecutorParams.builder().addAllCommand(getCommand()).setDirectory(filesystem.getRootPath().toAbsolutePath()).setRedirectErrorStream(true).setEnvironment(getEnv(context));
ProcessExecutorParams params = builder.build();
LOG.debug("xctest command: %s", Joiner.on(" ").join(params.getCommand()));
try {
ProcessExecutor executor = context.getProcessExecutor();
ProcessExecutor.LaunchedProcess launchedProcess = executor.launchProcess(params);
int exitCode;
try (OutputStream outputStream = filesystem.newFileOutputStream(outputPath);
TeeInputStream outputWrapperStream = new TeeInputStream(launchedProcess.getInputStream(), outputStream)) {
if (outputReadingCallback.isPresent()) {
// The caller is responsible for reading all the data, which TeeInputStream will
// copy to outputStream.
outputReadingCallback.get().readOutput(outputWrapperStream);
} else {
// Nobody's going to read from outputWrapperStream, so close it and copy
// the process's stdout and stderr to outputPath directly.
outputWrapperStream.close();
ByteStreams.copy(launchedProcess.getInputStream(), outputStream);
}
exitCode = executor.waitForLaunchedProcess(launchedProcess).getExitCode();
// There's no way to distinguish a test failure from an xctest issue. We don't
// want to fail the step on a test failure, so return 0 for any xctest exit code.
exitCode = 0;
LOG.debug("Finished running command, exit code %d", exitCode);
} finally {
context.getProcessExecutor().destroyLaunchedProcess(launchedProcess);
context.getProcessExecutor().waitForLaunchedProcess(launchedProcess);
}
if (exitCode != 0) {
context.getConsole().printErrorText(String.format(Locale.US, "xctest failed with exit code %d", exitCode));
}
return StepExecutionResult.of(exitCode);
} catch (IOException e) {
LOG.error(e, "Exception while running %s", params.getCommand());
MoreThrowables.propagateIfInterrupt(e);
context.getConsole().printBuildFailureWithStacktrace(e);
return StepExecutionResult.ERROR;
}
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class AppleCoreSimulatorServiceController method killService.
private boolean killService(String serviceName) throws IOException, InterruptedException {
ImmutableList<String> launchctlRemoveCommand = ImmutableList.of("launchctl", "remove", serviceName);
LOG.debug("Killing simulator process with with %s", launchctlRemoveCommand);
ProcessExecutorParams launchctlRemoveParams = ProcessExecutorParams.builder().setCommand(launchctlRemoveCommand).build();
ProcessExecutor.Result result = processExecutor.launchAndExecute(launchctlRemoveParams);
int launchctlExitCode = result.getExitCode();
LOG.debug("Command %s exited with code %d", launchctlRemoveParams, launchctlExitCode);
switch(launchctlExitCode) {
case LAUNCHCTL_EXIT_SUCCESS:
case LAUNCHCTL_EXIT_NO_SUCH_PROCESS:
// we told it to die, so we have to treat "no such process" as success.
return true;
default:
LOG.error(result.getMessageForUnexpectedResult(launchctlRemoveCommand.toString()));
return false;
}
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class AppleSimulatorController method installBundleInSimulator.
/**
* Installs a bundle in a previously-started simulator.
*
* @return true if the bundle was installed, false otherwise.
*/
public boolean installBundleInSimulator(String simulatorUdid, Path bundlePath) throws IOException, InterruptedException {
ImmutableList<String> command = ImmutableList.of(simctlPath.toString(), "install", simulatorUdid, bundlePath.toString());
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 AppleSimulatorDiscovery method discoverAppleSimulators.
/**
* Discovers information about Apple simulators installed on the system.
*/
public static ImmutableSet<AppleSimulator> discoverAppleSimulators(ProcessExecutor processExecutor, Path simctlPath) throws InterruptedException, IOException {
LOG.debug("Running xcrun simctl list to get list of simulators");
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of(simctlPath.toString(), "list")).build();
ProcessExecutor.LaunchedProcess simctlListProcess = processExecutor.launchProcess(processExecutorParams);
ImmutableSet.Builder<AppleSimulator> simulatorsBuilder = ImmutableSet.builder();
try (InputStreamReader stdoutReader = new InputStreamReader(simctlListProcess.getInputStream(), StandardCharsets.UTF_8)) {
LOG.debug("Parsing output of xcrun simctl list...");
SimctlListOutputParsing.parseOutputFromReader(stdoutReader, simulatorsBuilder);
}
ProcessExecutor.Result result = processExecutor.waitForLaunchedProcess(simctlListProcess);
if (result.getExitCode() != 0) {
throw new IOException(result.getMessageForUnexpectedResult("simctl list"));
}
ImmutableSet<AppleSimulator> simulators = simulatorsBuilder.build();
LOG.debug("Discovered simulators: %s", simulators);
return simulators;
}
Aggregations