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