2 * Copyright 2000-2015 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;
18 import com.google.common.collect.Lists;
19 import com.intellij.execution.configurations.GeneralCommandLine;
20 import com.intellij.execution.configurations.ParamsGroup;
21 import com.intellij.openapi.projectRoots.Sdk;
22 import com.intellij.openapi.util.io.FileUtil;
23 import com.jetbrains.python.psi.LanguageLevel;
24 import com.jetbrains.python.sdk.PythonEnvUtil;
25 import com.jetbrains.python.sdk.PythonSdkType;
26 import org.jetbrains.annotations.NotNull;
29 import java.util.List;
32 import static com.jetbrains.python.PythonHelpersLocator.getHelperFile;
33 import static com.jetbrains.python.PythonHelpersLocator.getHelpersRoot;
38 public enum PythonHelper implements HelperPackage {
39 COVERAGEPY("coveragepy", ""),
40 COVERAGE("coverage_runner", "run_coverage"),
41 DEBUGGER("pydev", "pydevd"),
43 ATTACH_DEBUGGER("pydev/pydevd_attach_to_process/attach_pydevd.py"),
45 CONSOLE("pydev", "pydevconsole"),
46 RUN_IN_CONSOLE("pydev", "pydev_run_in_console"),
47 PROFILER("profiler", "run_profiler"),
49 LOAD_ENTRY_POINT("pycharm", "pycharm_load_entry_point"),
52 UT("pycharm", "utrunner"),
53 SETUPPY("pycharm", "pycharm_setup_runner"),
54 NOSE("pycharm", "noserunner"),
55 PYTEST("pycharm", "pytestrunner"),
56 ATTEST("pycharm", "attestrunner"),
57 DOCSTRING("pycharm", "docrunner"),
59 BEHAVE("pycharm", "behave_runner"),
60 LETTUCE("pycharm", "lettuce_runner"),
62 DJANGO_TEST_MANAGE("pycharm", "django_test_manage"),
63 DJANGO_MANAGE("pycharm", "django_manage"),
64 MANAGE_TASKS_PROVIDER("pycharm", "_jb_manage_tasks_provider"),
66 APPCFG_CONSOLE("pycharm", "appcfg_fetcher"),
68 BUILDOUT_ENGULFER("pycharm", "buildout_engulfer"),
70 EPYDOC_FORMATTER("epydoc_formatter.py"),
71 REST_FORMATTER("rest_formatter.py"),
72 GOOGLE_FORMATTER("google_formatter.py"),
73 NUMPY_FORMATTER("numpy_formatter.py"),
75 EXTRA_SYSPATH("extra_syspath.py"),
76 SYSPATH("syspath.py"),
80 REST_RUNNER("rest_runners/rst2smth.py"),
82 SPHINX_RUNNER("rest_runners/sphinx_runner.py");
84 public static final String PY3_HELPER_DEPENDENCIES_DIR = "py3only";
85 public static final String PY2_HELPER_DEPENDENCIES_DIR = "py2only";
88 private static PathHelperPackage findModule(String moduleEntryPoint, String path, boolean asModule) {
89 if (getHelperFile(path + ".zip").isFile()) {
90 return new ModuleHelperPackage(moduleEntryPoint, path + ".zip");
93 if (!asModule && new File(getHelperFile(path), moduleEntryPoint + ".py").isFile()) {
94 return new ScriptPythonHelper(moduleEntryPoint + ".py", getHelperFile(path));
97 return new ModuleHelperPackage(moduleEntryPoint, path);
100 private final PathHelperPackage myModule;
102 PythonHelper(String pythonPath, String moduleName) {
103 this(pythonPath, moduleName, false);
106 PythonHelper(String pythonPath, String moduleName, boolean asModule) {
107 myModule = findModule(moduleName, pythonPath, asModule);
110 PythonHelper(String helperScript) {
111 myModule = new ScriptPythonHelper(helperScript, getHelpersRoot());
114 public abstract static class PathHelperPackage implements HelperPackage {
115 protected final File myPath;
117 PathHelperPackage(String path) {
118 myPath = new File(path);
122 public void addToPythonPath(@NotNull Map<String, String> environment) {
123 PythonEnvUtil.addToPythonPath(environment, getPythonPathEntry());
127 public void addToGroup(@NotNull ParamsGroup group, @NotNull GeneralCommandLine cmd) {
128 addToPythonPath(cmd.getEnvironment());
129 group.addParameter(asParamString());
134 public String asParamString() {
135 return FileUtil.toSystemDependentName(myPath.getAbsolutePath());
140 public GeneralCommandLine newCommandLine(@NotNull String sdkPath, @NotNull List<String> parameters) {
141 final List<String> args = Lists.newArrayList();
143 args.add(asParamString());
144 args.addAll(parameters);
145 final GeneralCommandLine cmd = new GeneralCommandLine(args);
146 addToPythonPath(cmd.getEnvironment());
152 public GeneralCommandLine newCommandLine(@NotNull Sdk pythonSdk, @NotNull List<String> parameters) {
153 final String sdkHomePath = pythonSdk.getHomePath();
154 assert sdkHomePath != null;
155 final GeneralCommandLine cmd = newCommandLine(sdkHomePath, parameters);
156 final LanguageLevel version = PythonSdkType.getLanguageLevelForSdk(pythonSdk);
157 final String perVersionDependenciesDir = version.isPy3K() ? PY3_HELPER_DEPENDENCIES_DIR : PY2_HELPER_DEPENDENCIES_DIR;
158 PythonEnvUtil.addToPythonPath(cmd.getEnvironment(), FileUtil.join(getPythonPathEntry(), perVersionDependenciesDir));
164 * Module Python helper can be executed from zip-archive
166 public static class ModuleHelperPackage extends PathHelperPackage {
167 private final String myModuleName;
169 public ModuleHelperPackage(String moduleName, String relativePath) {
170 super(getHelperFile(relativePath).getAbsolutePath());
171 this.myModuleName = moduleName;
176 public String asParamString() {
177 return "-m" + myModuleName;
182 public String getPythonPathEntry() {
183 return FileUtil.toSystemDependentName(myPath.getAbsolutePath());
188 * Script Python helper can be executed as a Python script, therefore
189 * PYTHONDONTWRITEBYTECODE option is set not to spoil installation
192 public static class ScriptPythonHelper extends PathHelperPackage {
193 private final String myPythonPath;
195 public ScriptPythonHelper(String script, File pythonPath) {
196 super(new File(pythonPath, script).getAbsolutePath());
197 myPythonPath = pythonPath.getAbsolutePath();
201 public void addToPythonPath(@NotNull Map<String, String> environment) {
202 PythonEnvUtil.setPythonDontWriteBytecode(environment);
203 super.addToPythonPath(environment);
208 public String getPythonPathEntry() {
216 public String getPythonPathEntry() {
217 return myModule.getPythonPathEntry();
221 public void addToPythonPath(@NotNull Map<String, String> environment) {
222 myModule.addToPythonPath(environment);
226 public void addToGroup(@NotNull ParamsGroup group, @NotNull GeneralCommandLine cmd) {
227 myModule.addToGroup(group, cmd);
232 public String asParamString() {
233 return myModule.asParamString();
238 public GeneralCommandLine newCommandLine(@NotNull String sdkPath, @NotNull List<String> parameters) {
239 return myModule.newCommandLine(sdkPath, parameters);
244 public GeneralCommandLine newCommandLine(@NotNull Sdk pythonSdk, @NotNull List<String> parameters) {
245 return myModule.newCommandLine(pythonSdk, parameters);