Search in sources :

Example 56 with ProcessAdapter

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

the class DebuggerTestCase method attachVM.

protected DebuggerSession attachVM(final RemoteConnection remoteConnection, final boolean pollConnection) throws InvocationTargetException, InterruptedException {
    final RemoteState remoteState = new RemoteStateState(myProject, remoteConnection);
    final DebuggerSession[] debuggerSession = new DebuggerSession[1];
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            try {
                debuggerSession[0] = attachVirtualMachine(remoteState, new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()).runProfile(new MockConfiguration()).build(), remoteConnection, pollConnection);
            } catch (ExecutionException e) {
                fail(e.getMessage());
            }
        }
    });
    debuggerSession[0].getProcess().getProcessHandler().addProcessListener(new ProcessAdapter() {

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            print(event.getText(), outputType);
        }
    });
    return debuggerSession[0];
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) ThrowableRunnable(com.intellij.util.ThrowableRunnable) RemoteStateState(com.intellij.debugger.engine.RemoteStateState) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Example 57 with ProcessAdapter

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

the class DebuggerTestCase method createLocalProcess.

protected DebuggerSession createLocalProcess(int transport, final JavaParameters javaParameters) throws ExecutionException, InterruptedException, InvocationTargetException {
    createBreakpoints(javaParameters.getMainClass());
    final DebuggerSession[] debuggerSession = new DebuggerSession[] { null };
    DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = transport;
    GenericDebuggerRunnerSettings debuggerRunnerSettings = new GenericDebuggerRunnerSettings();
    debuggerRunnerSettings.LOCAL = true;
    debuggerRunnerSettings.setDebugPort(String.valueOf(DEFAULT_ADDRESS));
    ExecutionEnvironment environment = new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()).runnerSettings(debuggerRunnerSettings).runProfile(new MockConfiguration()).build();
    final JavaCommandLineState javaCommandLineState = new JavaCommandLineState(environment) {

        @Override
        protected JavaParameters createJavaParameters() {
            return javaParameters;
        }

        @Override
        protected GeneralCommandLine createCommandLine() throws ExecutionException {
            return getJavaParameters().toCommandLine();
        }
    };
    final RemoteConnection debugParameters = DebuggerManagerImpl.createDebugParameters(javaCommandLineState.getJavaParameters(), debuggerRunnerSettings, true);
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            try {
                debuggerSession[0] = attachVirtualMachine(javaCommandLineState, javaCommandLineState.getEnvironment(), debugParameters, false);
            } catch (ExecutionException e) {
                fail(e.getMessage());
            }
        }
    });
    final ProcessHandler processHandler = debuggerSession[0].getProcess().getProcessHandler();
    debuggerSession[0].getProcess().addProcessListener(new ProcessAdapter() {

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            print(event.getText(), outputType);
        }
    });
    DebugProcessImpl process = (DebugProcessImpl) DebuggerManagerEx.getInstanceEx(myProject).getDebugProcess(processHandler);
    assertNotNull(process);
    return debuggerSession[0];
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) DebugProcessImpl(com.intellij.debugger.engine.DebugProcessImpl) ThrowableRunnable(com.intellij.util.ThrowableRunnable) ProcessHandler(com.intellij.execution.process.ProcessHandler) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Example 58 with ProcessAdapter

use of com.intellij.execution.process.ProcessAdapter in project kotlin by JetBrains.

the class RunUtils method run.

