pythonrunnerconfig.java

来自「Python Development Environment (Python I」· Java 代码 · 共 566 行 · 第 1/2 页

JAVA
566
字号
	    		
	            String pythonpath = SimpleRunner.makePythonPathEnvString(project, interpreterLocation);
                pythonpathUsed = pythonpath; 
	            //override it if it was the ambient pythonpath
	            for (int i = 0; i < envp.length; i++) {
	                if(win32){
                        //case insensitive
		                if(envp[i].toUpperCase().startsWith("PYTHONPATH")){
		                    //OK, finish it.
				            envp[i] = "PYTHONPATH="+pythonpath;
		                    return;
		                }
	                }else{
		                if(envp[i].startsWith("PYTHONPATH")){
		                    //OK, finish it.
				            envp[i] = "PYTHONPATH="+pythonpath;
		                    return;
		                }
	                }
	                
	            }
	            
	            //there was no pythonpath, let's set it
	            String[] s = new String[envp.length+1];
	            System.arraycopy(envp, 0, s, 0, envp.length);
	            s[s.length-1] = "PYTHONPATH="+pythonpath;
	            envp = s;
		            
    		}
        }
	}
	
    /**
     * @param envMap
     * @return
     */
    private boolean specifiedPythonpath(Map envMap) {
        if(envMap == null){
            return false;
            
        }else{
    		boolean win32= Platform.getOS().equals(org.eclipse.osgi.service.environment.Constants.OS_WIN32);

            for (Iterator iter = envMap.keySet().iterator(); iter.hasNext();) {
                String s = (String) iter.next();

                if(win32){
                    if(s.toUpperCase().equals("PYTHONPATH")){
                        return true;
                    }
                }else{
                    if(s.equals("PYTHONPATH")){
                        return true;
                    }
                }
                
            }
        }
        
        return false;
    }

    public int getDebugPort() throws CoreException {
		if (debugPort == 0) {
			debugPort= SocketUtil.findUnusedLocalPort("", 5000, 15000); //$NON-NLS-1$
			if (debugPort == -1)
				throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Could not find a free socket for debugger", null));
		}
		return debugPort;		
	}


	public String getRunningName() {
		return resource.lastSegment();
	}

	/**
	 * @throws CoreException if arguments are inconsistent
	 */
	public void verify() throws CoreException {
		if (resource == null || interpreter == null){
		    throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Invalid PythonRunnerConfig",null));
        }
        
		if (isDebug && ( acceptTimeout < 0|| debugPort < 0) ){
		    throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Invalid PythonRunnerConfig",null));
        }
	}

	/**
     * @return
	 * @throws CoreException
     */
    public static String getCoverageScript() throws CoreException {
        return REF.getFileAbsolutePath(PydevDebugPlugin.getScriptWithinPySrc("coverage.py"));
    }

    public static String getUnitTestScript() throws CoreException {
        return REF.getFileAbsolutePath(PydevDebugPlugin.getScriptWithinPySrc("SocketTestRunner.py"));
    }

    /** 
	 * gets location of jpydaemon.py
	 */
	public static String getDebugScript() throws CoreException {
	    return REF.getFileAbsolutePath(PydevDebugPlugin.getScriptWithinPySrc("pydevd.py"));
	}

    private String getRunFilesScript() throws CoreException {
        return REF.getFileAbsolutePath(PydevDebugPlugin.getScriptWithinPySrc("runfiles.py"));
    }
    
    
	/**
	 * Create a command line for launching.
	 * @return command line ready to be exec'd
	 * @throws CoreException 
	 * @throws JDTNotAvailableException 
	 */
	public String[] getCommandLine() throws CoreException, JDTNotAvailableException {
		List<String> cmdArgs = new ArrayList<String>();
        
        if(isJython()){
            //"java.exe" -classpath "C:\bin\jython21\jython.jar" org.python.util.jython script %ARGS%
            String javaLoc = JavaVmLocationFinder.findDefaultJavaExecutable().getAbsolutePath();
            if(!InterpreterInfo.isJythonExecutable(interpreter.toOSString())){
                throw new RuntimeException("The jython jar must be specified as the interpreter to run. Found: "+interpreter);
            }
            cmdArgs.add(javaLoc);

            //some nice things on the classpath config: http://mindprod.com/jgloss/classpath.html
            cmdArgs.add("-classpath");
            String cpath;
            
            //TODO: add some option in the project so that the user can choose to use the
            //classpath specified in the java project instead of the pythonpath itself
            
//            if (project.getNature(Constants.JAVA_NATURE) != null){
//            	cpath  = getClasspath(JavaCore.create(project));
//            } else {
            	cpath = interpreter + SimpleRunner.getPythonPathSeparator() + pythonpathUsed;
//            }
            cmdArgs.add(cpath);
            cmdArgs.add("-Dpython.path="+pythonpathUsed); //will be added to the env variables in the run (check if this works on all platforms...)
            
           	addVmArgs(cmdArgs);
            	
            if (isDebug) {
            	cmdArgs.add("-Dpython.security.respectJavaAccessibility=false"); //TODO: the user should configure this -- we use it so that 
            																	 //we can access the variables during debugging. 
            	cmdArgs.add("org.python.util.jython");

            	cmdArgs.add(getDebugScript());
                cmdArgs.add("--vm_type");
                cmdArgs.add("jython");
                cmdArgs.add("--client");
                cmdArgs.add("localhost");
                cmdArgs.add("--port");
                cmdArgs.add(Integer.toString(debugPort));
                cmdArgs.add("--file");

            }else{
            	cmdArgs.add("org.python.util.jython");
            	
            }
            
            
            if(isUnittest()){
                cmdArgs.add(getRunFilesScript());
                cmdArgs.add("--verbosity");
                cmdArgs.add( PydevPrefs.getPreferences().getString(PyunitPrefsPage.PYUNIT_VERBOSITY) );
                
                String filter = PydevPrefs.getPreferences().getString(PyunitPrefsPage.PYUNIT_TEST_FILTER);
                if (filter.length() > 0) {
                    cmdArgs.add("--filter");
                    cmdArgs.add( filter );
                }
            }

        }else{
        
    		cmdArgs.add(interpreter.toOSString());
    		// Next option is for unbuffered stdout, otherwise Eclipse will not see any output until done
            if(isInteractive){
                cmdArgs.add("-i");
                
            }else{
                cmdArgs.add("-u");
            }
        
            addVmArgs(cmdArgs);
            
    		if (isDebug) {
    			cmdArgs.add(getDebugScript());
                cmdArgs.add("--vm_type");
                cmdArgs.add("python");
    			cmdArgs.add("--client");
    			cmdArgs.add("localhost");
    			cmdArgs.add("--port");
    			cmdArgs.add(Integer.toString(debugPort));
    			cmdArgs.add("--file");
    		}
    		
    		if(isCoverage()){
    			cmdArgs.add(getCoverageScript());
    			String coverageFileLocation = PyCoverage.getCoverageFileLocation();
                cmdArgs.add(coverageFileLocation);
    			cmdArgs.add("-x");
    			if (!isFile()){
    			    //run all testcases
                    cmdArgs.add(getRunFilesScript());
                }
    		}
    
    		if(isUnittest()){
                cmdArgs.add(getRunFilesScript());
                cmdArgs.add("--verbosity");
                cmdArgs.add( PydevPrefs.getPreferences().getString(PyunitPrefsPage.PYUNIT_VERBOSITY) );
                
                String filter = PydevPrefs.getPreferences().getString(PyunitPrefsPage.PYUNIT_TEST_FILTER);
                if (filter.length() > 0) {
	                cmdArgs.add("--filter");
	                cmdArgs.add( filter );
                }
    		}
        }
        
        if(!isInteractive){
            //wnen it is interactive, we don't have the resource
    		cmdArgs.add(resource.toOSString());
        }
        
        String runArguments[] = null;
        if (arguments != null) {
            String expanded = getStringVariableManager().performStringSubstitution(arguments);
            runArguments = parseStringIntoList(expanded);
        }

        for (int i=0; runArguments != null && i<runArguments.length; i++){
            cmdArgs.add(runArguments[i]);
        }
        
		String[] retVal = new String[cmdArgs.size()];
		cmdArgs.toArray(retVal);
		return retVal;
	}

    /**
     * @param cmdArgs
     * @throws CoreException
     */
    private void addVmArgs(List<String> cmdArgs) throws CoreException {
        String[] vmArguments = getVMArguments(configuration);
        if(vmArguments != null){
            for (int i = 0; i < vmArguments.length; i++){
            	cmdArgs.add(vmArguments[i]);
            }
        }
    }

	
	
    private String[] getVMArguments(ILaunchConfiguration configuration) throws CoreException {
    	String args = configuration.getAttribute(Constants.ATTR_VM_ARGUMENTS, (String) null);
        if (args != null && args.trim().length() > 0) {
            String expanded = getStringVariableManager().performStringSubstitution(args);
            return parseStringIntoList(expanded);
       }
       return null;
	}

	public String getCommandLineAsString() throws JDTNotAvailableException {
		String[] args;
        try {
            args = getCommandLine();
            return SimpleRunner.getCommandLineAsString(args);
        } catch (CoreException e) {
            throw new RuntimeException(e);
        }
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?