Search in sources :

Example 11 with CommandException

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

the class CleanCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) throws ShutdownBlazeServerException {
    Options cleanOptions = options.getOptions(Options.class);
    cleanOptions.expunge_async = cleanOptions.cleanStyle.equals("expunge_async");
    cleanOptions.expunge = cleanOptions.cleanStyle.equals("expunge");
    cleanOptions.async = cleanOptions.cleanStyle.equals("async");
    env.getEventBus().post(new NoBuildEvent());
    if (!cleanOptions.expunge && !cleanOptions.expunge_async && !cleanOptions.async && !cleanOptions.cleanStyle.isEmpty()) {
        env.getReporter().handle(Event.error(null, "Invalid clean_style value '" + cleanOptions.cleanStyle + "'"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    String asyncName = (cleanOptions.expunge || cleanOptions.expunge_async) ? "--expunge_async" : "--async";
    // for non-Linux platforms (https://github.com/bazelbuild/bazel/issues/1906).
    if ((cleanOptions.expunge_async || cleanOptions.async) && OS.getCurrent() != OS.LINUX) {
        boolean expunge = cleanOptions.expunge_async;
        String fallbackName = expunge ? "--expunge" : "synchronous clean";
        env.getReporter().handle(Event.info(null, /*location*/
        asyncName + " cannot be used on non-Linux platforms, falling back to " + fallbackName));
        cleanOptions.expunge_async = false;
        cleanOptions.expunge = expunge;
        cleanOptions.async = false;
        cleanOptions.cleanStyle = expunge ? "expunge" : "";
    }
    String cleanBanner = (cleanOptions.expunge_async || cleanOptions.async) ? "Starting clean." : "Starting clean (this may take a while). " + "Consider using " + asyncName + " if the clean takes more than several minutes.";
    env.getReporter().handle(Event.info(null, /*location*/
    cleanBanner));
    try {
        String symlinkPrefix = options.getOptions(BuildRequest.BuildRequestOptions.class).getSymlinkPrefix(env.getRuntime().getProductName());
        actuallyClean(env, env.getOutputBase(), cleanOptions, symlinkPrefix);
        return ExitCode.SUCCESS;
    } catch (IOException e) {
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
    } catch (CommandException | ExecException e) {
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.RUN_FAILURE;
    } catch (InterruptedException e) {
        env.getReporter().handle(Event.error("clean interrupted"));
        return ExitCode.INTERRUPTED;
    }
}
Also used : ExecException(com.google.devtools.build.lib.actions.ExecException) NoBuildEvent(com.google.devtools.build.lib.analysis.NoBuildEvent) IOException(java.io.IOException) CommandException(com.google.devtools.build.lib.shell.CommandException)

Example 12 with CommandException

use of com.google.devtools.build.lib.shell.CommandException 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)

Example 13 with CommandException

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

the class DarwinSandboxedStrategy method getConfStr.

/**
   * Returns the value of a POSIX or X/Open system configuration variable.
   */
private static String getConfStr(String confVar) throws IOException {
    String[] commandArr = new String[2];
    commandArr[0] = "getconf";
    commandArr[1] = confVar;
    Command cmd = new Command(commandArr);
    CommandResult res;
    try {
        res = cmd.execute();
    } catch (CommandException e) {
        throw new IOException("getconf failed", e);
    }
    return new String(res.getStdout(), UTF_8).trim();
}
Also used : Command(com.google.devtools.build.lib.shell.Command) CommandException(com.google.devtools.build.lib.shell.CommandException) IOException(java.io.IOException) CommandResult(com.google.devtools.build.lib.shell.CommandResult)

Aggregations

CommandException (com.google.devtools.build.lib.shell.CommandException)13 Command (com.google.devtools.build.lib.shell.Command)11 IOException (java.io.IOException)6 AbnormalTerminationException (com.google.devtools.build.lib.shell.AbnormalTerminationException)5 ArrayList (java.util.ArrayList)5 UserExecException (com.google.devtools.build.lib.actions.UserExecException)4 CommandResult (com.google.devtools.build.lib.shell.CommandResult)4 TerminationStatus (com.google.devtools.build.lib.shell.TerminationStatus)4 File (java.io.File)4 Path (com.google.devtools.build.lib.vfs.Path)2 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)2 Test (org.junit.Test)2 ExecException (com.google.devtools.build.lib.actions.ExecException)1 Executor (com.google.devtools.build.lib.actions.Executor)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)1 NoBuildEvent (com.google.devtools.build.lib.analysis.NoBuildEvent)1 RunfilesSupport (com.google.devtools.build.lib.analysis.RunfilesSupport)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 RunUnder (com.google.devtools.build.lib.analysis.config.RunUnder)1