private static RunResult run(final RunSettings settings) {
    System.out.println("RUN COMMAND: " + settings);
    final StringBuilder stdOut = new StringBuilder();
    final StringBuilder stdErr = new StringBuilder();
    OSProcessHandler handler;
    try {
        handler = new OSProcessHandler(settings.commandLine.createProcess(), settings.commandLine.getCommandLineString(), Charsets.UTF_8);
        if (settings.input != null) {
            handler.getProcessInput().write(settings.input.getBytes());
        }
        close(handler.getProcessInput());
    } catch (ExecutionException e) {
        return new RunResult(false, getStackTrace(e));
    } catch (IOException e) {
        return new RunResult(false, getStackTrace(e));
    }
    handler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            System.out.println("TERMINATED: " + settings.commandLine);
            super.processTerminated(event);
        }

        @Override
        public void onTextAvailable(ProcessEvent event, Key outputType) {
            String str = event.getText();
            if (outputType == ProcessOutputTypes.STDOUT || outputType == ProcessOutputTypes.SYSTEM) {
                appendToContent(stdOut, str);
            } else if (outputType == ProcessOutputTypes.STDERR) {
                appendToContent(stdErr, str);
            }
        }

        private synchronized void appendToContent(StringBuilder content, String line) {
            if (settings.printOutputAtAppearance) {
                System.out.println(getPrefixString() + StringUtil.trimTrailing(line));
                System.out.flush();
            } else {
                content.append(getPrefixString());
                content.append(StringUtil.trimTrailing(line));
                content.append("\n");
            }
        }

        private String getPrefixString() {
            return (settings.outputPrefix != null) ? settings.outputPrefix + " " : "";
        }
    });
    handler.startNotify();
    if (settings.waitForEnd) {
        String timeoutAsString = System.getenv("kotlin.tests.android.timeout");
        if (timeoutAsString == null) {
            timeoutAsString = "30";
            System.err.println("Default value for timeout used: timeout = 30 min. You can change it using 'kotlin.tests.android.timeout' environment variable");
        }
        int timeout;
        try {
            timeout = Integer.parseInt(timeoutAsString);
        } catch (NumberFormatException e) {
            timeout = 30;
            System.err.println("Timeout system property should be a number");
        }
        handler.waitFor(timeout * 60 * 1000);
        if (!handler.isProcessTerminated()) {
            System.out.println("Output before handler.isProcessTerminated() " + settings.commandLine);
            System.out.println(stdOut);
            System.err.println(stdErr);
            return new RunResult(false, "Timeout exception: execution was terminated after ~20 min.");
        }
    } else {
        handler.waitFor();
    }
    int exitCode = handler.getProcess().exitValue();
    if (exitCode != 0) {
        return new RunResult(false, builderToString(stdOut) + builderToString(stdErr));
    } else {
        String output = builderToString(stdOut) + builderToString(stdErr);
        if (OutputUtils.isBuildFailed(output)) {
            return new RunResult(false, output);
        }
        if (!settings.commandLine.getCommandLineString().contains("install")) {
            System.out.print(output);
        }
        return new RunResult(true, output);
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Example 59 with ProcessAdapter

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

the class PyConsoleTask method runTestOn.

@Override
public void runTestOn(final String sdkHome) throws Exception {
    final Project project = getProject();
    final Sdk sdk = createTempSdk(sdkHome, SdkCreationType.EMPTY_SDK);
    setProcessCanTerminate(false);
    PydevConsoleRunner consoleRunner = new PydevConsoleRunnerImpl(project, sdk, PyConsoleType.PYTHON, myFixture.getTempDirPath(), Maps.newHashMap(), PyConsoleOptions.getInstance(project).getPythonConsoleSettings(), (s) -> {
    });
    before();
    myConsoleInitSemaphore = new Semaphore(0);
    consoleRunner.addConsoleListener(new PydevConsoleRunnerImpl.ConsoleListener() {

        @Override
        public void handleConsoleInitialized(LanguageConsoleView consoleView) {
            myConsoleInitSemaphore.release();
        }
    });
    consoleRunner.run();
    waitFor(myConsoleInitSemaphore);
    myCommandSemaphore = new Semaphore(1);
    myConsoleView = consoleRunner.getConsoleView();
    myProcessHandler = consoleRunner.getProcessHandler();
    myExecuteHandler = consoleRunner.getConsoleExecuteActionHandler();
    myCommunication = consoleRunner.getPydevConsoleCommunication();
    myCommunication.addCommunicationListener(new ConsoleCommunicationListener() {

        @Override
        public void commandExecuted(boolean more) {
            myCommandSemaphore.release();
        }

        @Override
        public void inputRequested() {
        }
    });
    myProcessHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            if (event.getExitCode() != 0 && !myProcessCanTerminate) {
                Assert.fail("Process terminated unexpectedly\n" + output());
            }
        }
    });
    OutputPrinter myOutputPrinter = null;
    if (shouldPrintOutput) {
        myOutputPrinter = new OutputPrinter();
        myOutputPrinter.start();
    }
    waitForOutput("PyDev console");
    try {
        testing();
        after();
    } finally {
        setProcessCanTerminate(true);
        if (myOutputPrinter != null) {
            myOutputPrinter.stop();
        }
        disposeConsole();
    }
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) LanguageConsoleView(com.intellij.execution.console.LanguageConsoleView) ProcessEvent(com.intellij.execution.process.ProcessEvent) Semaphore(java.util.concurrent.Semaphore) Project(com.intellij.openapi.project.Project) ConsoleCommunicationListener(com.jetbrains.python.console.pydev.ConsoleCommunicationListener) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 60 with ProcessAdapter

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

