Search in sources :

Example 46 with ProcessExecutorParams

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

the class TestCommand method runTestsExternal.

private int runTestsExternal(final CommandRunnerParams params, Build build, Iterable<String> command, Iterable<TestRule> testRules, SourcePathResolver pathResolver) throws InterruptedException, IOException {
    TestRunningOptions options = getTestRunningOptions(params);
    // Walk the test rules, collecting all the specs.
    List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList();
    for (TestRule testRule : testRules) {
        if (!(testRule instanceof ExternalTestRunnerRule)) {
            params.getBuckEventBus().post(ConsoleEvent.severe(String.format("Test %s does not support external test running", testRule.getBuildTarget())));
            return 1;
        }
        ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule;
        specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options, pathResolver));
    }
    // Serialize the specs to a file to pass into the test runner.
    Path infoFile = params.getCell().getFilesystem().resolve(params.getCell().getFilesystem().getBuckPaths().getScratchDir()).resolve("external_runner_specs.json");
    Files.createDirectories(infoFile.getParent());
    Files.deleteIfExists(infoFile);
    params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs);
    // Launch and run the external test runner, forwarding it's stdout/stderr to the console.
    // We wait for it to complete then returns its error code.
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(command).addAllCommand(withDashArguments).setEnvironment(params.getEnvironment()).addCommand("--buck-test-info", infoFile.toString()).addCommand("--jobs", String.valueOf(getConcurrencyLimit(params.getBuckConfig()).threadLimit)).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : Path(java.nio.file.Path) TestRule(com.facebook.buck.rules.TestRule) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ExternalTestRunnerRule(com.facebook.buck.rules.ExternalTestRunnerRule) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) TestRunningOptions(com.facebook.buck.test.TestRunningOptions) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec)

Example 47 with ProcessExecutorParams

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

the class CxxPreprocessAndCompileStep method executeCompilation.

private int executeCompilation(ExecutionContext context) throws Exception {
    ProcessExecutorParams.Builder builder = makeSubprocessBuilder(context, ImmutableMap.of());
    if (useArgfile) {
        filesystem.writeLinesToPath(Iterables.transform(getArguments(context.getAnsi().isAnsiTerminal()), Escaper.ARGFILE_ESCAPER), getArgfile());
        builder.setCommand(ImmutableList.<String>builder().addAll(getCommandPrefix()).add("@" + getArgfile()).build());
    } else {
        builder.setCommand(ImmutableList.<String>builder().addAll(getCommandPrefix()).addAll(getArguments(context.getAnsi().isAnsiTerminal())).build());
    }
    ProcessExecutorParams params = builder.build();
    LOG.debug("Running command (pwd=%s): %s", params.getDirectory(), getDescription(context));
    // Start the process.
    ProcessExecutor executor = new DefaultProcessExecutor(Console.createNullConsole());
    ProcessExecutor.LaunchedProcess process = executor.launchProcess(params);
    // We buffer error messages in memory, as these are typically small.
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    // Fire up managed threads to process the stdout and stderr lines.
    int exitCode;
    try {
        try (LineProcessorRunnable errorProcessor = createErrorTransformerFactory(context).createTransformerThread(context, compiler.getErrorStream(process), error)) {
            errorProcessor.start();
            errorProcessor.waitFor();
        } catch (Throwable thrown) {
            executor.destroyLaunchedProcess(process);
            throw thrown;
        }
        exitCode = executor.waitForLaunchedProcess(process).getExitCode();
    } finally {
        executor.destroyLaunchedProcess(process);
        executor.waitForLaunchedProcess(process);
    }
    // If we generated any error output, print that to the console.
    String err = new String(error.toByteArray());
    if (!err.isEmpty()) {
        context.getBuckEventBus().post(createConsoleEvent(context, preprocessorCommand.map(Optional::of).orElse(compilerCommand).get().supportsColorsInDiagnostics(), exitCode == 0 ? Level.WARNING : Level.SEVERE, err));
    }
    return exitCode;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) LineProcessorRunnable(com.facebook.buck.util.LineProcessorRunnable)

Example 48 with ProcessExecutorParams

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

the class ArchiveStep method runArchiver.

private ProcessExecutor.Result runArchiver(ExecutionContext context, final ImmutableList<String> command) throws IOException, InterruptedException {
    Map<String, String> env = new HashMap<>(context.getEnvironment());
    env.putAll(environment);
    ProcessExecutorParams params = ProcessExecutorParams.builder().setDirectory(filesystem.getRootPath()).setEnvironment(ImmutableMap.copyOf(env)).setCommand(command).build();
    ProcessExecutor.Result result = context.getProcessExecutor().launchAndExecute(params);
    if (result.getExitCode() != 0 && result.getStderr().isPresent()) {
        context.getBuckEventBus().post(ConsoleEvent.create(Level.SEVERE, result.getStderr().get()));
    }
    return result;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) HashMap(java.util.HashMap) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 49 with ProcessExecutorParams

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

the class ProjectBuildFileParser method init.

/**
   * Initialize the parser, starting buck.py.
   */
