Fix Django console to work with the refactored Python console from #PR444
[idea/community.git] / python / src / com / jetbrains / python / console / PydevConsoleRunnerFactory.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.jetbrains.python.console;
17
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;
35
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.Map;
39
40 /**
41  * @author traff
42  */
43 public class PydevConsoleRunnerFactory extends PythonConsoleRunnerFactory {
44   @Override
45   @NotNull
46   public PydevConsoleRunnerImpl createConsoleRunner(@NotNull Project project,
47                                                     @Nullable Module contextModule) {
48     Pair<Sdk, Module> sdkAndModule = PydevConsoleRunner.findPythonSdkAndModule(project, contextModule);
49
50     Module module = sdkAndModule.second;
51     Sdk sdk = sdkAndModule.first;
52
53     assert sdk != null;
54
55     PyConsoleOptions.PyConsoleSettings settingsProvider = PyConsoleOptions.getInstance(project).getPythonConsoleSettings();
56
57     PathMapper pathMapper = PydevConsoleRunner.getPathMapper(project, sdk, settingsProvider);
58
59     String[] setupFragment;
60
61     Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module, settingsProvider.shouldAddContentRoots(),
62                                                                              settingsProvider.shouldAddSourceRoots());
63
64     if (pathMapper != null) {
65       pythonPath = pathMapper.convertToRemote(pythonPath);
66     }
67
68     String customStartScript = settingsProvider.getCustomStartScript();
69
70     if (customStartScript.trim().length() > 0) {
71       customStartScript = "\n" + customStartScript;
72     }
73
74     String selfPathAppend = PydevConsoleRunner.constructPythonPathCommand(pythonPath, customStartScript);
75
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();
80       }
81       else {
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();
86           }
87         }
88       }
89     }
90
91     if (pathMapper != null && workingDir != null) {
92       workingDir = pathMapper.convertToRemote(workingDir);
93     }
94
95     BuildoutFacet facet = null;
96     if (module != null) {
97       facet = BuildoutFacet.getInstance(module);
98     }
99
100     if (facet != null) {
101       List<String> path = facet.getAdditionalPythonPath();
102       if (pathMapper != null) {
103         path = pathMapper.convertToRemote(path);
104       }
105       String prependStatement = facet.getPathPrependStatement(path);
106       setupFragment = new String[]{prependStatement, selfPathAppend};
107     }
108     else {
109       setupFragment = new String[]{selfPathAppend};
110     }
111
112     Map<String, String> envs = Maps.newHashMap(settingsProvider.getEnvs());
113     putIPythonEnvFlag(project, envs);
114
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);
118       runner.run();
119     });
120
121     return createConsoleRunner(project, sdk, workingDir, envs, PyConsoleType.PYTHON, settingsProvider, rerunAction, setupFragment);
122   }
123
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);
127   }
128
129   @NotNull
130   protected PydevConsoleRunnerImpl createConsoleRunner(Project project,
131                                                        Sdk sdk,
132                                                        String workingDir,
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);
138   }
139 }