Search in sources :

Example 56 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput 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)

Example 57 with ProcessOutput

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

the class PyStructuredDocstringFormatter method runExternalTool.

@Nullable
private static String runExternalTool(@NotNull final Module module, @NotNull final DocStringFormat format, @NotNull final String docstring) {
    final Sdk sdk;
    final String missingInterpreterMessage;
    if (format == DocStringFormat.EPYTEXT) {
        sdk = PythonSdkType.findPython2Sdk(module);
        missingInterpreterMessage = PyBundle.message("QDOC.epydoc.python2.sdk.not.found");
    } else {
        sdk = PythonSdkType.findLocalCPython(module);
        missingInterpreterMessage = PyBundle.message("QDOC.sdk.not.found");
    }
    if (sdk == null) {
        LOG.warn("Python SDK for docstring formatter " + format + " is not found");
        return "<p color=\"red\">" + missingInterpreterMessage + "</p>";
    }
    final String sdkHome = sdk.getHomePath();
    if (sdkHome == null)
        return null;
    final ByteBuffer encoded = DEFAULT_CHARSET.encode(docstring);
    final byte[] data = new byte[encoded.limit()];
    encoded.get(data);
    final ArrayList<String> arguments = Lists.newArrayList(format.getFormatterCommand());
    final GeneralCommandLine commandLine = PythonHelper.DOCSTRING_FORMATTER.newCommandLine(sdk, arguments);
    commandLine.setCharset(DEFAULT_CHARSET);
    LOG.debug("Command for launching docstring formatter: " + commandLine.getCommandLineString());
    final ProcessOutput output = PySdkUtil.getProcessOutput(commandLine, new File(sdkHome).getParent(), null, 5000, data, false);
    if (!output.checkSuccess(LOG)) {
        LOG.info("Malformed docstring:\n" + docstring);
        return null;
    }
    return output.getStdout();
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) Sdk(com.intellij.openapi.projectRoots.Sdk) StructuredDocString(com.jetbrains.python.psi.StructuredDocString) ByteBuffer(java.nio.ByteBuffer) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 58 with ProcessOutput

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

the class PythonSdkType method getSysPathsFromScript.

