Search in sources :

Example 11 with CapturingProcessHandler

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

the class GitVersion method identifyVersion.

@NotNull
public static GitVersion identifyVersion(String gitExecutable) throws TimeoutException, ExecutionException, ParseException {
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setExePath(gitExecutable);
    commandLine.addParameter("--version");
    commandLine.setCharset(CharsetToolkit.getDefaultSystemCharset());
    CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
    ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    ProcessOutput result = indicator == null ? handler.runProcess(ExecutableValidator.TIMEOUT_MS) : handler.runProcessWithProgressIndicator(indicator);
    if (result.isTimeout()) {
        throw new TimeoutException("Couldn't identify the version of Git - stopped by timeout.");
    } else if (result.isCancelled()) {
        LOG.info("Cancelled by user. exitCode=" + result.getExitCode());
        throw new ProcessCanceledException();
    } else if (result.getExitCode() != 0 || !result.getStderr().isEmpty()) {
        LOG.info("getVersion exitCode=" + result.getExitCode() + " errors: " + result.getStderr());
        // anyway trying to parse
        try {
            parse(result.getStdout());
        } catch (ParseException pe) {
            throw new ExecutionException("Errors while executing git --version. exitCode=" + result.getExitCode() + " errors: " + result.getStderr());
        }
    }
    return parse(result.getStdout());
}
Also used : ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) ParseException(java.text.ParseException) ExecutionException(com.intellij.execution.ExecutionException) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) TimeoutException(java.util.concurrent.TimeoutException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with CapturingProcessHandler

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

the class PyCondaManagementService method removeRepository.

@Override
public void removeRepository(String repositoryUrl) {
    final String conda = PyCondaPackageService.getCondaExecutable(mySdk.getHomeDirectory());
    final ArrayList<String> parameters = Lists.newArrayList(conda, "config", "--remove", "channels", repositoryUrl, "--force");
    final GeneralCommandLine commandLine = new GeneralCommandLine(parameters);
    try {
        final CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
        final ProcessOutput result = handler.runProcess();
        final int exitCode = result.getExitCode();
        if (exitCode != 0) {
            final String message = StringUtil.isEmptyOrSpaces(result.getStdout()) && StringUtil.isEmptyOrSpaces(result.getStderr()) ? "Permission denied" : "Non-zero exit code";
            LOG.warn("Failed to remove repository " + message);
        }
        PyCondaPackageService.getInstance().removeChannel(repositoryUrl);
    } catch (ExecutionException e) {
        LOG.warn("Failed to remove repository");
    }
}
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)

Example 13 with CapturingProcessHandler

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

the class PyCondaPackageManagerImpl method createVirtualEnv.

@NotNull
public static String createVirtualEnv(@NotNull String destinationDir, String version) throws ExecutionException {
    final String condaExecutable = PyCondaPackageService.getSystemCondaExecutable();
    if (condaExecutable == null)
        throw new PyExecutionException("Cannot find conda", "Conda", Collections.<String>emptyList(), new ProcessOutput());
    final ArrayList<String> parameters = Lists.newArrayList(condaExecutable, "create", "-p", destinationDir, "-y", "python=" + version);
    final GeneralCommandLine commandLine = new GeneralCommandLine(parameters);
    final CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
    final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    final ProcessOutput result = handler.runProcessWithProgressIndicator(indicator);
    if (result.isCancelled()) {
        throw new RunCanceledByUserException();
    }
    final int exitCode = result.getExitCode();
    if (exitCode != 0) {
        final String message = StringUtil.isEmptyOrSpaces(result.getStdout()) && StringUtil.isEmptyOrSpaces(result.getStderr()) ? "Permission denied" : "Non-zero exit code";
        throw new PyExecutionException(message, "Conda", parameters, result);
    }
    final String binary = PythonSdkType.getPythonExecutable(destinationDir);
    final String binaryFallback = destinationDir + File.separator + "bin" + File.separator + "python";
    return (binary != null) ? binary : binaryFallback;
}
Also used : RunCanceledByUserException(com.intellij.execution.RunCanceledByUserException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessOutput(com.intellij.execution.process.ProcessOutput) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with CapturingProcessHandler

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

the class PyCondaPackageManagerImpl method getCondaOutput.

private ProcessOutput getCondaOutput(@NotNull final String command, List<String> arguments) throws ExecutionException {
    final Sdk sdk = getSdk();
    final String condaExecutable = PyCondaPackageService.getCondaExecutable(sdk.getHomeDirectory());
    if (condaExecutable == null)
        throw new PyExecutionException("Cannot find conda", "Conda", Collections.<String>emptyList(), new ProcessOutput());
    final String path = getCondaDirectory();
    if (path == null)
        throw new PyExecutionException("Empty conda name for " + sdk.getHomePath(), command, arguments);
    final ArrayList<String> parameters = Lists.newArrayList(condaExecutable, command, "-p", path);
    parameters.addAll(arguments);
    final GeneralCommandLine commandLine = new GeneralCommandLine(parameters);
    final CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
    final ProcessOutput result = handler.runProcess();
    final int exitCode = result.getExitCode();
    if (exitCode != 0) {
        final String message = StringUtil.isEmptyOrSpaces(result.getStdout()) && StringUtil.isEmptyOrSpaces(result.getStderr()) ? "Permission denied" : "Non-zero exit code";
        throw new PyExecutionException(message, "Conda", parameters, result);
    }
    return result;
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) Sdk(com.intellij.openapi.projectRoots.Sdk) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Aggregations

CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)14 ProcessOutput (com.intellij.execution.process.ProcessOutput)13 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)10 ExecutionException (com.intellij.execution.ExecutionException)6 File (java.io.File)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 OutputStream (java.io.OutputStream)2 TimeoutException (java.util.concurrent.TimeoutException)2 RunCanceledByUserException (com.intellij.execution.RunCanceledByUserException)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)1 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)1