pythonrunnerconfig.java
来自「Python Development Environment (Python I」· Java 代码 · 共 566 行 · 第 1/2 页
JAVA
566 行
/*
* Author: atotic
* Created on Mar 18, 2004
* License: Common Public License v1.0
*/
package org.python.pydev.debug.ui.launching;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.python.copiedfromeclipsesrc.JDTNotAvailableException;
import org.python.copiedfromeclipsesrc.JavaVmLocationFinder;
import org.python.pydev.core.IInterpreterManager;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.REF;
import org.python.pydev.debug.codecoverage.PyCoverage;
import org.python.pydev.debug.core.Constants;
import org.python.pydev.debug.core.PydevDebugPlugin;
import org.python.pydev.plugin.PydevPlugin;
import org.python.pydev.plugin.PydevPrefs;
import org.python.pydev.plugin.PyunitPrefsPage;
import org.python.pydev.plugin.SocketUtil;
import org.python.pydev.plugin.nature.PythonNature;
import org.python.pydev.runners.SimplePythonRunner;
import org.python.pydev.runners.SimpleRunner;
import org.python.pydev.ui.pythonpathconf.InterpreterInfo;
/**
* Holds configuration for PythonRunner.
*
* It knows how to extract proper launching arguments from disparate sources.
* Has many launch utility functions (getCommandLine & friends).
*/
public class PythonRunnerConfig {
public static final String RUN_COVERAGE = "RUN_COVERAGE";
public static final String RUN_REGULAR = "RUN_REGULAR";
public static final String RUN_UNITTEST = "RUN_UNITTEST";
public static final String RUN_JYTHON_UNITTEST = "RUN_JYTHON_UNITTEST";
public static final String RUN_JYTHON = "RUN_JYTHON";
public IProject project;
public IPath resource;
public IPath interpreter;
public String interpreterLocation;
private String arguments;
public File workingDirectory;
public String pythonpathUsed;
// debugging
public boolean isDebug;
public boolean isInteractive;
private int debugPort = 0; // use getDebugPort
public int acceptTimeout = 5000; // miliseconds
public String[] envp = null;
private String run;
private ILaunchConfiguration configuration;
public boolean isCoverage(){
return this.run.equals(RUN_COVERAGE);
}
public boolean isUnittest(){
return this.run.equals(RUN_UNITTEST) || this.run.equals(RUN_JYTHON_UNITTEST);
}
public boolean isJython(){
return this.run.equals(RUN_JYTHON) || this.run.equals(RUN_JYTHON_UNITTEST);
}
public boolean isFile() throws CoreException{
int resourceType = configuration.getAttribute(Constants.ATTR_RESOURCE_TYPE, -1);
return resourceType == IResource.FILE;
}
/**
* Expands and returns the location attribute of the given launch
* configuration. The location is
* verified to point to an existing file, in the local file system.
*
* @param configuration launch configuration
* @return an absolute path to a file in the local file system
* @throws CoreException if unable to retrieve the associated launch
* configuration attribute, if unable to resolve any variables, or if the
* resolved location does not point to an existing file in the local file
* system
*/
public static IPath getLocation(ILaunchConfiguration configuration) throws CoreException {
String location = configuration.getAttribute(Constants.ATTR_LOCATION, (String) null);
if (location == null) {
throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get location for run", null));
} else {
String expandedLocation = getStringVariableManager().performStringSubstitution(location);
if (expandedLocation == null || expandedLocation.length() == 0) {
throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get expanded location for run", null));
} else {
return new Path(expandedLocation);
}
}
}
/**
* Expands and returns the arguments attribute of the given launch
* configuration. Returns <code>null</code> if arguments are not specified.
*
* @param configuration launch configuration
* @return an array of resolved arguments, or <code>null</code> if
* unspecified
* @throws CoreException if unable to retrieve the associated launch
* configuration attribute, or if unable to resolve any variables
*/
public static String getArguments(ILaunchConfiguration configuration) throws CoreException {
return configuration.getAttribute(Constants.ATTR_PROGRAM_ARGUMENTS, (String) null);
}
/**
* Parses the argument text into an array of individual
* strings using the space character as the delimiter.
* An individual argument containing spaces must have a
* double quote (") at the start and end. Two double
* quotes together is taken to mean an embedded double
* quote in the argument text.
*
* @param arguments the arguments as one string
* @return the array of arguments
*/
public static String[] parseStringIntoList(String arguments) {
if (arguments == null || arguments.length() == 0) {
return new String[0];
}
String[] res= DebugPlugin.parseArguments(arguments);
return res;
}
private static IStringVariableManager getStringVariableManager() {
return VariablesPlugin.getDefault().getStringVariableManager();
}
/**
* Expands and returns the working directory attribute of the given launch
* configuration. Returns <code>null</code> if a working directory is not
* specified. If specified, the working is verified to point to an existing
* directory in the local file system.
*
* @param configuration launch configuration
* @return an absolute path to a directory in the local file system, or
* <code>null</code> if unspecified
* @throws CoreException if unable to retrieve the associated launch
* configuration attribute, if unable to resolve any variables, or if the
* resolved location does not point to an existing directory in the local
* file system
*/
public static IPath getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
String location = configuration.getAttribute(Constants.ATTR_WORKING_DIRECTORY, (String) null);
if (location != null) {
String expandedLocation = getStringVariableManager().performStringSubstitution(location);
if (expandedLocation.length() > 0) {
File path = new File(expandedLocation);
if (path.isDirectory()) {
return new Path(expandedLocation);
}
throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get working location for the run \n(the location: '"+expandedLocation+"' is not a valid directory).",null));
}
}
return null;
}
/**
* Returns the location of the selected interpreter in the launch configuration
* @param conf
* @return the string location of the selected interpreter in the launch configuration
* @throws CoreException if unable to retrieve the launch configuration attribute or if unable to
* resolve the default interpreter.
*/
private static String getInterpreterLocation(ILaunchConfiguration conf, IPythonNature nature) throws CoreException {
IInterpreterManager interpreterManager = PydevPlugin.getInterpreterManager(nature);
String location = conf.getAttribute(Constants.ATTR_INTERPRETER, Constants.ATTR_INTERPRETER_DEFAULT);
if (location != null && location.equals(Constants.ATTR_INTERPRETER_DEFAULT)){
location = interpreterManager.getDefaultInterpreter();
}else if(interpreterManager.hasInfoOnInterpreter(location) == false){
throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, "Error. The interprer: "+location+" is not configured in the pydev preferences as a '"+nature.getVersion()+"' interpreter (as required by the project:"+nature.getProject().getName()+").", null));
}
return location;
}
/**
* Expands and returns the python interprter attribute of the given launch
* configuration. The intepreter path is verified to point to an existing
* file in the local file system.
*
* @param configuration launch configuration
* @return an absolute path to the interpreter in the local file system
* @throws CoreException if unable to retrieve the associated launch
* configuration attribute, if unable to resolve any variables, or if the
* resolved location does not point to an existing directory in the local
* file system
*/
public static IPath getInterpreter(ILaunchConfiguration configuration, IPythonNature nature) throws CoreException {
String location = getInterpreterLocation(configuration, nature);
if (location == null) {
throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get python interpreter for run", null));
} else {
String expandedLocation = getStringVariableManager().performStringSubstitution(location);
if (expandedLocation == null || expandedLocation.length() == 0) {
throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get expanded interpreter for run", null));
} else {
return new Path(expandedLocation);
}
}
}
/**
* Sets defaults.
*/
public PythonRunnerConfig(ILaunchConfiguration conf, String mode, String run) throws CoreException {
this.configuration = conf;
this.run = run;
isDebug = mode.equals(ILaunchManager.DEBUG_MODE);
isInteractive = mode.equals("interactive");
resource = getLocation(conf);
arguments = getArguments(conf);
IPath workingPath = getWorkingDirectory(conf);
workingDirectory = workingPath == null ? null : workingPath.toFile();
acceptTimeout = PydevPrefs.getPreferences().getInt(PydevPrefs.CONNECT_TIMEOUT);
//find the project
IWorkspace w = ResourcesPlugin.getWorkspace();
String projName = conf.getAttribute(Constants.ATTR_PROJECT, "");
if (projName.length() == 0){
throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get project for the run",null));
}
project = w.getRoot().getProject(projName);
if(project == null){ //Ok, we could not find it out
CoreException e = PydevPlugin.log("Could not get project for resource: "+resource);
throw e;
}
// We need the project to find out the default interpreter from the InterpreterManager.
interpreterLocation = getInterpreterLocation(conf, (IPythonNature) project.getNature(PythonNature.PYTHON_NATURE_ID));
interpreter = getInterpreter(conf, (IPythonNature) project.getNature(PythonNature.PYTHON_NATURE_ID));
//make the environment
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
envp = launchManager.getEnvironment(conf);
if(envp == null){
//ok, the user has done nothing to the environment, just get all the default environment and
//put the pythonpath in it
envp = new SimplePythonRunner().getEnvironment(project, interpreterLocation);
pythonpathUsed = SimpleRunner.makePythonPathEnvString(project, interpreterLocation);
}else{
boolean win32= Platform.getOS().equals(org.eclipse.osgi.service.environment.Constants.OS_WIN32);
//ok, the user has done something to configure it, so, just add the pythonpath to the
//current env (if he still didn't do so)
Map envMap = conf.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map)null);
if(!specifiedPythonpath(envMap)){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?