@NotNull
public static List<String> getSysPathsFromScript(@NotNull String binaryPath) throws InvalidSdkException {
    // to handle the situation when PYTHONPATH contains ., we need to run the syspath script in the
    // directory of the script itself - otherwise the dir in which we run the script (e.g. /usr/bin) will be added to SDK path
    GeneralCommandLine cmd = PythonHelper.SYSPATH.newCommandLine(binaryPath, Lists.newArrayList());
    final ProcessOutput runResult = PySdkUtil.getProcessOutput(cmd, new File(binaryPath).getParent(), getVirtualEnvExtraEnv(binaryPath), MINUTE);
    if (!runResult.checkSuccess(LOG)) {
        throw new InvalidSdkException(String.format("Failed to determine Python's sys.path value:\nSTDOUT: %s\nSTDERR: %s", runResult.getStdout(), runResult.getStderr()));
    }
    return runResult.getStdoutLines();
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with ProcessOutput

use of com.intellij.execution.process.ProcessOutput in project intellij-elixir by KronicDeth.

the class MixConfigurationForm method validateMixPath.

private boolean validateMixPath() {
    String mixPath = myMixPathSelector.getText();
    File mix = new File(mixPath);
    if (!mix.exists()) {
        return false;
    }
    if (!mix.canExecute()) {
        String reason = mix.getPath() + "is not executable.";
        LOGGER.warn("Can't detect Mix version: " + reason);
        return false;
    }
    File exeFile = mix.getAbsoluteFile();
    String exePath = exeFile.getPath();
    String workDir = exeFile.getParent();
    ProcessOutput output = null;
    boolean valid = false;
    for (String[] arguments : MIX_ARGUMENTS_ARRAY) {
        try {
            output = ElixirSystemUtil.getProcessOutput(3000, workDir, exePath, arguments);
        } catch (ExecutionException executionException) {
            LOGGER.warn(executionException);
        }
        if (output != null) {
            String transformedStdout = transformStdoutLine(output, STDOUT_LINE_TRANSFORMER);
            if (transformedStdout != null) {
                myMixVersionText.setText(transformedStdout);
                String versionString = transformedStdout.replaceAll("^[^0-9]*", "");
                // Support for the --formatter option may be added in a 1.3.x release, but I'm being conservative for now
                // and assuming it won't be released until 1.4
                ElixirSdkRelease elixirSdkRelease = ElixirSdkRelease.fromString(versionString);
                if (elixirSdkRelease != null) {
                    supportsFormatterOptionCheckBox.setSelected(elixirSdkRelease.compareTo(ElixirSdkRelease.V_1_4) >= 0);
                }
                valid = true;
                break;
            } else {
                String stderr = output.getStderr();
                StringBuilder text = new StringBuilder("N/A");
                if (StringUtil.isNotEmpty(stderr)) {
                    text.append(": Error: ").append(stderr);
                }
                myMixVersionText.setText(text.toString());
            }
        }
    }
    return valid;
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) ElixirSdkRelease(org.elixir_lang.sdk.ElixirSdkRelease) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 60 with ProcessOutput

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

the class PhoneGapAddPlatformBeforeRun method executeTask.

@Override
public boolean executeTask(DataContext context, final RunConfiguration configuration, ExecutionEnvironment env, PhoneGapAddPlatformTask task) {
    final PhoneGapRunConfiguration phoneGapRunConfiguration = (PhoneGapRunConfiguration) configuration;
    final PhoneGapCommandLine line = phoneGapRunConfiguration.getCommandLine();
    if (!line.needAddPlatform()) {
        return true;
    }
    final Project project = configuration.getProject();
    final Semaphore targetDone = new Semaphore();
    final Ref<Boolean> result = new Ref<>(true);
    final List<Exception> exceptions = new ArrayList<>();
    ApplicationManager.getApplication().invokeAndWait(() -> {
        //Save all opened documents
        FileDocumentManager.getInstance().saveAllDocuments();
        targetDone.down();
        new Task.Backgroundable(project, PhoneGapBundle.message("phonegap.before.task.init.title"), true) {

            public boolean shouldStartInBackground() {
                return true;
            }

            public void run(@NotNull final ProgressIndicator indicator) {
                try {
                    String platform = phoneGapRunConfiguration.getPlatform();
                    assert platform != null;
                    ProcessOutput output = line.platformAdd(platform);
                    if (output.getExitCode() != 0) {
                        ExecutionHelper.showOutput(project, output, PhoneGapBundle.message("phonegap.before.task.init.title"), null, true);
                        result.set(false);
                    }
                } catch (final Exception e) {
                    exceptions.add(e);
                    result.set(false);
                } finally {
                    targetDone.up();
                }
            }
        }.queue();
    }, ModalityState.NON_MODAL);
    if (!targetDone.waitFor(TimeUnit.MINUTES.toMillis(2))) {
        ExecutionHelper.showErrors(project, ContainerUtil.createMaybeSingletonList(new RuntimeException("Timeout")), PhoneGapBundle.message("phonegap.before.task.init.error"), null);
    } else if (!exceptions.isEmpty()) {
        ExecutionHelper.showErrors(project, exceptions, PhoneGapBundle.message("phonegap.before.task.init.error"), null);
    }
    return result.get();
}
Also used : PhoneGapCommandLine(com.github.masahirosuzuka.PhoneGapIntelliJPlugin.commandLine.PhoneGapCommandLine) Task(com.intellij.openapi.progress.Task) BeforeRunTask(com.intellij.execution.BeforeRunTask) ArrayList(java.util.ArrayList) Semaphore(com.intellij.util.concurrency.Semaphore) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessOutput(com.intellij.execution.process.ProcessOutput)

Aggregations

ProcessOutput (com.intellij.execution.process.ProcessOutput)66 ExecutionException (com.intellij.execution.ExecutionException)25 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 File (java.io.File)23 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)16 Nullable (org.jetbrains.annotations.Nullable)13 NotNull (org.jetbrains.annotations.NotNull)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 Project (com.intellij.openapi.project.Project)5 PsiFile (com.intellij.psi.PsiFile)5 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)4 CapturingAnsiEscapesAwareProcessHandler (com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler)3 Notification (com.intellij.notification.Notification)3 Document (com.intellij.openapi.editor.Document)3 Task (com.intellij.openapi.progress.Task)3 Sdk (com.intellij.openapi.projectRoots.Sdk)3