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.console;
18 import com.google.common.collect.Maps;
19 import com.intellij.openapi.application.TransactionGuard;
20 import com.intellij.openapi.application.TransactionId;
21 import com.intellij.openapi.module.Module;
22 import com.intellij.openapi.module.ModuleManager;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.projectRoots.Sdk;
25 import com.intellij.openapi.roots.ModuleRootManager;
26 import com.intellij.openapi.util.Pair;
27 import com.intellij.openapi.util.text.StringUtil;
28 import com.intellij.openapi.vfs.VirtualFile;
29 import com.intellij.util.PathMapper;
30 import com.jetbrains.python.buildout.BuildoutFacet;
31 import com.jetbrains.python.run.PythonCommandLineState;
32 import com.jetbrains.python.sdk.PythonEnvUtil;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import java.util.Collection;
37 import java.util.List;
43 public class PydevConsoleRunnerFactory extends PythonConsoleRunnerFactory {
46 public PydevConsoleRunnerImpl createConsoleRunner(@NotNull Project project,
47 @Nullable Module contextModule) {
48 Pair<Sdk, Module> sdkAndModule = PydevConsoleRunner.findPythonSdkAndModule(project, contextModule);
50 Module module = sdkAndModule.second;
51 Sdk sdk = sdkAndModule.first;
55 PyConsoleOptions.PyConsoleSettings settingsProvider = PyConsoleOptions.getInstance(project).getPythonConsoleSettings();
57 PathMapper pathMapper = PydevConsoleRunner.getPathMapper(project, sdk, settingsProvider);
59 String[] setupFragment;
61 Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module, settingsProvider.shouldAddContentRoots(),
62 settingsProvider.shouldAddSourceRoots());
64 if (pathMapper != null) {
65 pythonPath = pathMapper.convertToRemote(pythonPath);
68 String customStartScript = settingsProvider.getCustomStartScript();
70 if (customStartScript.trim().length() > 0) {
71 customStartScript = "\n" + customStartScript;
74 String selfPathAppend = PydevConsoleRunner.constructPythonPathCommand(pythonPath, customStartScript);
76 String workingDir = settingsProvider.getWorkingDirectory();
77 if (StringUtil.isEmpty(workingDir)) {
78 if (module != null && ModuleRootManager.getInstance(module).getContentRoots().length > 0) {
79 workingDir = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
82 if (ModuleManager.getInstance(project).getModules().length > 0) {
83 VirtualFile[] roots = ModuleRootManager.getInstance(ModuleManager.getInstance(project).getModules()[0]).getContentRoots();
84 if (roots.length > 0) {
85 workingDir = roots[0].getPath();
91 if (pathMapper != null && workingDir != null) {
92 workingDir = pathMapper.convertToRemote(workingDir);
95 BuildoutFacet facet = null;
97 facet = BuildoutFacet.getInstance(module);
101 List<String> path = facet.getAdditionalPythonPath();
102 if (pathMapper != null) {
103 path = pathMapper.convertToRemote(path);
105 String prependStatement = facet.getPathPrependStatement(path);
106 setupFragment = new String[]{prependStatement, selfPathAppend};
109 setupFragment = new String[]{selfPathAppend};
112 Map<String, String> envs = Maps.newHashMap(settingsProvider.getEnvs());
113 putIPythonEnvFlag(project, envs);
115 //The transaction is needed due the the FileDocumentManager.getInstance().saveAllDocuments() call in PydevConsoleRunnerImpl
116 Runnable rerunAction = () -> TransactionGuard.submitTransaction(project, () -> {
117 PydevConsoleRunnerImpl runner = createConsoleRunner(project, module);
121 return createConsoleRunner(project, sdk, workingDir, envs, PyConsoleType.PYTHON, settingsProvider, rerunAction, setupFragment);
124 public static void putIPythonEnvFlag(@NotNull Project project, Map<String, String> envs) {
125 String ipythonEnabled = PyConsoleOptions.getInstance(project).isIpythonEnabled() ? "True" : "False";
126 envs.put(PythonEnvUtil.IPYTHONENABLE, ipythonEnabled);
130 protected PydevConsoleRunnerImpl createConsoleRunner(Project project,
133 Map<String, String> envs,
134 PyConsoleType consoleType,
135 PyConsoleOptions.PyConsoleSettings settingsProvider,
136 Runnable rerunAction, String... setupFragment) {
137 return new PydevConsoleRunnerImpl(project, sdk, consoleType, workingDir, envs, settingsProvider, rerunAction, setupFragment);