Search in sources :

Example 11 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class CommandUtilsTest method longCommand.

@Test
public void longCommand() throws Exception {
    String[] args = new String[40];
    args[0] = "this_command_will_not_be_found";
    for (int i = 1; i < args.length; i++) {
        args[i] = "arg" + i;
    }
    Map<String, String> env = Maps.newTreeMap();
    env.put("PATH", "/usr/bin:/bin:/sbin");
    env.put("FOO", "foo");
    File directory = new File("/tmp");
    try {
        new Command(args, env, directory).execute();
        fail();
    } catch (CommandException exception) {
        String message = CommandUtils.describeCommandError(false, exception.getCommand());
        String verboseMessage = CommandUtils.describeCommandError(true, exception.getCommand());
        assertEquals("error executing command this_command_will_not_be_found arg1 " + "arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 " + "arg11 arg12 arg13 arg14 arg15 arg16 arg17 arg18 " + "arg19 arg20 arg21 arg22 arg23 arg24 arg25 arg26 " + "arg27 arg28 arg29 arg30 " + "... (remaining 9 argument(s) skipped)", message);
        assertEquals("error executing command \n" + "  (cd /tmp && \\\n" + "  exec env - \\\n" + "    FOO=foo \\\n" + "    PATH=/usr/bin:/bin:/sbin \\\n" + "  this_command_will_not_be_found arg1 " + "arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 " + "arg11 arg12 arg13 arg14 arg15 arg16 arg17 arg18 " + "arg19 arg20 arg21 arg22 arg23 arg24 arg25 arg26 " + "arg27 arg28 arg29 arg30 arg31 arg32 arg33 arg34 " + "arg35 arg36 arg37 arg38 arg39)", verboseMessage);
    }
}
Also used : Command(com.google.devtools.build.lib.shell.Command) CommandException(com.google.devtools.build.lib.shell.CommandException) File(java.io.File) Test(org.junit.Test)

Example 12 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class StandaloneSpawnStrategy method actuallyExec.

/**
   * Executes the given {@code spawn}.
   */
private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext) throws ExecException {
    Executor executor = actionExecutionContext.getExecutor();
    if (executor.reportsSubcommands()) {
        executor.reportSubcommand(spawn);
    }
    int timeoutSeconds = Spawns.getTimeoutSeconds(spawn);
    // We must wrap the subprocess with process-wrapper to kill the process tree.
    // All actions therefore depend on the process-wrapper file. Since it's embedded,
    // we don't bother with declaring it as an input.
    List<String> args = new ArrayList<>();
    if (OS.getCurrent() != OS.WINDOWS) {
        // TODO(bazel-team): process-wrapper seems to work on Windows, but requires
        // additional setup as it is an msys2 binary, so it needs msys2 DLLs on %PATH%.
        // Disable it for now to make the setup easier and to avoid further PATH hacks.
        // Ideally we should have a native implementation of process-wrapper for Windows.
        args.add(processWrapper.getPathString());
        args.add(Integer.toString(timeoutSeconds));
        args.add("5");
        /* kill delay: give some time to print stacktraces and whatnot. */
        // TODO(bazel-team): use process-wrapper redirection so we don't have to
        // pass test logs through the Java heap.
        args.add("-");
        /* stdout. */
        args.add("-");
    /* stderr. */
    }
    args.addAll(spawn.getArguments());
    String cwd = executor.getExecRoot().getPathString();
    Command cmd = new Command(args.toArray(new String[] {}), locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment()), new File(cwd), OS.getCurrent() == OS.WINDOWS && timeoutSeconds >= 0 ? timeoutSeconds * 1000 : -1);
    FileOutErr outErr = actionExecutionContext.getFileOutErr();
    try {
        cmd.execute(/* stdin */
        new byte[] {}, Command.NO_OBSERVER, outErr.getOutputStream(), outErr.getErrorStream(), /*killSubprocessOnInterrupt*/
        true);
    } catch (AbnormalTerminationException e) {
        TerminationStatus status = e.getResult().getTerminationStatus();
        boolean timedOut = !status.exited() && (status.timedout() || status.getTerminatingSignal() == 14);
        String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), spawn.getEnvironment(), cwd);
        throw new UserExecException(String.format("%s: %s", message, e), timedOut);
    } catch (CommandException e) {
        String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), spawn.getEnvironment(), cwd);
        throw new UserExecException(message, e);
    }
}
Also used : Executor(com.google.devtools.build.lib.actions.Executor) FileOutErr(com.google.devtools.build.lib.util.io.FileOutErr) Command(com.google.devtools.build.lib.shell.Command) TerminationStatus(com.google.devtools.build.lib.shell.TerminationStatus) ArrayList(java.util.ArrayList) UserExecException(com.google.devtools.build.lib.actions.UserExecException) AbnormalTerminationException(com.google.devtools.build.lib.shell.AbnormalTerminationException) CommandException(com.google.devtools.build.lib.shell.CommandException) File(java.io.File)

