Python console execution refactoring. Occasionally fixes PY-13041

This commit is contained in:
Dmitry Trofimov
2015-07-31 00:18:04 +02:00
parent 0ec428183b
commit 8dc480c2e2
20 changed files with 257 additions and 191 deletions

View File

@@ -25,7 +25,10 @@ import java.util.Map;
/**
* @author Roman.Chernyatchik, oleg
* @deprecated Use GeneralCommandLine instead
* @deprecated Usages only in Ruby. Move to Ruby module?
*/
@Deprecated
public class CommandLineArgumentsProvider {
/**
* @return Commands to execute (one command corresponds to one add argument)

View File

@@ -364,4 +364,10 @@ public class PyIdeCommonOptionsForm implements AbstractPyCommonOptionsForm {
public void sdkHomeSelected(Sdk sdk, String newSdkHome) {
}
}
@Override
public String getModuleName() {
Module module = getModule();
return module != null? module.getName() : null;
}
}

View File

@@ -16,53 +16,12 @@
package com.jetbrains.python.run;
import com.intellij.openapi.module.Module;
import com.intellij.util.PathMappingSettings;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* @author Leonid Shalupov
*/
public interface AbstractPythonRunConfigurationParams {
String getInterpreterOptions();
void setInterpreterOptions(String interpreterOptions);
String getWorkingDirectory();
void setWorkingDirectory(String workingDirectory);
@Nullable
String getSdkHome();
void setSdkHome(String sdkHome);
public interface AbstractPythonRunConfigurationParams extends PythonRunParams {
@Nullable
Module getModule();
void setModule(Module module);
boolean isUseModuleSdk();
void setUseModuleSdk(boolean useModuleSdk);
boolean isPassParentEnvs();
void setPassParentEnvs(boolean passParentEnvs);
Map<String, String> getEnvs();
void setEnvs(final Map<String, String> envs);
@Nullable
PathMappingSettings getMappingSettings();
void setMappingSettings(@Nullable PathMappingSettings mappingSettings);
boolean shouldAddContentRoots();
boolean shouldAddSourceRoots();
void setAddContentRoots(boolean flag);
void setAddSourceRoots(boolean flag);
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.run;
import com.intellij.openapi.module.Module;
import com.intellij.util.PathMappingSettings;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* @author traff
*/
public interface PythonRunParams {
String getInterpreterOptions();
void setInterpreterOptions(String interpreterOptions);
String getWorkingDirectory();
void setWorkingDirectory(String workingDirectory);
@Nullable
String getSdkHome();
void setSdkHome(String sdkHome);
void setModule(Module module);
String getModuleName();
boolean isUseModuleSdk();
void setUseModuleSdk(boolean useModuleSdk);
boolean isPassParentEnvs();
void setPassParentEnvs(boolean passParentEnvs);
Map<String, String> getEnvs();
void setEnvs(Map<String, String> envs);
@Nullable
PathMappingSettings getMappingSettings();
void setMappingSettings(@Nullable PathMappingSettings mappingSettings);
boolean shouldAddContentRoots();
boolean shouldAddSourceRoots();
void setAddContentRoots(boolean flag);
void setAddSourceRoots(boolean flag);
}

View File

@@ -148,7 +148,7 @@ public class SphinxBaseCommand {
ReSTService service = ReSTService.getInstance(module);
cmd.setWorkDirectory(service.getWorkdir().isEmpty()? module.getProject().getBaseDir().getPath(): service.getWorkdir());
PythonCommandLineState.createStandardGroupsIn(cmd);
PythonCommandLineState.createStandardGroups(cmd);
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
assert script_params != null;

View File

@@ -31,7 +31,8 @@ import java.util.Map;
public enum PythonHelpersLocator implements PythonHelper {
COVERAGEPY("", "coveragepy"), COVERAGE("run_coverage", "coverage"),
DEBUGGER("pydevd", "pydev"),
CONSOLE("pydevconsole", "pydev");
CONSOLE("pydevconsole", "pydev"),
RUN_IN_CONSOLE("pydev_run_in_console", "pydev");
@NotNull
private PathPythonHelper findModule(String moduleName, String path) {

View File

@@ -17,12 +17,15 @@ package com.jetbrains.python.console;
import com.google.common.collect.Maps;
import com.intellij.openapi.components.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.util.PathMappingSettings;
import com.intellij.util.containers.ComparatorUtil;
import com.intellij.util.xmlb.annotations.*;
import com.jetbrains.python.run.AbstractPyCommonOptionsForm;
import com.jetbrains.python.run.AbstractPythonRunConfigurationParams;
import com.jetbrains.python.run.PythonRunParams;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -84,7 +87,7 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
}
@Tag("console-settings")
public static class PyConsoleSettings {
public static class PyConsoleSettings implements PythonRunParams {
public String myCustomStartScript = PydevConsoleRunner.CONSOLE_START_COMMAND;
public String mySdkHome = null;
public String myInterpreterOptions = "";
@@ -104,7 +107,7 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
this.myCustomStartScript = myCustomStartScript;
}
public void apply(AbstractPyCommonOptionsForm form) {
public void apply(AbstractPythonRunConfigurationParams form) {
mySdkHome = form.getSdkHome();
myInterpreterOptions = form.getInterpreterOptions();
myEnvs = form.getEnvs();
@@ -129,7 +132,7 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
|| !myMappings.equals(form.getMappingSettings());
}
public void reset(Project project, AbstractPyCommonOptionsForm form) {
public void reset(Project project, AbstractPythonRunConfigurationParams form) {
form.setEnvs(myEnvs);
form.setInterpreterOptions(myInterpreterOptions);
form.setSdkHome(mySdkHome);
@@ -190,12 +193,12 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
}
@Attribute("add-content-roots")
public boolean addContentRoots() {
public boolean shouldAddContentRoots() {
return myAddContentRoots;
}
@Attribute("add-source-roots")
public boolean addSourceRoots() {
public boolean shouldAddSourceRoots() {
return myAddSourceRoots;
}
@@ -217,6 +220,11 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
mySdkHome = sdkHome;
}
@Override
public void setModule(Module module) {
setModuleName(module.getName());
}
public void setInterpreterOptions(String interpreterOptions) {
myInterpreterOptions = interpreterOptions;
}
@@ -225,6 +233,16 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
myUseModuleSdk = useModuleSdk;
}
@Override
public boolean isPassParentEnvs() {
return true; // doesn't make sense for console
}
@Override
public void setPassParentEnvs(boolean passParentEnvs) {
// doesn't make sense for console
}
public void setModuleName(String moduleName) {
myModuleName = moduleName;
}
@@ -233,6 +251,17 @@ public class PyConsoleOptions implements PersistentStateComponent<PyConsoleOptio
myEnvs = envs;
}
@Nullable
@Override
public PathMappingSettings getMappingSettings() {
return null;
}
@Override
public void setMappingSettings(@Nullable PathMappingSettings mappingSettings) {
}
public void setWorkingDirectory(String workingDirectory) {
myWorkingDirectory = workingDirectory;
}

View File

@@ -24,10 +24,10 @@ import com.intellij.execution.ExecutionHelper;
import com.intellij.execution.Executor;
import com.intellij.execution.configurations.EncodingEnvironmentUtil;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.ParamsGroup;
import com.intellij.execution.console.ConsoleHistoryController;
import com.intellij.execution.console.LanguageConsoleView;
import com.intellij.execution.console.ProcessBackedConsoleExecuteActionHandler;
import com.intellij.execution.process.CommandLineArgumentsProvider;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessOutputTypes;
@@ -59,7 +59,6 @@ import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
@@ -90,7 +89,6 @@ import com.jetbrains.python.debugger.PySourcePosition;
import com.jetbrains.python.remote.PyRemoteSdkAdditionalDataBase;
import com.jetbrains.python.remote.PyRemoteSdkCredentials;
import com.jetbrains.python.remote.PythonRemoteInterpreterManager;
import com.jetbrains.python.run.ProcessRunner;
import com.jetbrains.python.run.PythonCommandLineState;
import com.jetbrains.python.run.PythonTracebackFilter;
import com.jetbrains.python.sdk.PySdkUtil;
@@ -125,7 +123,7 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
public static final int PORTS_WAITING_TIMEOUT = 20000;
private Sdk mySdk;
private CommandLineArgumentsProvider myCommandLineArgumentsProvider;
private GeneralCommandLine myGeneralCommandLine;
protected int[] myPorts;
private PydevConsoleCommunication myPydevConsoleCommunication;
private PyConsoleProcessHandler myProcessHandler;
@@ -247,6 +245,7 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
/**
* Add requered ENV var to Python task to set its stdout charset to current project charset to allow it print correctly.
*
* @param envs map of envs to add variable
* @param project current project
*/
@@ -294,7 +293,7 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
assert myPorts != null;
myCommandLineArgumentsProvider = createCommandLineArgumentsProvider(mySdk, myEnvironmentVariables, myPorts);
myGeneralCommandLine = createCommandLine(mySdk, myEnvironmentVariables, myPorts);
try {
super.initAndRun();
@@ -340,7 +339,7 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
assert myPorts != null;
myCommandLineArgumentsProvider = createCommandLineArgumentsProvider(mySdk, myEnvironmentVariables, myPorts);
myGeneralCommandLine = createCommandLine(mySdk, myEnvironmentVariables, myPorts);
UIUtil.invokeLaterIfNeeded(new Runnable() {
@Override
@@ -377,35 +376,27 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
return ports;
}
protected CommandLineArgumentsProvider createCommandLineArgumentsProvider(final Sdk sdk,
final Map<String, String> environmentVariables,
protected GeneralCommandLine createCommandLine(@NotNull final Sdk sdk,
@NotNull final Map<String, String> environmentVariables,
int[] ports) {
final ArrayList<String> args = new ArrayList<String>();
args.add(sdk.getHomePath());
final String versionString = sdk.getVersionString();
if (versionString == null || !versionString.toLowerCase().contains("jython")) {
args.add("-u");
return doCreateConsoleCmdLine(ports, PythonHelpersLocator.CONSOLE);
}
args.add(FileUtil.toSystemDependentName(PythonHelpersLocator.getHelperPath(PYDEV_PYDEVCONSOLE_PY)));
@NotNull
protected GeneralCommandLine doCreateConsoleCmdLine(int[] ports, PythonHelpersLocator helpersLocator) {
PyConsoleOptions.PyConsoleSettings settings = PyConsoleOptions.getInstance(getProject()).getPythonConsoleSettings();
GeneralCommandLine cmd = PythonCommandLineState.createPythonCommandLine(getProject(), settings);
cmd.withWorkDirectory(getWorkingDir());
ParamsGroup group = cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
helpersLocator.addToGroup(group, cmd);
for (int port : ports) {
args.add(String.valueOf(port));
}
return new CommandLineArgumentsProvider() {
@Override
public String[] getArguments() {
return ArrayUtil.toStringArray(args);
group.addParameter(String.valueOf(port));
}
@Override
public boolean passParentEnvs() {
return false;
}
@Override
public Map<String, String> getAdditionalEnvs() {
return addDefaultEnvironments(sdk, environmentVariables, getProject());
}
};
return cmd;
}
@Override
@@ -421,19 +412,18 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
if (PySdkUtil.isRemote(mySdk)) {
PythonRemoteInterpreterManager manager = PythonRemoteInterpreterManager.getInstance();
if (manager != null) {
return createRemoteConsoleProcess(manager, myCommandLineArgumentsProvider.getArguments(),
myCommandLineArgumentsProvider.getAdditionalEnvs());
return createRemoteConsoleProcess(manager, myGeneralCommandLine.getParametersList().getArray(),
myGeneralCommandLine.getEnvironment());
}
throw new PythonRemoteInterpreterManager.PyRemoteInterpreterExecutionException();
}
else {
myCommandLine = myCommandLineArgumentsProvider.getCommandLineString();
Map<String, String> envs = myCommandLineArgumentsProvider.getAdditionalEnvs();
if (envs != null) {
EncodingEnvironmentUtil.fixDefaultEncodingIfMac(envs, getProject());
}
final Process server = ProcessRunner
.createProcess(getWorkingDir(), envs, myCommandLineArgumentsProvider.getArguments());
myCommandLine = myGeneralCommandLine.getCommandLineString();
Map<String, String> envs = myGeneralCommandLine.getEnvironment();
EncodingEnvironmentUtil.setLocaleEnvironmentIfMac(envs, myGeneralCommandLine.getCharset());
final Process server = myGeneralCommandLine.createProcess();
try {
myPydevConsoleCommunication = new PydevConsoleCommunication(getProject(), myPorts[0], server, myPorts[1]);
}
@@ -1007,7 +997,8 @@ public class PydevConsoleRunner extends AbstractConsoleRunnerWithHistory<PythonC
@Override
public void actionPerformed(AnActionEvent e) {
PydevConsoleRunner runner = PythonConsoleRunnerFactory.getInstance().createConsoleRunner(e.getData(CommonDataKeys.PROJECT), e.getData(LangDataKeys.MODULE));
PydevConsoleRunner runner =
PythonConsoleRunnerFactory.getInstance().createConsoleRunner(e.getData(CommonDataKeys.PROJECT), e.getData(LangDataKeys.MODULE));
runner.createNewConsole();
}
}

View File

@@ -54,8 +54,8 @@ public class PydevConsoleRunnerFactory extends PythonConsoleRunnerFactory {
String[] setupFragment;
PyConsoleOptions.PyConsoleSettings settingsProvider = PyConsoleOptions.getInstance(project).getPythonConsoleSettings();
Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module, settingsProvider.addContentRoots(),
settingsProvider.addSourceRoots());
Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module, settingsProvider.shouldAddContentRoots(),
settingsProvider.shouldAddSourceRoots());
if (mappingSettings != null) {
pythonPath = mappingSettings.convertToRemote(pythonPath);

View File

@@ -436,4 +436,10 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractRunConfig
}
return result;
}
@Override
public String getModuleName() {
Module module = getModule();
return module != null? module.getName() : null;
}
}

View File

@@ -34,6 +34,7 @@ import com.intellij.facet.Facet;
import com.intellij.facet.FacetManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
@@ -43,13 +44,13 @@ import com.intellij.openapi.roots.impl.libraries.LibraryImpl;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.PersistentLibraryKind;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.encoding.EncodingProjectManager;
import com.intellij.remote.RemoteProcessHandlerBase;
import com.intellij.util.PlatformUtils;
import com.intellij.util.containers.HashMap;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.console.PyDebugConsoleBuilder;
import com.jetbrains.python.debugger.PyDebugRunner;
import com.jetbrains.python.debugger.PyDebuggerOptionsProvider;
@@ -60,13 +61,13 @@ import com.jetbrains.python.sdk.PySdkUtil;
import com.jetbrains.python.sdk.PythonEnvUtil;
import com.jetbrains.python.sdk.PythonSdkAdditionalData;
import com.jetbrains.python.sdk.PythonSdkType;
import com.jetbrains.python.sdk.flavors.JythonSdkFlavor;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.charset.Charset;
import java.util.*;
/**
@@ -205,7 +206,7 @@ public abstract class PythonCommandLineState extends CommandLineState {
}
public GeneralCommandLine generateCommandLine(CommandLinePatcher[] patchers) throws ExecutionException {
public GeneralCommandLine generateCommandLine(CommandLinePatcher[] patchers) {
GeneralCommandLine commandLine = generateCommandLine();
if (patchers != null) {
for (CommandLinePatcher patcher : patchers) {
@@ -219,23 +220,31 @@ public abstract class PythonCommandLineState extends CommandLineState {
return PythonProcessRunner.createProcess(commandLine);
}
public GeneralCommandLine generateCommandLine() throws ExecutionException {
GeneralCommandLine commandLine = createCommandLine();
commandLine.withCharset(EncodingProjectManager.getInstance(myConfig.getProject()).getDefaultCharset());
setRunnerPath(commandLine);
// define groups
createStandardGroupsIn(commandLine);
public GeneralCommandLine generateCommandLine() {
GeneralCommandLine commandLine = createPythonCommandLine(myConfig.getProject(), myConfig);
buildCommandLineParameters(commandLine);
initEnvironment(commandLine);
customizeEnvironmentVars(commandLine.getEnvironment(), myConfig.isPassParentEnvs());
return commandLine;
}
private static GeneralCommandLine createCommandLine() {
@NotNull
public static GeneralCommandLine createPythonCommandLine(Project project, PythonRunParams config) {
GeneralCommandLine commandLine = generalCommandLine();
initEnvironment(project, commandLine, config);
commandLine.withCharset(EncodingProjectManager.getInstance(project).getDefaultCharset());
setRunnerPath(project, commandLine, config);
createStandardGroups(commandLine);
return commandLine;
}
public static GeneralCommandLine generalCommandLine() {
return PtyCommandLine.isEnabled() ? new PtyCommandLine() : new GeneralCommandLine();
}
@@ -246,7 +255,7 @@ public abstract class PythonCommandLineState extends CommandLineState {
*
* @param commandLine
*/
public static void createStandardGroupsIn(GeneralCommandLine commandLine) {
public static void createStandardGroups(GeneralCommandLine commandLine) {
ParametersList params = commandLine.getParametersList();
params.addParamsGroup(GROUP_EXE_OPTIONS);
params.addParamsGroup(GROUP_DEBUGGER);
@@ -255,7 +264,7 @@ public abstract class PythonCommandLineState extends CommandLineState {
params.addParamsGroup(GROUP_SCRIPT);
}
protected void initEnvironment(GeneralCommandLine commandLine) {
protected static void initEnvironment(Project project, GeneralCommandLine commandLine, PythonRunParams myConfig) {
Map<String, String> env = myConfig.getEnvs();
if (env == null) {
env = new HashMap<String, String>();
@@ -264,14 +273,15 @@ public abstract class PythonCommandLineState extends CommandLineState {
env = new HashMap<String, String>(env);
}
addPredefinedEnvironmentVariables(env, myConfig.isPassParentEnvs());
setupEncodingEnvs(env, commandLine.getCharset());
addCommonEnvironmentVariables(env);
commandLine.getEnvironment().clear();
commandLine.getEnvironment().putAll(env);
commandLine.setPassParentEnvironment(myConfig.isPassParentEnvs());
buildPythonPath(commandLine, myConfig.isPassParentEnvs());
buildPythonPath(project, commandLine, myConfig);
}
protected static void addCommonEnvironmentVariables(Map<String, String> env) {
@@ -279,19 +289,19 @@ public abstract class PythonCommandLineState extends CommandLineState {
env.put("PYCHARM_HOSTED", "1");
}
public void addPredefinedEnvironmentVariables(Map<String, String> envs, boolean passParentEnvs) {
final PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(myConfig.getInterpreterPath());
if (flavor != null) {
flavor.addPredefinedEnvironmentVariables(envs, myConfig.getProject());
}
public void customizeEnvironmentVars(Map<String, String> envs, boolean passParentEnvs) {
}
private void buildPythonPath(GeneralCommandLine commandLine, boolean passParentEnvs) {
Sdk pythonSdk = PythonSdkType.findSdkByPath(myConfig.getInterpreterPath());
private static void setupEncodingEnvs(Map<String, String> envs, Charset charset) {
PythonSdkFlavor.setupEncodingEnvs(envs, charset);
}
private static void buildPythonPath(Project project, GeneralCommandLine commandLine, PythonRunParams config) {
Sdk pythonSdk = PythonSdkType.findSdkByPath(config.getSdkHome());
if (pythonSdk != null) {
List<String> pathList = Lists.newArrayList(getAddedPaths(pythonSdk));
pathList.addAll(collectPythonPath());
initPythonPath(commandLine, passParentEnvs, pathList, myConfig.getInterpreterPath());
pathList.addAll(collectPythonPath(project, config));
initPythonPath(commandLine, config.isPassParentEnvs(), pathList, config.getSdkHome());
}
}
@@ -344,16 +354,14 @@ public abstract class PythonCommandLineState extends CommandLineState {
}
}
protected Collection<String> collectPythonPath() {
final Module module = myConfig.getModule();
Set<String> pythonPath = Sets.newHashSet(collectPythonPath(module, myConfig.shouldAddContentRoots(), myConfig.shouldAddSourceRoots()));
protected static Collection<String> collectPythonPath(Project project, PythonRunParams config) {
final Module module = getModule(project, config);
if (isDebug() && getSdkFlavor() instanceof JythonSdkFlavor) { //that fixes Jython problem changing sys.argv on execfile, see PY-8164
pythonPath.add(PythonHelpersLocator.getHelperPath("pycharm"));
pythonPath.add(PythonHelpersLocator.getHelperPath("pydev"));
return Sets.newHashSet(collectPythonPath(module, config.shouldAddContentRoots(), config.shouldAddSourceRoots()));
}
return pythonPath;
private static Module getModule(Project project, PythonRunParams config) {
return ModuleManager.getInstance(project).findModuleByName(config.getModuleName());
}
@NotNull
@@ -448,10 +456,24 @@ public abstract class PythonCommandLineState extends CommandLineState {
}
}
protected void setRunnerPath(GeneralCommandLine commandLine) throws ExecutionException {
String interpreterPath = getInterpreterPath();
protected static void setRunnerPath(Project project, GeneralCommandLine commandLine, PythonRunParams config) {
String interpreterPath = getInterpreterPath(project, config);
if (StringUtil.isNotEmpty(interpreterPath)) {
commandLine.setExePath(FileUtil.toSystemDependentName(interpreterPath));
}
}
@Nullable
public static String getInterpreterPath(Project project, PythonRunParams config) {
String sdkHome = config.getSdkHome();
if (config.isUseModuleSdk() || StringUtil.isEmpty(sdkHome)) {
Sdk sdk = PythonSdkType.findPythonSdk(getModule(project, config));
if (sdk == null) return null;
sdkHome = sdk.getHomePath();
}
return sdkHome;
}
protected String getInterpreterPath() throws ExecutionException {
String interpreterPath = myConfig.getInterpreterPath();

View File

@@ -24,21 +24,19 @@ import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.ParametersList;
import com.intellij.execution.configurations.ParamsGroup;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.process.CommandLineArgumentsProvider;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.jetbrains.python.PythonHelpersLocator;
import com.jetbrains.python.console.PyConsoleType;
import com.jetbrains.python.console.PydevConsoleRunner;
import com.jetbrains.python.sdk.PythonEnvUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -127,46 +125,22 @@ public class PythonScriptCommandLineState extends PythonCommandLineState {
}
@Override
protected CommandLineArgumentsProvider createCommandLineArgumentsProvider(final Sdk sdk,
final Map<String, String> environmentVariables,
protected GeneralCommandLine createCommandLine(@NotNull Sdk sdk,
@NotNull Map<String, String> environmentVariables,
int[] ports) {
final ArrayList<String> args = new ArrayList<String>();
args.add(sdk.getHomePath());
final String versionString = sdk.getVersionString();
if (versionString == null || !versionString.toLowerCase().contains("jython")) {
args.add("-u");
}
args.add(FileUtil.toSystemDependentName(PythonHelpersLocator.getHelperPath("pydev/pydev_run_in_console.py")));
for (int port : ports) {
args.add(String.valueOf(port));
}
GeneralCommandLine consoleCmdLine = doCreateConsoleCmdLine(ports, PythonHelpersLocator.RUN_IN_CONSOLE);
try {
final GeneralCommandLine cmd = generateCommandLine(myPatchers);
args.addAll(cmd.getParametersList().getList());
return new CommandLineArgumentsProvider() {
@Override
public String[] getArguments() {
return ArrayUtil.toStringArray(args);
}
ParamsGroup group = consoleCmdLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
assert group != null;
group.addParameters(cmd.getParametersList().getList());
@Override
public boolean passParentEnvs() {
return false;
}
PythonEnvUtil.mergePythonPath(consoleCmdLine.getEnvironment(), cmd.getEnvironment());
@Override
public Map<String, String> getAdditionalEnvs() {
Map<String, String> map = addDefaultEnvironments(sdk, environmentVariables,getProject());
map.putAll(cmd.getEnvironment());
return map;
}
};
}
catch (Exception e) {
throw new IllegalStateException(e);
}
consoleCmdLine.getEnvironment().putAll(cmd.getEnvironment());
return consoleCmdLine;
}
}
}

View File

@@ -139,7 +139,7 @@ public class PythonTask {
homePath = FileUtil.toSystemDependentName(homePath);
}
PythonCommandLineState.createStandardGroupsIn(cmd);
PythonCommandLineState.createStandardGroups(cmd);
ParamsGroup scriptParams = cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
assert scriptParams != null;

View File

@@ -86,6 +86,14 @@ public class PythonEnvUtil {
addPathToEnv(env, PYTHONPATH, value);
}
public static void mergePythonPath(@NotNull Map<String, String> from, @NotNull Map<String, String> to) {
String value = from.get(PYTHONPATH);
if (value != null) {
Set<String> paths = Sets.newHashSet(value.split(File.pathSeparator));
addToPythonPath(to, paths);
}
}
@NotNull
public static Map<String, String> setPythonDontWriteBytecode(@NotNull Map<String, String> env) {
env.put(PYTHONDONTWRITEBYTECODE, "1");

View File

@@ -208,10 +208,8 @@ public abstract class PythonSdkFlavor {
PythonEnvUtil.addPathToEnv(envs, key, value);
}
@SuppressWarnings({"MethodMayBeStatic"})
public void addPredefinedEnvironmentVariables(Map<String, String> envs, @NotNull Project project) {
Charset defaultCharset = EncodingProjectManager.getInstance(project).getDefaultCharset();
final String encoding = defaultCharset.name();
public static void setupEncodingEnvs(Map<String, String> envs, @NotNull Charset charset) {
final String encoding = charset.name();
PythonEnvUtil.setPythonIOEncoding(envs, encoding);
}

View File

@@ -122,18 +122,18 @@ public class PyRerunFailedTestsAction extends AbstractRerunFailedTestsAction {
}
@Override
protected void addAfterParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addAfterParameters(GeneralCommandLine cmd) {
myState.addAfterParameters(cmd);
}
@Override
protected void addBeforeParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addBeforeParameters(GeneralCommandLine cmd) {
myState.addBeforeParameters(cmd);
}
@Override
public void addPredefinedEnvironmentVariables(Map<String, String> envs, boolean passParentEnvs) {
myState.addPredefinedEnvironmentVariables(envs,
public void customizeEnvironmentVars(Map<String, String> envs, boolean passParentEnvs) {
myState.customizeEnvironmentVars(envs,
passParentEnvs);
}
}

View File

@@ -92,7 +92,7 @@ public abstract class PythonTestCommandLineStateBase extends PythonCommandLineSt
}
@Override
public GeneralCommandLine generateCommandLine() throws ExecutionException {
public GeneralCommandLine generateCommandLine() {
GeneralCommandLine cmd = super.generateCommandLine();
setWorkingDirectory(cmd);
@@ -154,10 +154,10 @@ public abstract class PythonTestCommandLineStateBase extends PythonCommandLineSt
return executionResult;
}
protected void addBeforeParameters(GeneralCommandLine cmd) throws ExecutionException {}
protected void addAfterParameters(GeneralCommandLine cmd) throws ExecutionException {}
protected void addBeforeParameters(GeneralCommandLine cmd) {}
protected void addAfterParameters(GeneralCommandLine cmd) {}
protected void addTestRunnerParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addTestRunnerParameters(GeneralCommandLine cmd) {
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert script_params != null;
script_params.addParameter(new File(PythonHelpersLocator.getHelpersRoot(), getRunner()).getAbsolutePath());
@@ -167,8 +167,8 @@ public abstract class PythonTestCommandLineStateBase extends PythonCommandLineSt
}
@Override
public void addPredefinedEnvironmentVariables(Map<String, String> envs, boolean passParentEnvs) {
super.addPredefinedEnvironmentVariables(envs, passParentEnvs);
public void customizeEnvironmentVars(Map<String, String> envs, boolean passParentEnvs) {
super.customizeEnvironmentVars(envs, passParentEnvs);
envs.put("PYCHARM_HELPERS_DIR", PythonHelpersLocator.getHelperPath("pycharm"));
}

View File

@@ -70,7 +70,7 @@ public class PythonNoseTestCommandLineState extends PythonTestCommandLineStateBa
}
@Override
protected void addAfterParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addAfterParameters(GeneralCommandLine cmd) {
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert script_params != null;
if (myConfig.useParam() && !StringUtil.isEmptyOrSpaces(myConfig.getParams()))

View File

@@ -43,7 +43,7 @@ public class PyTestCommandLineState extends PythonTestCommandLineStateBase {
}
@Override
protected void addBeforeParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addBeforeParameters(GeneralCommandLine cmd) {
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert script_params != null;
script_params.addParameters("-p", "pytest_teamcity");
@@ -62,7 +62,7 @@ public class PyTestCommandLineState extends PythonTestCommandLineStateBase {
}
@Override
protected void addAfterParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addAfterParameters(GeneralCommandLine cmd) {
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert script_params != null;
String params = myConfiguration.getParams();

View File

@@ -84,7 +84,7 @@ public class PythonUnitTestCommandLineState extends
}
@Override
protected void addAfterParameters(GeneralCommandLine cmd) throws ExecutionException {
protected void addAfterParameters(GeneralCommandLine cmd) {
ParamsGroup script_params = cmd.getParametersList().getParamsGroup(GROUP_SCRIPT);
assert script_params != null;
if (myConfig.useParam() && !StringUtil.isEmptyOrSpaces(myConfig.getParams()))