-- get rid of inheritance from AbstractConsoleRunner with history. It adds no almost...
[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.module.Module;
20 import com.intellij.openapi.module.ModuleManager;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.projectRoots.Sdk;
23 import com.intellij.openapi.roots.ModuleRootManager;
24 import com.intellij.openapi.util.Pair;
25 import com.intellij.openapi.util.text.StringUtil;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.util.PathMapper;
28 import com.jetbrains.python.buildout.BuildoutFacet;
29 import com.jetbrains.python.run.PythonCommandLineState;
30 import com.jetbrains.python.sdk.PythonEnvUtil;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.Map;
37
38 /**
39  * @author traff
40  */
41 public class PydevConsoleRunnerFactory extends PythonConsoleRunnerFactory {
42   @Override
43   public PydevConsoleRunnerImpl createConsoleRunner(@NotNull Project project,
44                                                     @Nullable Module contextModule) {
45     Pair<Sdk, Module> sdkAndModule = PydevConsoleRunner.findPythonSdkAndModule(project, contextModule);
46
47     Module module = sdkAndModule.second;
48     Sdk sdk = sdkAndModule.first;
49
50     assert sdk != null;
51
52     PyConsoleOptions.PyConsoleSettings settingsProvider = PyConsoleOptions.getInstance(project).getPythonConsoleSettings();
53
54     PathMapper pathMapper = PydevConsoleRunner.getPathMapper(project, sdk, settingsProvider);
55
56     String[] setupFragment;
57
58     Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module, settingsProvider.shouldAddContentRoots(),
59                                                                              settingsProvider.shouldAddSourceRoots());
60
61     if (pathMapper != null) {
62       pythonPath = pathMapper.convertToRemote(pythonPath);
63     }
64
65     String customStartScript = settingsProvider.getCustomStartScript();
66
67     if (customStartScript.trim().length() > 0) {
68       customStartScript = "\n" + customStartScript;
69     }
70
71     String selfPathAppend = PydevConsoleRunner.constructPythonPathCommand(pythonPath, customStartScript);
72
73     String workingDir = settingsProvider.getWorkingDirectory();
74     if (StringUtil.isEmpty(workingDir)) {
75       if (module != null && ModuleRootManager.getInstance(module).getContentRoots().length > 0) {
76         workingDir = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
77       }
78       else {
79         if (ModuleManager.getInstance(project).getModules().length > 0) {
80           VirtualFile[] roots = ModuleRootManager.getInstance(ModuleManager.getInstance(project).getModules()[0]).getContentRoots();
81           if (roots.length > 0) {
82             workingDir = roots[0].getPath();
83           }
84         }
85       }
86     }
87
88     if (pathMapper != null && workingDir != null) {
89       workingDir = pathMapper.convertToRemote(workingDir);
90     }
91
92     BuildoutFacet facet = null;
93     if (module != null) {
94       facet = BuildoutFacet.getInstance(module);
95     }
96
97     if (facet != null) {
98       List<String> path = facet.getAdditionalPythonPath();
99       if (pathMapper != null) {
100         path = pathMapper.convertToRemote(path);
101       }
102       String prependStatement = facet.getPathPrependStatement(path);
103       setupFragment = new String[]{prependStatement, selfPathAppend};
104     }
105     else {
106       setupFragment = new String[]{selfPathAppend};
107     }
108
109     Map<String, String> envs = Maps.newHashMap(settingsProvider.getEnvs());
110     String ipythonEnabled = PyConsoleOptions.getInstance(project).isIpythonEnabled() ? "True" : "False";
111     envs.put(PythonEnvUtil.IPYTHONENABLE, ipythonEnabled);
112     
113     return createConsoleRunner(project, sdk, workingDir, envs, PyConsoleType.PYTHON, settingsProvider, setupFragment);
114   }
115
116   protected PydevConsoleRunnerImpl createConsoleRunner(Project project,
117                                                        Sdk sdk,
118                                                        String workingDir,
119                                                        Map<String, String> envs,
120                                                        PyConsoleType consoleType,
121                                                        PyConsoleOptions.PyConsoleSettings settingsProvider,
122                                                        String... setupFragment) {
123     return new PydevConsoleRunnerImpl(project, sdk, consoleType, workingDir, envs, settingsProvider, setupFragment);
124   }
125 }