Example #1
0
 private String getGoEnvFromTool(ProcessExecutor processExecutor, String env) {
   Path goTool = getGoToolPath();
   Optional<Map<String, String>> goRootEnv =
       delegate
           .getPath("go", "root")
           .transform(
               new Function<Path, Map<String, String>>() {
                 @Override
                 public Map<String, String> apply(Path input) {
                   return ImmutableMap.of("GOROOT", input.toString());
                 }
               });
   try {
     ProcessExecutor.Result goToolResult =
         processExecutor.launchAndExecute(
             ProcessExecutorParams.builder()
                 .addCommand(goTool.toString(), "env", env)
                 .setEnvironment(goRootEnv)
                 .build(),
             EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_ERR),
             /* stdin */ Optional.<String>absent(),
             /* timeOutMs */ Optional.<Long>absent(),
             /* timeoutHandler */ Optional.<Function<Process, Void>>absent());
     if (goToolResult.getExitCode() == 0) {
       return CharMatcher.WHITESPACE.trimFrom(goToolResult.getStdout().get());
     } else {
       throw new HumanReadableException(goToolResult.getStderr().get());
     }
   } catch (InterruptedException e) {
     throw Throwables.propagate(e);
   } catch (IOException e) {
     throw new HumanReadableException(e, "Could not run \"%s env %s\": %s", env, goTool);
   }
 }
Example #2
0
  public int createIntellijProject(
      File jsonTempFile,
      ProcessExecutor processExecutor,
      boolean generateMinimalProject,
      PrintStream stdOut,
      PrintStream stdErr)
      throws IOException {
    List<Module> modules = createModulesForProjectConfigs();
    writeJsonConfig(jsonTempFile, modules);

    List<String> modifiedFiles = Lists.newArrayList();

    // Process the JSON config to generate the .xml and .iml files for IntelliJ.
    ExitCodeAndOutput result = processJsonConfig(jsonTempFile, generateMinimalProject);
    if (result.exitCode != 0) {
      return result.exitCode;
    } else {
      // intellij.py writes the list of modified files to stdout, so parse stdout and add the
      // resulting file paths to the modifiedFiles list.
      Iterable<String> paths =
          Splitter.on('\n').trimResults().omitEmptyStrings().split(result.stdOut);
      Iterables.addAll(modifiedFiles, paths);
    }

    // Write out the project.properties files.
    List<String> modifiedPropertiesFiles = generateProjectDotPropertiesFiles(modules);
    modifiedFiles.addAll(modifiedPropertiesFiles);

    // Write out the .idea/compiler.xml file (the .idea/ directory is guaranteed to exist).
    CompilerXml compilerXml = new CompilerXml(modules);
    final String pathToCompilerXml = ".idea/compiler.xml";
    File compilerXmlFile = projectFilesystem.getFileForRelativePath(pathToCompilerXml);
    if (compilerXml.write(compilerXmlFile)) {
      modifiedFiles.add(pathToCompilerXml);
    }

    // If the user specified a post-processing script, then run it.
    if (pathToPostProcessScript.isPresent()) {
      String pathToScript = pathToPostProcessScript.get();
      Process process = Runtime.getRuntime().exec(new String[] {pathToScript});
      ProcessExecutor.Result postProcessResult = processExecutor.execute(process);
      int postProcessExitCode = postProcessResult.getExitCode();
      if (postProcessExitCode != 0) {
        return postProcessExitCode;
      }
    }

    // If any files have been modified by `buck project`, then list them for the user.
    if (!modifiedFiles.isEmpty()) {
      SortedSet<String> modifiedFilesInSortedForder = Sets.newTreeSet(modifiedFiles);
      stdOut.printf("MODIFIED FILES:\n%s\n", Joiner.on('\n').join(modifiedFilesInSortedForder));
    }
    // Blit stderr from intellij.py to parent stderr.
    stdErr.print(result.stdErr);

    return 0;
  }
 @Test
 public void fatJarWithOutput() throws IOException, InterruptedException {
   ProjectWorkspace workspace =
       TestDataHelper.createProjectWorkspaceForScenario(this, "fat_jar", tmp);
   workspace.setUp();
   Path jar = workspace.buildAndReturnOutput("//:bin-output");
   ProcessExecutor.Result result = workspace.runJar(jar);
   assertEquals("output", result.getStdout().get().trim());
   assertEquals("error", result.getStderr().get().trim());
 }
 private ProcessExecutor.Result runRuleKeyDiffer(ProjectWorkspace workspace)
     throws IOException, InterruptedException {
   ProcessExecutor.Result result =
       workspace.runCommand(
           "python2.7",
           Paths.get("scripts", "diff_rulekeys.py").toString(),
           tmp.getRootPath().resolve("buck-0.log").toString(),
           tmp.getRootPath().resolve("buck-1.log").toString(),
           "//:java_lib_2");
   assertThat(result.getStderr(), Matchers.equalTo(Optional.of("")));
   assertThat(result.getExitCode(), Matchers.is(0));
   return result;
 }