private void init() throws IOException {
    projectBuildFileParseEventStarted = new ProjectBuildFileParseEvents.Started();
    buckEventBus.post(projectBuildFileParseEventStarted);
    try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(buckEventBus, PerfEventId.of("ParserInit"))) {
        ImmutableMap.Builder<String, String> pythonEnvironmentBuilder = ImmutableMap.builder();
        // Strip out PYTHONPATH. buck.py manually sets this to include only nailgun. We don't want
        // to inject nailgun into the parser's PYTHONPATH, so strip that value out.
        // If we wanted to pass on some environmental PYTHONPATH, we would have to do some actual
        // merging of this and the BuckConfig's python module search path.
        pythonEnvironmentBuilder.putAll(Maps.filterKeys(environment, k -> !PYTHONPATH_ENV_VAR_NAME.equals(k)));
        if (options.getPythonModuleSearchPath().isPresent()) {
            pythonEnvironmentBuilder.put(PYTHONPATH_ENV_VAR_NAME, options.getPythonModuleSearchPath().get());
        }
        ImmutableMap<String, String> pythonEnvironment = pythonEnvironmentBuilder.build();
        ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(buildArgs()).setEnvironment(pythonEnvironment).build();
        LOG.debug("Starting buck.py command: %s environment: %s", params.getCommand(), params.getEnvironment());
        buckPyProcess = processExecutor.launchProcess(params);
        LOG.debug("Started process %s successfully", buckPyProcess);
        OutputStream stdin = buckPyProcess.getOutputStream();
        InputStream stderr = buckPyProcess.getErrorStream();
        InputStreamConsumer stderrConsumer = new InputStreamConsumer(stderr, (InputStreamConsumer.Handler) line -> buckEventBus.post(ConsoleEvent.warning("Warning raised by BUCK file parser: %s", line)));
        stderrConsumerTerminationFuture = new FutureTask<>(stderrConsumer);
        stderrConsumerThread = Threads.namedThread(ProjectBuildFileParser.class.getSimpleName(), stderrConsumerTerminationFuture);
        stderrConsumerThread.start();
        buckPyStdinWriter = new BufferedOutputStream(stdin);
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) ProjectWatch(com.facebook.buck.io.ProjectWatch) PerfEventId(com.facebook.buck.event.PerfEventId) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Supplier(com.google.common.base.Supplier) FutureTask(java.util.concurrent.FutureTask) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) BufferedOutputStream(java.io.BufferedOutputStream) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Value(org.immutables.value.Value) BserSerializer(com.facebook.buck.bser.BserSerializer) Map(java.util.Map) BserDeserializer(com.facebook.buck.bser.BserDeserializer) Suppliers(com.google.common.base.Suppliers) BuckStyleTuple(com.facebook.buck.util.immutables.BuckStyleTuple) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) OutputStream(java.io.OutputStream) Logger(com.facebook.buck.log.Logger) WatchmanDiagnostic(com.facebook.buck.io.WatchmanDiagnostic) MoreThrowables(com.facebook.buck.util.MoreThrowables) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) Threads(com.facebook.buck.util.Threads) IOException(java.io.IOException) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) PathOrGlobMatcher(com.facebook.buck.io.PathOrGlobMatcher) Maps(com.google.common.collect.Maps) ExecutionException(java.util.concurrent.ExecutionException) ConstructorArgMarshaller(com.facebook.buck.rules.ConstructorArgMarshaller) List(java.util.List) Paths(java.nio.file.Paths) AssertScopeExclusiveAccess(com.facebook.buck.util.concurrent.AssertScopeExclusiveAccess) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InputStreamConsumer(com.facebook.buck.util.InputStreamConsumer) Collections(java.util.Collections) Description(com.facebook.buck.rules.Description) InputStream(java.io.InputStream) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) InputStreamConsumer(com.facebook.buck.util.InputStreamConsumer) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) BufferedOutputStream(java.io.BufferedOutputStream)

Example 50 with ProcessExecutorParams

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

the class ExternalJavac method buildWithClasspath.

@Override
public int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, Optional<Path> workingDirectory, JavacOptions.AbiGenerationMode abiGenerationMode) throws InterruptedException {
    Preconditions.checkArgument(abiGenerationMode == AbstractJavacOptions.AbiGenerationMode.CLASS, "Source ABI verification requires JSR199");
    ImmutableList.Builder<String> command = ImmutableList.builder();
    command.add(pathToJavac.isLeft() ? pathToJavac.getLeft().toString() : context.getAbsolutePathsForInputs().get(0).toString());
    command.addAll(options);
    ImmutableList<Path> expandedSources;
    try {
        expandedSources = getExpandedSourcePaths(context.getProjectFilesystem(), invokingRule, javaSourceFilePaths, workingDirectory);
    } catch (IOException e) {
        throw new HumanReadableException("Unable to expand sources for %s into %s", invokingRule, workingDirectory);
    }
    try {
        context.getProjectFilesystem().writeLinesToPath(FluentIterable.from(expandedSources).transform(Object::toString).transform(ARGFILES_ESCAPER), pathToSrcsList);
        command.add("@" + pathToSrcsList);
    } catch (IOException e) {
        context.getEventSink().reportThrowable(e, "Cannot write list of .java files to compile to %s file! Terminating compilation.", pathToSrcsList);
        return 1;
    }
    // Run the command
    int exitCode = -1;
    try {
        ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(command.build()).setEnvironment(context.getEnvironment()).setDirectory(context.getProjectFilesystem().getRootPath().toAbsolutePath()).build();
        ProcessExecutor.Result result = context.getProcessExecutor().launchAndExecute(params);
        exitCode = result.getExitCode();
    } catch (IOException e) {
        e.printStackTrace(context.getStdErr());
        return exitCode;
    }
    return exitCode;
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ImmutableList(com.google.common.collect.ImmutableList) HumanReadableException(com.facebook.buck.util.HumanReadableException) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor)

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