Search in sources :

Example 16 with GeneralCommandLine

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

the class PydevConsoleRunnerImpl method doCreateConsoleCmdLine.

@NotNull
protected GeneralCommandLine doCreateConsoleCmdLine(Sdk sdk, Map<String, String> environmentVariables, String workingDir, int[] ports, PythonHelper helper) {
    GeneralCommandLine cmd = PythonCommandLineState.createPythonCommandLine(myProject, new PythonConsoleRunParams(myConsoleSettings, workingDir, sdk, environmentVariables), false, PtyCommandLine.isEnabled() && !SystemInfo.isWindows);
    cmd.withWorkDirectory(myWorkingDir);
    ParamsGroup group = cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
    helper.addToGroup(group, cmd);
    for (int port : ports) {
        group.addParameter(String.valueOf(port));
    }
    return cmd;
}
Also used : ParamsGroup(com.intellij.execution.configurations.ParamsGroup) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with GeneralCommandLine

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

the class PydevConsoleRunnerImpl method createRemoteConsoleProcess.

private RemoteProcess createRemoteConsoleProcess(PythonRemoteInterpreterManager manager, String[] command, Map<String, String> env, File workDirectory) throws ExecutionException {
    PyRemoteSdkAdditionalDataBase data = (PyRemoteSdkAdditionalDataBase) mySdk.getSdkAdditionalData();
    assert data != null;
    GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setWorkDirectory(workDirectory);
    commandLine.withParameters(command);
    commandLine.getEnvironment().putAll(env);
    commandLine.getParametersList().set(0, PythonRemoteInterpreterManager.toSystemDependent(new File(data.getHelpersPath(), getRunnerFileFromHelpers()).getPath(), PySourcePosition.isWindowsPath(data.getInterpreterPath())));
    commandLine.getParametersList().set(1, "0");
    commandLine.getParametersList().set(2, "0");
    try {
        PyRemotePathMapper pathMapper = PydevConsoleRunner.getPathMapper(myProject, mySdk, myConsoleSettings);
        assert pathMapper != null;
        commandLine.putUserData(PyRemoteProcessStarter.OPEN_FOR_INCOMING_CONNECTION, true);
        // we do not have an option to setup Docker container settings now for Python console so we should bind at least project
        // directory to some path inside the Docker container
        commandLine.putUserData(PythonRemoteInterpreterManager.ADDITIONAL_MAPPINGS, buildDockerPathMappings());
        myRemoteProcessHandlerBase = PyRemoteProcessStarterManagerUtil.getManager(data).startRemoteProcess(myProject, commandLine, manager, data, pathMapper);
        myCommandLine = myRemoteProcessHandlerBase.getCommandLine();
        RemoteProcess remoteProcess = myRemoteProcessHandlerBase.getProcess();
        Couple<Integer> remotePorts = getRemotePortsFromProcess(remoteProcess);
        if (remoteProcess instanceof Tunnelable) {
            Tunnelable tunnelableProcess = (Tunnelable) remoteProcess;
            tunnelableProcess.addLocalTunnel(myPorts[0], remotePorts.first);
            tunnelableProcess.addRemoteTunnel(remotePorts.second, "localhost", myPorts[1]);
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Using tunneled communication for Python console: port %d (=> %d) on IDE side, " + "port %d (=> %d) on pydevconsole.py side", myPorts[1], remotePorts.second, myPorts[0], remotePorts.first));
            }
            myPydevConsoleCommunication = new PydevRemoteConsoleCommunication(myProject, myPorts[0], remoteProcess, myPorts[1]);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Using direct communication for Python console: port %d on IDE side, port %d on pydevconsole.py side", remotePorts.second, remotePorts.first));
            }
            myPydevConsoleCommunication = new PydevRemoteConsoleCommunication(myProject, remotePorts.first, remoteProcess, remotePorts.second);
        }
        return remoteProcess;
    } catch (Exception e) {
        throw new ExecutionException(e.getMessage(), e);
    }
}
Also used : PyRemotePathMapper(com.jetbrains.python.remote.PyRemotePathMapper) Tunnelable(com.intellij.remote.Tunnelable) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) PyRemoteSdkAdditionalDataBase(com.jetbrains.python.remote.PyRemoteSdkAdditionalDataBase) RemoteProcess(com.intellij.remote.RemoteProcess) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) XmlRpcException(org.apache.xmlrpc.XmlRpcException) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException)

Example 18 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 19 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 20 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)

Aggregations

GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)136 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