Search in sources :

Example 11 with CommandOutputWithStatus

use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.

the class GitRepository method lsRemote.

/**
 * Runs a git ls-remote from the current directory for a repository url. Assumes the path to the
 * git binary is already set. You don't have to be in a git repository to run this command. Does
 * not work with remote names.
 *
 * @param url - see <repository> in git help ls-remote
 * @param refs - see <refs> in git help ls-remote
 * @param env - determines where the Git binaries are
 * @param maxLogLines - Limit log lines to the number specified. -1 for unlimited
 * @return - a map of refs to sha1 from the git ls-remote output.
 */
public static Map<String, String> lsRemote(String url, Collection<String> refs, Map<String, String> env, int maxLogLines) throws RepoException {
    ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
    List<String> args = Lists.newArrayList("ls-remote", validateUrl(url));
    args.addAll(refs);
    CommandOutputWithStatus output;
    try {
        output = executeGit(FileSystems.getDefault().getPath("."), args, env, false, maxLogLines);
    } catch (BadExitStatusWithOutputException e) {
        throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s': Exit code %s, Output:\n%s", url, refs, e.getOutput().getTerminationStatus().getExitCode(), e.getOutput().getStderr()), e);
    } catch (CommandException e) {
        throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s'", url, refs), e);
    }
    if (output.getTerminationStatus().success()) {
        for (String line : Splitter.on('\n').split(output.getStdout())) {
            if (line.isEmpty()) {
                continue;
            }
            Matcher matcher = LS_REMOTE_OUTPUT_LINE.matcher(line);
            if (!matcher.matches()) {
                throw new RepoException("Unexpected format for ls-remote output: " + line);
            }
            result.put(matcher.group(2), matcher.group(1));
        }
    }
    return result.build();
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) Matcher(com.google.re2j.Matcher) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with CommandOutputWithStatus

use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.

the class BuildozerOptions method run.

/**
 * Runs buildozer with the given commands.
 */
