2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.run;
18 import com.google.common.collect.Lists;
19 import com.intellij.execution.DefaultExecutionResult;
20 import com.intellij.execution.ExecutionException;
21 import com.intellij.execution.ExecutionResult;
22 import com.intellij.execution.Executor;
23 import com.intellij.execution.configurations.GeneralCommandLine;
24 import com.intellij.execution.configurations.ParametersList;
25 import com.intellij.execution.configurations.ParamsGroup;
26 import com.intellij.execution.console.ConsoleExecuteAction;
27 import com.intellij.execution.executors.DefaultDebugExecutor;
28 import com.intellij.execution.runners.ExecutionEnvironment;
29 import com.intellij.openapi.actionSystem.AnAction;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.projectRoots.Sdk;
32 import com.intellij.openapi.util.text.StringUtil;
33 import com.intellij.util.ArrayUtil;
34 import com.jetbrains.python.PythonHelper;
35 import com.jetbrains.python.console.PyConsoleOptions;
36 import com.jetbrains.python.console.PyConsoleType;
37 import com.jetbrains.python.console.PydevConsoleRunner;
38 import com.jetbrains.python.console.PydevConsoleRunnerImpl;
39 import com.jetbrains.python.sdk.PythonEnvUtil;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
43 import java.util.List;
46 import static com.intellij.execution.runners.AbstractConsoleRunnerWithHistory.registerActionShortcuts;
51 public class PythonScriptCommandLineState extends PythonCommandLineState {
52 private final PythonRunConfiguration myConfig;
54 public PythonScriptCommandLineState(PythonRunConfiguration runConfiguration, ExecutionEnvironment env) {
55 super(runConfiguration, env);
56 myConfig = runConfiguration;
60 public ExecutionResult execute(Executor executor, final CommandLinePatcher... patchers) throws ExecutionException {
61 if (myConfig.showCommandLineAfterwards()) {
62 if (executor.getId() == DefaultDebugExecutor.EXECUTOR_ID) {
63 return super.execute(executor, ArrayUtil.append(patchers, new CommandLinePatcher() {
65 public void patchCommandLine(GeneralCommandLine commandLine) {
66 commandLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_DEBUGGER).addParameterAt(1, "--cmd-line");
71 PydevConsoleRunner runner =
72 new PythonScriptWithConsoleRunner(myConfig.getProject(), myConfig.getSdk(), PyConsoleType.PYTHON, myConfig.getWorkingDirectory(),
73 myConfig.getEnvs(), patchers,
74 PyConsoleOptions.getInstance(myConfig.getProject()).getPythonConsoleSettings());
77 // runner.getProcessHandler() would be null if execution error occurred
78 if (runner.getProcessHandler() == null) {
81 List<AnAction> actions = Lists.newArrayList(createActions(runner.getConsoleView(), runner.getProcessHandler()));
83 return new DefaultExecutionResult(runner.getConsoleView(), runner.getProcessHandler(), actions.toArray(new AnAction[actions.size()]));
86 return super.execute(executor, patchers);
91 protected void buildCommandLineParameters(GeneralCommandLine commandLine) {
92 ParametersList parametersList = commandLine.getParametersList();
93 ParamsGroup exe_options = parametersList.getParamsGroup(GROUP_EXE_OPTIONS);
94 assert exe_options != null;
95 exe_options.addParametersString(myConfig.getInterpreterOptions());
97 ParamsGroup script_parameters = parametersList.getParamsGroup(GROUP_SCRIPT);
98 assert script_parameters != null;
99 if (!StringUtil.isEmptyOrSpaces(myConfig.getScriptName())) {
100 script_parameters.addParameter(myConfig.getScriptName());
103 final String script_options_string = myConfig.getScriptParameters();
104 if (script_options_string != null) script_parameters.addParametersString(script_options_string);
106 if (!StringUtil.isEmptyOrSpaces(myConfig.getWorkingDirectory())) {
107 commandLine.setWorkDirectory(myConfig.getWorkingDirectory());
114 public class PythonScriptWithConsoleRunner extends PydevConsoleRunnerImpl {
116 private CommandLinePatcher[] myPatchers;
118 public PythonScriptWithConsoleRunner(@NotNull Project project,
120 @NotNull PyConsoleType consoleType,
121 @Nullable String workingDir,
122 Map<String, String> environmentVariables,
123 CommandLinePatcher[] patchers,
124 PyConsoleOptions.PyConsoleSettings consoleSettings,
125 String... statementsToExecute) {
126 super(project, sdk, consoleType, workingDir, environmentVariables, consoleSettings, statementsToExecute);
127 myPatchers = patchers;
131 protected void createContentDescriptorAndActions() {
132 AnAction a = new ConsoleExecuteAction(super.getConsoleView(), myConsoleExecuteActionHandler,
133 myConsoleExecuteActionHandler.getEmptyExecuteAction(), myConsoleExecuteActionHandler);
134 registerActionShortcuts(Lists.newArrayList(a), getConsoleView().getConsoleEditor().getComponent());
138 protected GeneralCommandLine createCommandLine(@NotNull Sdk sdk,
139 @NotNull Map<String, String> environmentVariables,
140 String workingDir, int[] ports) {
141 GeneralCommandLine consoleCmdLine = doCreateConsoleCmdLine(sdk, environmentVariables, workingDir, ports, PythonHelper.RUN_IN_CONSOLE);
143 final GeneralCommandLine cmd = generateCommandLine(myPatchers);
145 ParamsGroup group = consoleCmdLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
146 assert group != null;
147 group.addParameters(cmd.getParametersList().getList());
149 PythonEnvUtil.mergePythonPath(consoleCmdLine.getEnvironment(), cmd.getEnvironment());
151 consoleCmdLine.getEnvironment().putAll(cmd.getEnvironment());
153 return consoleCmdLine;