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);
}
}
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);
}
}
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;
}
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;
}
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;
}
}
Aggregations