Search in sources :

Example 26 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project intellij-community by JetBrains.

the class CreateDesktopEntryAction method exec.

private static void exec(GeneralCommandLine command, @Nullable String prompt) throws IOException, ExecutionException {
    command.setRedirectErrorStream(true);
    ProcessOutput result = prompt != null ? ExecUtil.sudoAndGetOutput(command, prompt) : ExecUtil.execAndGetOutput(command);
    int exitCode = result.getExitCode();
    if (exitCode != 0) {
        String message = "Command '" + (prompt != null ? "sudo " : "") + command.getCommandLineString() + "' returned " + exitCode;
        String output = result.getStdout();
        if (!StringUtil.isEmptyOrSpaces(output))
            message += "\nOutput: " + output.trim();
        throw new RuntimeException(message);
    }
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput)

Example 27 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project intellij-community by JetBrains.

the class PlatformTestUtil method assertSuccessful.

public static void assertSuccessful(@NotNull GeneralCommandLine command) {
    try {
        ProcessOutput output = ExecUtil.execAndGetOutput(command.withRedirectErrorStream(true));
        Assert.assertEquals(output.getStdout(), 0, output.getExitCode());
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) ExecutionException(com.intellij.execution.ExecutionException)

Example 28 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project intellij-community by JetBrains.

the class Executor method run.

@NotNull
public static String run(@NotNull File workingDir, @NotNull List<String> params, boolean ignoreNonZeroExitCode) throws ExecutionException {
    final ProcessBuilder builder = new ProcessBuilder().command(params);
    builder.directory(workingDir);
    builder.redirectErrorStream(true);
    Process clientProcess;
    try {
        clientProcess = builder.start();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    String commandLine = StringUtil.join(params, " ");
    CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset(), commandLine);
    ProcessOutput result = handler.runProcess(30 * 1000);
    if (result.isTimeout()) {
        throw new RuntimeException("Timeout waiting for the command execution. Command: " + commandLine);
    }
    String stdout = result.getStdout().trim();
    if (result.getExitCode() != 0) {
        if (ignoreNonZeroExitCode) {
            debug("{" + result.getExitCode() + "}");
        }
        debug(stdout);
        if (!ignoreNonZeroExitCode) {
            throw new ExecutionException(result.getExitCode(), stdout);
        }
    } else {
        debug(stdout);
    }
    return stdout;
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) IOException(java.io.IOException) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project intellij-community by JetBrains.

the class TestClientRunner method runClient.

public ProcessOutput runClient(@NotNull String exeName, @Nullable String stdin, @Nullable final File workingDir, String... commandLine) throws IOException {
    final List<String> arguments = new ArrayList<>();
    final File client = new File(myClientBinaryPath, SystemInfo.isWindows ? exeName + ".exe" : exeName);
    if (client.exists()) {
        arguments.add(client.toString());
    } else {
        // assume client is in path
        arguments.add(exeName);
    }
    Collections.addAll(arguments, commandLine);
    if (myTraceClient) {
        LOG.info("*** running:\n" + arguments);
        if (StringUtil.isNotEmpty(stdin)) {
            LOG.info("*** stdin:\n" + stdin);
        }
    }
    final ProcessBuilder builder = new ProcessBuilder().command(arguments);
    if (workingDir != null) {
        builder.directory(workingDir);
    }
    if (myClientEnvironment != null) {
        builder.environment().putAll(myClientEnvironment);
    }
    final Process clientProcess = builder.start();
    if (stdin != null) {
        final OutputStream outputStream = clientProcess.getOutputStream();
        try {
            final byte[] bytes = stdin.getBytes();
            outputStream.write(bytes);
        } finally {
            outputStream.close();
        }
    }
    final CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset(), StringUtil.join(arguments, " "));
    final ProcessOutput result = handler.runProcess(100 * 1000, false);
    if (myTraceClient || result.isTimeout()) {
        LOG.debug("*** result: " + result.getExitCode());
        final String out = result.getStdout().trim();
        if (out.length() > 0) {
            LOG.debug("*** output:\n" + out);
        }
        final String err = result.getStderr().trim();
        if (err.length() > 0) {
            LOG.debug("*** error:\n" + err);
        }
    }
    if (result.isTimeout()) {
        String processList = LogUtil.getProcessList();
        handler.destroyProcess();
        throw new RuntimeException("Timeout waiting for VCS client to finish execution:\n" + processList);
    }
    return result;
}
Also used : OutputStream(java.io.OutputStream) ProcessOutput(com.intellij.execution.process.ProcessOutput) ArrayList(java.util.ArrayList) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Example 30 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project intellij-community by JetBrains.

the class GitExecutableDetector method runs.

/**
   * Checks if it is possible to run the specified program.
   * Made protected for tests not to start a process there.
   */
protected boolean runs(@NotNull String exec) {
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setExePath(exec);
    commandLine.setCharset(CharsetToolkit.getDefaultSystemCharset());
    try {
        CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
        ProcessOutput result = handler.runProcess((int) TimeUnit.SECONDS.toMillis(5));
        return !result.isTimeout();
    } catch (ExecutionException e) {
        return false;
    }
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) ExecutionException(com.intellij.execution.ExecutionException) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Aggregations

ProcessOutput (com.intellij.execution.process.ProcessOutput)57 File (java.io.File)22 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)20 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 ExecutionException (com.intellij.execution.ExecutionException)18 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)13 Nullable (org.jetbrains.annotations.Nullable)10 NotNull (org.jetbrains.annotations.NotNull)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 CapturingAnsiEscapesAwareProcessHandler (com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFile (com.intellij.psi.PsiFile)3 SkeletonVersionChecker.fromVersionString (com.jetbrains.python.sdk.skeletons.SkeletonVersionChecker.fromVersionString)3 IOException (java.io.IOException)3 PhoneGapCommandLine (com.github.masahirosuzuka.PhoneGapIntelliJPlugin.commandLine.PhoneGapCommandLine)2 Change (com.intellij.openapi.vcs.changes.Change)2 ChangeListManager (com.intellij.openapi.vcs.changes.ChangeListManager)2 TempDirTestFixture (com.intellij.testFramework.fixtures.TempDirTestFixture)2