Search in sources :

Example 31 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine 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 32 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine 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 33 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-community by JetBrains.

the class JUnit4IntegrationTest method ignoredTestMethod.

@Test
public void ignoredTestMethod() throws Throwable {
    EdtTestUtil.runInEdtAndWait(() -> {
        PsiClass psiClass = findClass(getModule1(), CLASS_NAME);
        assertNotNull(psiClass);
        PsiMethod testMethod = psiClass.findMethodsByName(METHOD_NAME, false)[0];
        JUnitConfiguration configuration = createConfiguration(testMethod);
        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        RunnerAndConfigurationSettingsImpl settings = new RunnerAndConfigurationSettingsImpl(RunManagerImpl.getInstanceImpl(getProject()), configuration, false);
        ExecutionEnvironment environment = new ExecutionEnvironment(executor, ProgramRunnerUtil.getRunner(DefaultRunExecutor.EXECUTOR_ID, settings), settings, getProject());
        TestObject state = configuration.getState(executor, environment);
        JavaParameters parameters = state.getJavaParameters();
        parameters.setUseDynamicClasspath(getProject());
        GeneralCommandLine commandLine = parameters.toCommandLine();
        StringBuffer buf = new StringBuffer();
        StringBuffer err = new StringBuffer();
        OSProcessHandler process = new OSProcessHandler(commandLine);
        process.addProcessListener(new ProcessAdapter() {

            @Override
            public void onTextAvailable(ProcessEvent event, Key outputType) {
                String text = event.getText();
                try {
                    if (outputType == ProcessOutputTypes.STDOUT && !text.isEmpty() && ServiceMessage.parse(text.trim()) == null) {
                        buf.append(text);
                    }
                    if (outputType == ProcessOutputTypes.STDERR) {
                        err.append(text);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        process.startNotify();
        process.waitFor();
        process.destroyProcess();
        String testOutput = buf.toString();
        assertEmpty(err.toString());
        switch(myJUnitVersion) {
            //shouldn't work for old versions
            case "4.4":
            //shouldn't work for old versions
            case "4.5":
                break;
            default:
                assertTrue(testOutput, testOutput.contains("Test1"));
        }
    });
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) PsiMethod(com.intellij.psi.PsiMethod) ProcessEvent(com.intellij.execution.process.ProcessEvent) JUnitConfiguration(com.intellij.execution.junit.JUnitConfiguration) PsiClass(com.intellij.psi.PsiClass) TestObject(com.intellij.execution.junit.TestObject) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) Executor(com.intellij.execution.Executor) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) RunnerAndConfigurationSettingsImpl(com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl) JavaParameters(com.intellij.execution.configurations.JavaParameters) ParseException(java.text.ParseException) Key(com.intellij.openapi.util.Key) Test(org.junit.Test)

Example 34 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-community by JetBrains.

the class GenerateBinaryStubsFix method generateSkeletonsForList.

private boolean generateSkeletonsForList(@NotNull final PySkeletonRefresher refresher, ProgressIndicator indicator, @Nullable final String currentBinaryFilesPath) throws InvalidSdkException {
    final PySkeletonGenerator generator = new PySkeletonGenerator(refresher.getSkeletonsPath(), mySdk, currentBinaryFilesPath);
    indicator.setIndeterminate(false);
    final String homePath = mySdk.getHomePath();
    if (homePath == null)
        return false;
    GeneralCommandLine cmd = PythonHelper.EXTRA_SYSPATH.newCommandLine(homePath, Lists.newArrayList(myQualifiedName));
    final ProcessOutput runResult = PySdkUtil.getProcessOutput(cmd, new File(homePath).getParent(), PythonSdkType.getVirtualEnvExtraEnv(homePath), 5000);
    if (runResult.getExitCode() == 0 && !runResult.isTimeout()) {
        final String extraPath = runResult.getStdout();
        final PySkeletonGenerator.ListBinariesResult binaries = generator.listBinaries(mySdk, extraPath);
        final List<String> names = Lists.newArrayList(binaries.modules.keySet());
        Collections.sort(names);
        final int size = names.size();
        for (int i = 0; i != size; ++i) {
            final String name = names.get(i);
            indicator.setFraction((double) i / size);
            if (needBinaryList(name)) {
                indicator.setText2(name);
                final PySkeletonRefresher.PyBinaryItem item = binaries.modules.get(name);
                final String modulePath = item != null ? item.getPath() : "";
                //noinspection unchecked
                refresher.generateSkeleton(name, modulePath, new ArrayList<>(), Consumer.EMPTY_CONSUMER);
            }
        }
    }
    return true;
}
Also used : PySkeletonRefresher(com.jetbrains.python.sdk.skeletons.PySkeletonRefresher) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) PySkeletonGenerator(com.jetbrains.python.sdk.skeletons.PySkeletonGenerator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 35 with GeneralCommandLine

use of com.intellij.execution.configurations.GeneralCommandLine in project intellij-community by JetBrains.

the class Pep8ExternalAnnotator method doAnnotate.

@Nullable
@Override
public Results doAnnotate(State collectedInfo) {
    if (collectedInfo == null)
        return null;
    ArrayList<String> options = Lists.newArrayList();
    if (!collectedInfo.ignoredErrors.isEmpty()) {
        options.add("--ignore=" + DEFAULT_IGNORED_ERRORS + "," + StringUtil.join(collectedInfo.ignoredErrors, ","));
    }
    if (collectedInfo.hangClosingBrackets) {
        options.add("--hang-closing");
    }
    options.add("--max-line-length=" + collectedInfo.margin);
    options.add("-");
    GeneralCommandLine cmd = PythonHelper.PYCODESTYLE.newCommandLine(collectedInfo.interpreterPath, options);
    ProcessOutput output = PySdkUtil.getProcessOutput(cmd, new File(collectedInfo.interpreterPath).getParent(), ImmutableMap.of("PYTHONBUFFERED", "1"), 10000, collectedInfo.fileText.getBytes(), false);
    Results results = new Results(collectedInfo.level);
    if (output.isTimeout()) {
        LOG.info("Timeout running pycodestyle.py");
    } else if (output.getStderrLines().isEmpty()) {
        for (String line : output.getStdoutLines()) {
            final Problem problem = parseProblem(line);
            if (problem != null) {
                results.problems.add(problem);
            }
        }
    } else if (((ApplicationInfoImpl) ApplicationInfo.getInstance()).isEAP()) {
        LOG.info("Error running pycodestyle.py: " + output.getStderr());
    }
    return results;
}
Also used : GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) ProcessOutput(com.intellij.execution.process.ProcessOutput) ApplicationInfoImpl(com.intellij.openapi.application.impl.ApplicationInfoImpl) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)137 ExecutionException (com.intellij.execution.ExecutionException)42 File (java.io.File)35 NotNull (org.jetbrains.annotations.NotNull)35 ProcessOutput (com.intellij.execution.process.ProcessOutput)20 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)10 Project (com.intellij.openapi.project.Project)10 Key (com.intellij.openapi.util.Key)10 IOException (java.io.IOException)10 Nullable (org.jetbrains.annotations.Nullable)10 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)9 ProcessHandler (com.intellij.execution.process.ProcessHandler)7 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)7 Sdk (com.intellij.openapi.projectRoots.Sdk)7 PsiFile (com.intellij.psi.PsiFile)7 Test (org.junit.Test)6 CapturingAnsiEscapesAwareProcessHandler (com.intellij.execution.process.CapturingAnsiEscapesAwareProcessHandler)5 ArrayList (java.util.ArrayList)5 RunResult (org.jetbrains.kotlin.android.tests.run.RunResult)5