📄 simplerunner.java
字号:
/**
* Creates a string that can be passed as the PYTHONPATH
*
* @param project the project we want to get the settings from. If it is null, the system pythonpath is returned
* @param interpreter this is the interpreter to be used to create the env.
* @return a string that can be used as the PYTHONPATH env variable
*/
public static String makePythonPathEnvString(IProject project, String interpreter) {
List<String> paths;
if(project == null){
return ""; //no pythonpath can be gotten (set to empty, so that the default is gotten)
}
//if we have a project, get its complete pythonpath
IPythonPathNature pythonPathNature = PythonNature.getPythonPathNature(project);
if(pythonPathNature == null){
throw new RuntimeException("The project "+project.getName()+" does not have the pythonpath configured, \n" +
"please configure it correcly (please check the pydev faq at \n" +
"http://pydev.sf.net/faq.html for better information on how to do it).");
}
paths = pythonPathNature.getCompleteProjectPythonPath(interpreter);
String separator = getPythonPathSeparator();
StringBuffer pythonpath = new StringBuffer();
for (int i = 0; i < paths.size(); i++) {
if (i > 0){
pythonpath.append(separator);
}
String path = REF.getFileAbsolutePath(new File((String) paths.get(i)));
pythonpath.append(path);
}
return pythonpath.toString();
}
/**
* @return the separator for the pythonpath variables (system dependent)
*/
public static String getPythonPathSeparator(){
return System.getProperty( "path.separator" ); //is system dependent, and should cover for all cases...
// boolean win32= isWindowsPlatform();
// String separator = ";";
// if(!win32){
// separator = ":"; //system dependent
// }
// return separator;
}
/**
* @param env a map that will have its values formatted to xx=yy, so that it can be passed in an exec
* @return an array with the formatted map
*/
private String[] getMapEnvAsArray(Map<String,String> env) {
List<String> strings= new ArrayList<String>(env.size());
for(Iterator<Map.Entry<String, String>> iter= env.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, String> entry = iter.next();
StringBuffer buffer= new StringBuffer(entry.getKey());
buffer.append('=').append(entry.getValue());
strings.add(buffer.toString());
}
return strings.toArray(new String[strings.size()]);
}
/**
* shortcut
*/
public Tuple<String,String> runAndGetOutput(String executionString, File workingDir, IProgressMonitor monitor) {
return runAndGetOutput(executionString, workingDir, null, monitor);
}
/**
* shortcut
*/
public Tuple<String,String> runAndGetOutput(String executionString, File workingDir) {
return runAndGetOutput(executionString, workingDir, null, new NullProgressMonitor());
}
/**
* shortcut
*/
public Tuple<String,String> runAndGetOutput(String executionString, File workingDir, IProject project) {
return runAndGetOutput(executionString, workingDir, project, new NullProgressMonitor());
}
/**
* shortcut
*/
public Tuple<String,String> runAndGetOutput(String script, String[] args, File workingDir) {
return runAndGetOutput(script, args, workingDir, null);
}
/**
* This is the method that actually does the running (all others are just 'shortcuts' to this one).
*
* @param executionString this is the string that will be executed
* @param workingDir this is the directory where the execution will happen
* @param project this is the project that is related to the run (it is used to get the environment for the shell we are going to
* execute with the correct pythonpath environment variable).
* @param monitor this is the monitor used to communicate the progress to the user
*
* @return the string that is the output of the process (stdout) and the stderr (o2)
*/
public Tuple<String,String> runAndGetOutput(String executionString, File workingDir, IProject project, IProgressMonitor monitor) {
monitor.setTaskName("Executing: "+executionString);
monitor.worked(5);
Process process = null;
try {
monitor.setTaskName("Making pythonpath environment..."+executionString);
String[] envp = getEnvironment(project, null); //should get the environment for the default interpreter and the given project
monitor.setTaskName("Making exec..."+executionString);
if(workingDir != null){
if(!workingDir.isDirectory()){
throw new RuntimeException(StringUtils.format("Working dir must be an existing directory (received: %s)", workingDir));
}
}
process = Runtime.getRuntime().exec(executionString, envp, workingDir);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (process != null) {
try {
process.getOutputStream().close(); //we won't write to it...
} catch (IOException e2) {
}
monitor.setTaskName("Reading output...");
monitor.worked(5);
ThreadStreamReader std = new ThreadStreamReader(process.getInputStream());
ThreadStreamReader err = new ThreadStreamReader(process.getErrorStream());
std.start();
err.start();
try {
monitor.setTaskName("Waiting for process to finish.");
monitor.worked(5);
process.waitFor(); //wait until the process completion.
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
try {
//just to see if we get something after the process finishes (and let the other threads run).
synchronized (this) {
this.wait(50);
}
} catch (Exception e) {
//ignore
}
return new Tuple<String, String>(std.contents.toString(), err.contents.toString());
} else {
try {
throw new CoreException(PydevPlugin.makeStatus(IStatus.ERROR, "Error creating process - got null process("
+ executionString + ")", new Exception("Error creating process - got null process.")));
} catch (CoreException e) {
PydevPlugin.log(IStatus.ERROR, e.getMessage(), e);
}
}
return new Tuple<String, String>("","Error creating process - got null process("+ executionString + ")"); //no output
}
/**
* Execute the script specified with the interpreter for a given project
*
* @param script the script we will execute
* @param args the arguments to pass to the script
* @param workingDir the working directory
* @param project the project that is associated to this run
*
* @return a string with the output of the process (stdout)
*/
public abstract Tuple<String,String> runAndGetOutput(String script, String args[], File workingDir, IProject project);
/**
* @param pythonpath the pythonpath string to be used
* @return a list of strings with the elements of the pythonpath
*/
public static List<String> splitPythonpath(String pythonpath) {
ArrayList<String> splitted = new ArrayList<String>();
StringTokenizer tokenizer = new StringTokenizer(pythonpath, getPythonPathSeparator());
while(tokenizer.hasMoreTokens()){
splitted.add(tokenizer.nextToken());
}
return splitted;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -