summaryrefslogtreecommitdiff
path: root/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java
diff options
context:
space:
mode:
Diffstat (limited to 'python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java')
-rw-r--r--python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java118
1 files changed, 118 insertions, 0 deletions
diff --git a/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java b/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java
index 81d2daaee3d0..cd92fdb34638 100644
--- a/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java
+++ b/python/src/com/jetbrains/python/run/PythonScriptCommandLineState.java
@@ -15,11 +15,37 @@
*/
package com.jetbrains.python.run;
+import com.google.common.collect.Lists;
+import com.intellij.execution.*;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.ParametersList;
import com.intellij.execution.configurations.ParamsGroup;
+import com.intellij.execution.executors.DefaultDebugExecutor;
+import com.intellij.execution.process.CommandLineArgumentsProvider;
+import com.intellij.execution.process.OSProcessHandler;
+import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
+import com.intellij.execution.ui.RunContentDescriptor;
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.progress.ProgressIndicator;
+import com.intellij.openapi.progress.ProgressManager;
+import com.intellij.openapi.progress.Task;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.projectRoots.Sdk;
+import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.util.ArrayUtil;
+import com.intellij.util.ui.UIUtil;
+import com.jetbrains.python.PythonHelpersLocator;
+import com.jetbrains.python.console.PyConsoleType;
+import com.jetbrains.python.console.PydevConsoleRunner;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
/**
* @author yole
@@ -32,6 +58,34 @@ public class PythonScriptCommandLineState extends PythonCommandLineState {
myConfig = runConfiguration;
}
+ @NotNull
+ @Override
+ public ExecutionResult execute(Executor executor, final CommandLinePatcher... patchers) throws ExecutionException {
+ if (myConfig.showCommandLineAfterwards()) {
+ if (executor.getId() == DefaultDebugExecutor.EXECUTOR_ID) {
+ return super.execute(executor, ArrayUtil.append(patchers, new CommandLinePatcher() {
+ @Override
+ public void patchCommandLine(GeneralCommandLine commandLine) {
+ commandLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_DEBUGGER).addParameterAt(1, "--cmd-line");
+ }
+ }));
+ }
+
+ PydevConsoleRunner runner =
+ new PythonScriptWithConsoleRunner(myConfig.getProject(), myConfig.getSdk(), PyConsoleType.PYTHON, myConfig.getWorkingDirectory(),
+ myConfig.getEnvs(), patchers);
+
+ runner.runSync();
+
+ List<AnAction> actions = Lists.newArrayList(createActions(runner.getConsoleView(), runner.getProcessHandler()));
+
+ return new DefaultExecutionResult(runner.getConsoleView(), runner.getProcessHandler(), actions.toArray(new AnAction[actions.size()]));
+ }
+ else {
+ return super.execute(executor, patchers);
+ }
+ }
+
@Override
protected void buildCommandLineParameters(GeneralCommandLine commandLine) {
ParametersList parametersList = commandLine.getParametersList();
@@ -53,4 +107,68 @@ public class PythonScriptCommandLineState extends PythonCommandLineState {
}
}
+ /**
+ * @author traff
+ */
+ public class PythonScriptWithConsoleRunner extends PydevConsoleRunner {
+
+ private CommandLinePatcher[] myPatchers;
+
+ public PythonScriptWithConsoleRunner(@NotNull Project project,
+ @NotNull Sdk sdk,
+ @NotNull PyConsoleType consoleType,
+ @Nullable String workingDir,
+ Map<String, String> environmentVariables,
+ CommandLinePatcher[] patchers,
+ String... statementsToExecute) {
+ super(project, sdk, consoleType, workingDir, environmentVariables, statementsToExecute);
+ myPatchers = patchers;
+ }
+
+ @Override
+ protected void createContentDescriptorAndActions() {
+ AnAction a = createConsoleExecAction(myConsoleExecuteActionHandler);
+ registerActionShortcuts(Lists.newArrayList(a), getConsoleView().getConsole().getConsoleEditor().getComponent());
+ }
+
+ @Override
+ protected CommandLineArgumentsProvider createCommandLineArgumentsProvider(final Sdk sdk,
+ final Map<String, String> environmentVariables,
+ int[] ports) {
+ final ArrayList<String> args = new ArrayList<String>();
+ args.add(sdk.getHomePath());
+ final String versionString = sdk.getVersionString();
+ if (versionString == null || !versionString.toLowerCase().contains("jython")) {
+ args.add("-u");
+ }
+ args.add(FileUtil.toSystemDependentName(PythonHelpersLocator.getHelperPath("pydev/pydev_run_in_console.py")));
+ for (int port : ports) {
+ args.add(String.valueOf(port));
+ }
+
+ try {
+ GeneralCommandLine cmd = generateCommandLine(myPatchers);
+ args.addAll(cmd.getParametersList().getList());
+ }
+ catch (Exception e) {
+ //pass
+ }
+ return new CommandLineArgumentsProvider() {
+ @Override
+ public String[] getArguments() {
+ return ArrayUtil.toStringArray(args);
+ }
+
+ @Override
+ public boolean passParentEnvs() {
+ return false;
+ }
+
+ @Override
+ public Map<String, String> getAdditionalEnvs() {
+ return addDefaultEnvironments(sdk, environmentVariables);
+ }
+ };
+ }
+ }
}