Example 13 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class BazelBuilder method clean.

@Override
public void clean() throws CommandException {
    String[] cleanCommand = { "bazel", "clean", "--expunge" };
    Command cmd = new Command(cleanCommand, null, generatedCodeDir.toFile());
    cmd.execute();
}
Also used : Command(com.google.devtools.build.lib.shell.Command)

Example 14 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class BazelBuilder method prepareFromGitRepo.

void prepareFromGitRepo(String gitRepo) throws IOException, CommandException {
    // Try to pull git repo first, delete directory if failed.
    if (builderDir.toFile().isDirectory()) {
        try {
            pullGitRepo();
        } catch (CommandException e) {
            FileSystemUtils.deleteTree(fileSystem.getPath(builderDir.toString()));
        }
    }
    if (Files.notExists(builderDir)) {
        try {
            Files.createDirectories(builderDir);
        } catch (IOException e) {
            throw new IOException("Failed to create directory for bazel", e);
        }
        String[] gitCloneCommand = { "git", "clone", gitRepo, "." };
        Command cmd = new Command(gitCloneCommand, null, builderDir.toFile());
        cmd.execute();
    }
// Assume the directory is what we need if not empty
}
Also used : Command(com.google.devtools.build.lib.shell.Command) CommandException(com.google.devtools.build.lib.shell.CommandException) IOException(java.io.IOException)

Example 15 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class DarwinSandboxRunner method isSupported.

static boolean isSupported() {
    // Check osx version, only >=10.11 is supported.
    // And we should check if sandbox still work when it gets 11.x
    String osxVersion = OS.getVersion();
    String[] parts = osxVersion.split("\\.");
    if (parts.length != 3) {
        // Currently the format is 10.11.x
        return false;
    }
    try {
        int v0 = Integer.parseInt(parts[0]);
        int v1 = Integer.parseInt(parts[1]);
        if (v0 != 10 || v1 < 11) {
            return false;
        }
    } catch (NumberFormatException e) {
        return false;
    }
    List<String> args = new ArrayList<>();
    args.add(SANDBOX_EXEC);
    args.add("-p");
    args.add("(version 1) (allow default)");
    args.add("/usr/bin/true");
    ImmutableMap<String, String> env = ImmutableMap.of();
    File cwd = new File("/usr/bin");
    Command cmd = new Command(args.toArray(new String[0]), env, cwd);
    try {
        cmd.execute(/* stdin */
        new byte[] {}, Command.NO_OBSERVER, ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream(), /* killSubprocessOnInterrupt */
        true);
    } catch (CommandException e) {
        return false;
    }
    return true;
}
Also used : Command(com.google.devtools.build.lib.shell.Command) ArrayList(java.util.ArrayList) CommandException(com.google.devtools.build.lib.shell.CommandException) File(java.io.File)

Aggregations

Command (com.google.devtools.build.lib.shell.Command)16 CommandException (com.google.devtools.build.lib.shell.CommandException)11 CommandResult (com.google.devtools.build.lib.shell.CommandResult)6 IOException (java.io.IOException)6 UserExecException (com.google.devtools.build.lib.actions.UserExecException)4 AbnormalTerminationException (com.google.devtools.build.lib.shell.AbnormalTerminationException)4 TerminationStatus (com.google.devtools.build.lib.shell.TerminationStatus)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 Executor (com.google.devtools.build.lib.actions.Executor)1 FileOutErr (com.google.devtools.build.lib.util.io.FileOutErr)1 Path (com.google.devtools.build.lib.vfs.Path)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 Path (java.nio.file.Path)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1