void run(Console console, Path checkoutDir, Iterable<BuildozerCommand> commands) throws ValidationException, TargetNotFoundException {
    List<String> args = Lists.newArrayList(buildozerBin, "-buildifier=" + buildifierOptions.buildifierBin);
    // We only use -k in keep going mode because it shows less errors (http://b/69386431)
    if (workflowOptions.ignoreNoop) {
        args.add("-k");
    }
    args.add("-f");
    args.add("-");
    try {
        Command cmd = new Command(args.toArray(new String[0]), /*environmentVariables*/
        null, checkoutDir.toFile());
        CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(generalOptions.isVerbose()).withInput(Joiner.on('\n').join(commands).getBytes(UTF_8)).execute();
        if (!output.getStdout().isEmpty()) {
            logger.atInfo().log("buildozer stdout: %s", output.getStdout());
        }
        if (!output.getStderr().isEmpty()) {
            logger.atInfo().log("buildozer stderr: %s", output.getStderr());
        }
    } catch (BadExitStatusWithOutputException e) {
        // Don't print the output for common/known errors.
        if (generalOptions.isVerbose()) {
            logError(console, e.getOutput());
        }
        if (e.getResult().getTerminationStatus().getExitCode() == 3) {
            // :%java_library
            throw new TargetNotFoundException(commandsMessage("Buildozer could not find a target for", commands));
        }
        if (e.getResult().getTerminationStatus().getExitCode() == 2) {
            ImmutableList<String> errors = Splitter.on('\n').splitToList(e.getOutput().getStderr()).stream().filter(s -> !(s.isEmpty() || s.startsWith("fixed "))).collect(ImmutableList.toImmutableList());
            ImmutableList.Builder<String> notFoundMsg = ImmutableList.builder();
            boolean allNotFound = true;
            for (String error : errors) {
                Matcher matcher = targetNotFound.matcher(error);
                if (matcher.matches()) {
                    notFoundMsg.add(String.format("Buildozer could not find a target for %s", matcher.group(1)));
                } else if (error.contains("no such file or directory") || error.contains("not a directory")) {
                    notFoundMsg.add("Buildozer could not find build file: " + error);
                } else {
                    allNotFound = false;
                }
            }
            if (allNotFound) {
                throw new TargetNotFoundException(Joiner.on("\n").join(notFoundMsg.build()));
            }
        }
        // Otherwise we have already printed above.
        if (!generalOptions.isVerbose()) {
            logError(console, e.getOutput());
        }
        throw new ValidationException(String.format("%s\nCommand stderr:%s", commandsMessage("Failed to execute buildozer with args", commands), e.getOutput().getStderr()), e);
    } catch (CommandException e) {
        String message = String.format("Error '%s' running buildozer command: %s", e.getMessage(), e.getCommand().toDebugString());
        console.error(message);
        throw new ValidationException(message, e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) ValidationException(com.google.copybara.exception.ValidationException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) CommandException(com.google.copybara.shell.CommandException)

Example 13 with CommandOutputWithStatus

use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.

the class BuildifierFormat method run.

/**
 * Runs buildifier with the given arguments.
 */
private void run(Console console, Path checkoutDir, Iterable<String> args) throws IOException, ValidationException {
    ImmutableList.Builder<String> argBuilder = new Builder<String>().add(buildifierOptions.buildifierBin);
    if (type != null) {
        argBuilder.add("-type=" + type);
    }
    if (lintMode != LintMode.OFF) {
        argBuilder.add("-lint=" + lintMode.toString().toLowerCase());
        if (!warnings.isEmpty()) {
            argBuilder.add("-warnings=" + Joiner.on(",").join(warnings));
        }
    }
    String[] argv = argBuilder.addAll(args).build().toArray(new String[0]);
    try {
        Command cmd = new Command(argv, /*environmentVariables*/
        null, checkoutDir.toFile());
        CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(generalOptions.isVerbose()).execute();
        if (!output.getStdout().isEmpty()) {
            logger.atInfo().log("buildifier stdout: %s", output.getStdout());
        }
        if (!output.getStderr().isEmpty()) {
            logger.atInfo().log("buildifier stderr: %s", output.getStderr());
        }
    } catch (BadExitStatusWithOutputException e) {
        log(console, e.getOutput());
        checkCondition(e.getResult().getTerminationStatus().getExitCode() != 1, "Build file(s) couldn't be formatted because there was a syntax error");
        throw new IOException("Failed to execute buildifier with args: " + args, e);
    } catch (CommandException e) {
        throw new IOException("Failed to execute buildifier with args: " + args, e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 14 with CommandOutputWithStatus

use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.

the class GitRepository method gitAllowNonZeroExit.

/**
 * Execute git allowing non-zero exit codes. This will only allow program non-zero exit codes
 * (0-10. The upper bound is arbitrary). And will still fail for exit codes like 127 (Command not
 * found).
 */
private CommandOutputWithStatus gitAllowNonZeroExit(byte[] stdin, Iterable<String> params, Duration defaultTimeout) throws RepoException {
    try {
        List<String> allParams = new ArrayList<>();
        allParams.add(gitEnv.resolveGitBinary());
        allParams.addAll(addGitDirAndWorkTreeParams(params));
        Command cmd = new Command(Iterables.toArray(allParams, String.class), gitEnv.getEnvironment(), getCwd().toFile());
        return new CommandRunner(cmd, defaultTimeout).withVerbose(verbose).withInput(stdin).execute();
    } catch (BadExitStatusWithOutputException e) {
        CommandOutputWithStatus output = e.getOutput();
        int exitCode = e.getOutput().getTerminationStatus().getExitCode();
        if (NON_CRASH_ERROR_EXIT_CODES.contains(exitCode)) {
            return output;
        }
        throw throwUnknownGitError(output, params);
    } catch (CommandException e) {
        throw new RepoException("Error executing 'git': " + e.getMessage(), e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) ArrayList(java.util.ArrayList) CommandException(com.google.copybara.shell.CommandException) RepoException(com.google.copybara.exception.RepoException) CommandRunner(com.google.copybara.util.CommandRunner)

Example 15 with CommandOutputWithStatus

use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.

the class CommandRunnerTest method testCommandWithMaxLogLines.

@Test
public void testCommandWithMaxLogLines() throws Exception {
    Command command = new Command(new String[] { "echo", "hello\n", "world" });
    CommandOutputWithStatus result = runCommand(new CommandRunner(command).withMaxStdOutLogLines(1));
    assertThat(result.getTerminationStatus().success()).isTrue();
    assertThat(result.getStdout()).isEqualTo("hello\n world\n");
    assertThat(outContent.toByteArray()).isEmpty();
    assertLogContains("Executing [echo 'hello\n' world]", "'echo' STDOUT: hello", "'echo' STDOUT: ... truncated after 1 line(s)");
}
Also used : Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Aggregations

CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)17 Command (com.google.copybara.shell.Command)10 CommandException (com.google.copybara.shell.CommandException)7 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)7 CommandRunner (com.google.copybara.util.CommandRunner)7 RepoException (com.google.copybara.exception.RepoException)6 ImmutableList (com.google.common.collect.ImmutableList)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ValidationException (com.google.copybara.exception.ValidationException)2 Matcher (com.google.re2j.Matcher)2 IOException (java.io.IOException)2 AccessValidationException (com.google.copybara.exception.AccessValidationException)1 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)1 BufferedWriter (java.io.BufferedWriter)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 LinkedHashSet (java.util.LinkedHashSet)1 Matcher (java.util.regex.Matcher)1