the class PyUnitTestTask method runConfiguration.

/**
   * Run configuration.
   *
   * @param settings settings (if have any, null otherwise)
   * @param config   configuration to run
   * @throws Exception
   */
protected void runConfiguration(@Nullable final RunnerAndConfigurationSettings settings, @NotNull final RunConfiguration config) throws Exception {
    final ExecutionEnvironment environment;
    if (settings == null) {
        environment = ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), config).build();
    } else {
        environment = ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), settings).build();
    }
    //noinspection ConstantConditions
    Assert.assertTrue(environment.getRunner().canRun(DefaultRunExecutor.EXECUTOR_ID, config));
    before();
    final com.intellij.util.concurrency.Semaphore s = new com.intellij.util.concurrency.Semaphore();
    s.down();
    myOutput = new StringBuilder();
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            try {
                environment.getRunner().execute(environment, new ProgramRunner.Callback() {

                    @Override
                    public void processStarted(RunContentDescriptor descriptor) {
                        myDescriptor = descriptor;
                        myProcessHandler = myDescriptor.getProcessHandler();
                        myProcessHandler.addProcessListener(new ProcessAdapter() {

                            @Override
                            public void onTextAvailable(ProcessEvent event, Key outputType) {
                                myOutput.append(event.getText());
                            }
                        });
                        myConsoleView = (SMTRunnerConsoleView) descriptor.getExecutionConsole();
                        myTestProxy = myConsoleView.getResultsViewer().getTestsRootNode();
                        myConsoleView.getResultsViewer().addEventsListener(new TestResultsViewer.SMEventsAdapter() {

                            @Override
                            public void onTestingFinished(TestResultsViewer sender) {
                                s.up();
                            }
                        });
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    Assert.assertTrue(s.waitFor(getTestTimeout()));
    XDebuggerTestUtil.waitForSwing();
    assertFinished();
    Assert.assertTrue(output(), allTestsCount() > 0);
    after();
    disposeProcess(myProcessHandler);
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) TestResultsViewer(com.intellij.execution.testframework.sm.runner.ui.TestResultsViewer) Key(com.intellij.openapi.util.Key)

Aggregations

ProcessAdapter (com.intellij.execution.process.ProcessAdapter)62 ProcessEvent (com.intellij.execution.process.ProcessEvent)59 Key (com.intellij.openapi.util.Key)19 NotNull (org.jetbrains.annotations.NotNull)15 ProcessHandler (com.intellij.execution.process.ProcessHandler)12 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)10 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)10 ExecutionException (com.intellij.execution.ExecutionException)9 IOException (java.io.IOException)8 BaseOSProcessHandler (com.intellij.execution.process.BaseOSProcessHandler)7 Nullable (org.jetbrains.annotations.Nullable)7 Project (com.intellij.openapi.project.Project)5 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)4 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)4 File (java.io.File)4 ExecutionResult (com.intellij.execution.ExecutionResult)3 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)3 Disposable (com.intellij.openapi.Disposable)3 Semaphore (com.intellij.util.concurrency.Semaphore)3 RunProfile (com.intellij.execution.configurations.RunProfile)2