Example #5
0
  @Override
  public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
    // Build the process, redirecting output to the provided output file.  In general,
    // it's undesirable that both stdout and stderr are being redirected to the same
    // input stream.  However, due to the nature of OS pipe buffering, we can't really
    // maintain the natural interleaving of multiple output streams in a way that we
    // can correctly associate both stdout/stderr streams to the one correct test out
    // of the many that ran.  So, our best bet is to just combine them all into stdout,
    // so they get properly interleaved with the test start and end messages that we
    // use when we parse the test output.
    ProcessBuilder builder = new ProcessBuilder();
    builder.command(command);
    builder.redirectOutput(filesystem.resolve(output).toFile());
    builder.redirectErrorStream(true);

    Process process;
    try {
      process = BgProcessKiller.startProcess(builder);
    } catch (IOException e) {
      context.logError(e, "Error starting command %s", command);
      return StepExecutionResult.ERROR;
    }

    // Run the test process, saving the exit code.
    ProcessExecutor executor = context.getProcessExecutor();
    ImmutableSet<ProcessExecutor.Option> options =
        ImmutableSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);
    ProcessExecutor.Result result =
        executor.execute(
            process,
            options,
            /* stdin */ Optional.<String>absent(),
            /* timeOutMs */ testRuleTimeoutMs,
            /* timeOutHandler */ Optional.<Function<Process, Void>>absent());

    if (result.isTimedOut()) {
      throw new HumanReadableException(
          "Timed out after %d ms running test command %s", testRuleTimeoutMs.or(-1L), command);
    }

    // Since test binaries return a non-zero exit code when unittests fail, save the exit code
    // to a file rather than signalling a step failure.
    try (FileOutputStream stream = new FileOutputStream(filesystem.resolve(exitCode).toFile())) {
      stream.write((Integer.toString(result.getExitCode())).getBytes());
    } catch (IOException e) {
      context.logError(e, "Error saving exit code to %s", exitCode);
      return StepExecutionResult.ERROR;
    }

    return StepExecutionResult.SUCCESS;
  }
Example #6
0
 @VisibleForTesting
 static PythonVersion extractPythonVersion(Path pythonPath, ProcessExecutor.Result versionResult) {
   if (versionResult.getExitCode() == 0) {
     String versionString =
         CharMatcher.WHITESPACE.trimFrom(
             CharMatcher.WHITESPACE.trimFrom(versionResult.getStderr().get())
                 + CharMatcher.WHITESPACE
                     .trimFrom(versionResult.getStdout().get())
                     .replaceAll("\u001B\\[[;\\d]*m", ""));
     Matcher matcher = PYTHON_VERSION_REGEX.matcher(versionString.split("\\r?\\n")[0]);
     if (!matcher.matches()) {
       throw new HumanReadableException(
           "`%s -V` returned an invalid version string %s", pythonPath, versionString);
     }
     return PythonVersion.of(matcher.group(1));
   } else {
     throw new HumanReadableException(versionResult.getStderr().get());
   }
 }
Example #7
0
  /**
   * 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", "-vvvv", 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");
  }
  @Test
  public void appleLibraryBuildsMultiarchFramework() throws Exception {
    assumeTrue(Platform.detect() == Platform.MACOS);
    assumeTrue(AppleNativeIntegrationTestUtils.isApplePlatformAvailable(ApplePlatform.MACOSX));

    ProjectWorkspace workspace =
        TestDataHelper.createProjectWorkspaceForScenario(
            this, "apple_library_builds_something", tmp);
    workspace.setUp();
    ProjectFilesystem filesystem = new ProjectFilesystem(workspace.getDestPath());

    BuildTarget target =
        BuildTargetFactory.newInstance(
            "//Libraries/TestLibrary:TestLibrary#framework,macosx-x86_64,macosx-i386,no-debug");
    ProjectWorkspace.ProcessResult result =
        workspace.runBuckCommand("build", target.getFullyQualifiedName());
    result.assertSuccess();

    Path frameworkPath =
        workspace.getPath(
            BuildTargets.getGenPath(
                    filesystem,
                    BuildTarget.builder(target)
                        .addFlavors(AppleDescriptions.INCLUDE_FRAMEWORKS_FLAVOR)
                        .build(),
                    "%s")
                .resolve("TestLibrary.framework"));
    Path libraryPath = frameworkPath.resolve("TestLibrary");
    assertThat(Files.exists(libraryPath), is(true));
    ProcessExecutor.Result lipoVerifyResult =
        workspace.runCommand("lipo", libraryPath.toString(), "-verify_arch", "i386", "x86_64");
    assertEquals(lipoVerifyResult.getStderr().orElse(""), 0, lipoVerifyResult.getExitCode());
    assertThat(
        workspace.runCommand("file", libraryPath.toString()).getStdout().get(),
        containsString("dynamically linked shared library"));
  }