Search in sources :

Example 1 with CapturingProcessHandler

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

the class PySdkUtil method getProcessOutput.

public static ProcessOutput getProcessOutput(@NotNull GeneralCommandLine cmd, @Nullable String homePath, @Nullable @NonNls Map<String, String> extraEnv, int timeout, @Nullable byte[] stdin, boolean needEOFMarker) {
    if (homePath == null || !new File(homePath).exists()) {
        return new ProcessOutput();
    }
    final Map<String, String> systemEnv = System.getenv();
    final Map<String, String> expandedCmdEnv = mergeEnvVariables(systemEnv, cmd.getEnvironment());
    final Map<String, String> env = extraEnv != null ? mergeEnvVariables(expandedCmdEnv, extraEnv) : expandedCmdEnv;
    PythonEnvUtil.resetHomePathChanges(homePath, env);
    try {
        final GeneralCommandLine commandLine = cmd.withWorkDirectory(homePath).withEnvironment(env);
        final CapturingProcessHandler processHandler = new CapturingProcessHandler(commandLine);
        if (stdin != null) {
            final OutputStream processInput = processHandler.getProcessInput();
            assert processInput != null;
            processInput.write(stdin);
            if (SystemInfo.isWindows && needEOFMarker) {
                processInput.write(SUBSTITUTE);
                processInput.flush();
            } else {
                processInput.close();
            }
        }
        return processHandler.runProcess(timeout);
    } catch (ExecutionException | IOException e) {
        return getOutputForException(e);
    }
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler)

Example 2 with CapturingProcessHandler

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

the class StudyCheckTask method getTestOutput.

@Nullable
private StudyTestsOutputParser.TestsOutput getTestOutput(@NotNull ProgressIndicator indicator) {
    final CapturingProcessHandler handler = new CapturingProcessHandler(myTestProcess, null, myCommandLine);
    final ProcessOutput output = handler.runProcessWithProgressIndicator(indicator);
    if (indicator.isCanceled()) {
        ApplicationManager.getApplication().invokeLater(() -> StudyCheckUtils.showTestResultPopUp("Check cancelled", MessageType.WARNING.getPopupBackground(), myProject));
    }
    myRunTestFile = !output.getStdout().contains(DO_NOT_RUN_ON_CHECK);
    final Course course = StudyTaskManager.getInstance(myProject).getCourse();
    if (course != null) {
        final StudyTestsOutputParser.TestsOutput testsOutput = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive());
        String stderr = output.getStderr();
        if (!stderr.isEmpty() && output.getStdout().isEmpty()) {
            //log error output of tests
            LOG.info("#educational " + stderr);
            return new StudyTestsOutputParser.TestsOutput(false, stderr);
        }
        return testsOutput;
    }
    return null;
}
Also used : ProcessOutput(com.intellij.execution.process.ProcessOutput) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with CapturingProcessHandler

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

the class StudySmartChecker method smartCheck.

public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
    VirtualFile fileWindows = null;
    File resourceFile = null;
    VirtualFile windowCopy = null;
    try {
        final int index = placeholder.getIndex();
        String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
        windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
        final FileDocumentManager documentManager = FileDocumentManager.getInstance();
        final Document windowDocument = documentManager.getDocument(windowCopy);
        if (windowDocument != null) {
            resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
            TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
            if (windowTaskFile == null) {
                return;
            }
            EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
            windowDocument.addDocumentListener(listener);
            int start = placeholder.getOffset();
            int end = start + placeholder.getRealLength();
            final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
            int userStart = userAnswerPlaceholder.getOffset();
            int userEnd = userStart + userAnswerPlaceholder.getRealLength();
            String text = usersDocument.getText(new TextRange(userStart, userEnd));
            windowDocument.replaceString(start, end, text);
            ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
            fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
            Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
            final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
            final ProcessOutput output = handler.runProcess();
            final Course course = StudyTaskManager.getInstance(project).getCourse();
            if (course != null) {
                boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
                StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
            }
        }
    } catch (ExecutionException | IOException e) {
        LOG.error(e);
    } finally {
        StudyUtils.deleteFile(windowCopy);
        StudyUtils.deleteFile(fileWindows);
        if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
            LOG.error("failed to delete", resourceFile.getPath());
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) TextRange(com.intellij.openapi.util.TextRange) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) ProcessOutput(com.intellij.execution.process.ProcessOutput) Course(com.jetbrains.edu.learning.courseFormat.Course) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 4 with CapturingProcessHandler

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

the class PyCondaManagementService method addRepository.

@Override
public void addRepository(String repositoryUrl) {
    final String conda = PyCondaPackageService.getCondaExecutable(mySdk.getHomeDirectory());
    final ArrayList<String> parameters = Lists.newArrayList(conda, "config", "--add", "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 add repository " + message);
        }
        PyCondaPackageService.getInstance().addChannel(repositoryUrl);
    } catch (ExecutionException e) {
        LOG.warn("Failed to add 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 5 with CapturingProcessHandler

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

the class ExecutableValidator method doCheckExecutable.

protected static boolean doCheckExecutable(@NotNull String executable, @NotNull List<String> processParameters) {
    try {
        GeneralCommandLine commandLine = new GeneralCommandLine();
        commandLine.setExePath(executable);
        commandLine.addParameters(processParameters);
        commandLine.setCharset(CharsetToolkit.getDefaultSystemCharset());
        CapturingProcessHandler handler = new CapturingProcessHandler(commandLine);
        ProcessOutput result = handler.runProcess(TIMEOUT_MS);
        boolean timeout = result.isTimeout();
        int exitCode = result.getExitCode();
        String stderr = result.getStderr();
        if (timeout) {
            LOG.warn("Validation of " + executable + " failed with a timeout");
        }
        if (exitCode != 0) {
            LOG.warn("Validation of " + executable + " failed with a non-zero exit code: " + exitCode);
        }
        if (!stderr.isEmpty()) {
            LOG.warn("Validation of " + executable + " failed with a non-empty error output: " + stderr);
        }
        return !timeout && exitCode == 0 && stderr.isEmpty();
    } catch (Throwable t) {
        LOG.warn(t);
        return false;
    }
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) 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