[Pydev-cvs] org.python.pydev.debug/src/org/python/pydev/debug/ui/launching PythonRunner.java,NONE,1.
Brought to you by:
fabioz
From: Aleksandar T. <at...@us...> - 2004-03-29 17:18:55
|
Update of /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/ui/launching In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32567/src/org/python/pydev/debug/ui/launching Modified Files: PythonRunActionDelegate.java LaunchShortcut.java package.html PythonLaunchConfigurationDelegate.java Added Files: PythonRunner.java SocketUtil.java PythonRunnerConfig.java Log Message: Develpment of the debugger model Index: package.html =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/ui/launching/package.html,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** package.html 8 Jan 2004 22:47:21 -0000 1.1 --- package.html 29 Mar 2004 17:07:25 -0000 1.2 *************** *** 1,9 **** <body> ! Launch configurations: integration with the org.eclipse.debug LaunchConfiguration framework (Debug menus etc). <p>PythonRunActionDelegate gets called by the popup menu. <p>It creates LaunchShortcut, which displays the "How do you want to launch" dialog. ! <p>Finally, LaunchShortcut dells PythonLaunchConfigurationDelegate to launch the file. ! <p>PythonLaunchConfigurationDelegate has the smarts about generating the right command ! line, etc. </body> \ No newline at end of file --- 1,13 ---- <body> ! This package handles processing after user presses "Run" button on launch until ! the program gets launched.<p> + <p>It integrates with the org.eclipse.debug LaunchConfiguration framework (Debug menus etc). <p>PythonRunActionDelegate gets called by the popup menu. <p>It creates LaunchShortcut, which displays the "How do you want to launch" dialog. ! <p>Finally, LaunchShortcut tells PythonLaunchConfigurationDelegate to launch the file. ! <p>PythonLaunchConfigurationDelegate passes all the parameters to PythonRunnerConfig, ! <p>and creates PythonRunner. ! <p>PythonRunner launches the executable and connects it to internal debug model. ! <p> </body> \ No newline at end of file --- NEW FILE: PythonRunner.java --- /* * Author: atotic * Created on Mar 18, 2004 * License: Common Public License v1.0 */ package org.python.pydev.debug.ui.launching; import java.io.IOException; import java.net.*; import java.util.HashMap; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.model.IProcess; import org.python.pydev.debug.core.Constants; import org.python.pydev.debug.core.PydevDebugPlugin; import org.python.pydev.debug.model.PythonDebugTarget; import org.python.pydev.debug.model.RemoteDebugger; /** * Launches Python process, and connects it to Eclipse's debugger. * Waits for process to complete. * * Modelled after org.eclipse.jdt.internal.launching.StandardVMDebugger. */ public class PythonRunner { class DebugConnector implements Runnable { int port; int timeout; ServerSocket serverSocket; Socket socket; // what got accepted Exception e; boolean terminated; public DebugConnector(int port, int timeout) throws IOException { this.port = port; this.timeout = timeout; serverSocket = new ServerSocket(port); } Exception getException() { return e; } public Socket getSocket() { return socket; } public void stopListening() throws IOException { if (serverSocket != null) serverSocket.close(); terminated = true; } public void run() { try { serverSocket.setSoTimeout(timeout); socket = serverSocket.accept(); } catch (IOException e) { this.e = e; } } } /** * launches the debug configuration * @param config * @param launch * @param monitor * @throws CoreException */ public void run(PythonRunnerConfig config, ILaunch launch, IProgressMonitor monitor) throws CoreException, IOException { if (monitor == null) monitor = new NullProgressMonitor(); IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 5); subMonitor.beginTask("Launching python", 1); // Launch & connect to the debugger subMonitor.subTask("Finding free socket..."); DebugConnector server = new DebugConnector(config.getDebugPort(), config.acceptTimeout); subMonitor.worked(1); subMonitor.subTask("Constructing command_line..."); String[] cmdLine = config.getCommandLine(); subMonitor.worked(1); Thread connectThread = new Thread(server, "Pydev debug listener"); connectThread.start(); Process p = DebugPlugin.exec(cmdLine, config.workingDirectory); if (p == null) // TODO this might not be an error throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Could not execute python process. Was it cancelled?", null)); // Register the process with the debug plugin subMonitor.worked(2); subMonitor.subTask("Starting debugger..."); HashMap processAttributes = new HashMap(); processAttributes.put(IProcess.ATTR_PROCESS_TYPE, Constants.PROCESS_TYPE); processAttributes.put(IProcess.ATTR_CMDLINE, config.getCommandLineAsString()); IProcess process = DebugPlugin.newProcess(launch,p, config.file.lastSegment(), processAttributes); // Launch the debug listener on a thread, and wait until it completes while (connectThread.isAlive()) { if (monitor.isCanceled()) { server.stopListening(); p.destroy(); return; } try { p.exitValue(); // throws exception if process has terminated // process has terminated - stop waiting for a connection try { server.stopListening(); } catch (IOException e) { // expected } checkErrorMessage(process); } catch (IllegalThreadStateException e) { // expected while process is alive } try { Thread.sleep(100); } catch (InterruptedException e) { } } Exception ex = server.getException(); if (ex != null) { process.terminate(); p.destroy(); String message = "Unexpected error setting up the debugger"; if (ex instanceof SocketTimeoutException) message = "Timed out after " + Float.toString(config.acceptTimeout/1000) + " seconds while waiting for python script to connect."; throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, message, ex)); } // hook up debug model, and we are off & running RemoteDebugger debugger = new RemoteDebugger(server.getSocket()); PythonDebugTarget t = new PythonDebugTarget(launch, process, config.getRunningName(), debugger); Thread dt = new Thread(debugger, "Pydev remote debug connection"); dt.start(); } protected void checkErrorMessage(IProcess process) throws CoreException { String errorMessage= process.getStreamsProxy().getErrorStreamMonitor().getContents(); if (errorMessage.length() != 0) // TODO not sure if this is really an error throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Something got printed in the error stream", null)); } } Index: PythonRunActionDelegate.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/ui/launching/PythonRunActionDelegate.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PythonRunActionDelegate.java 8 Jan 2004 22:47:21 -0000 1.1 --- PythonRunActionDelegate.java 29 Mar 2004 17:07:25 -0000 1.2 *************** *** 18,23 **** * Implements "Run Python..." extension for org.eclipse.ui.popupMenus. * ! * <p>Lots of functionality borrowed from AntRunActionDelegate. ! * <p>Collects all the files, then passes everything off to LaunchShortcut * * @see org.python.pydev.debug.ui.launching.LaunchShortcut --- 18,22 ---- * Implements "Run Python..." extension for org.eclipse.ui.popupMenus. * ! * <p>Passes off the selected file to {@link org.python.pydev.debug.ui.launching.LaunchShortcut LaunchShortcut}. * * @see org.python.pydev.debug.ui.launching.LaunchShortcut Index: PythonLaunchConfigurationDelegate.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/ui/launching/PythonLaunchConfigurationDelegate.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** PythonLaunchConfigurationDelegate.java 16 Jan 2004 23:54:30 -0000 1.3 --- PythonLaunchConfigurationDelegate.java 29 Mar 2004 17:07:25 -0000 1.4 *************** *** 6,40 **** package org.python.pydev.debug.ui.launching; ! import java.io.File; ! import java.text.MessageFormat; ! import java.util.HashMap; ! import java.util.Map; - import org.python.pydev.debug.core.Constants; - import org.python.pydev.debug.ui.InterpreterEditor; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; - import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; - import org.eclipse.debug.core.ILaunchManager; - import org.eclipse.debug.core.ILaunchConfigurationType; - import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; - import org.eclipse.debug.core.model.IProcess; - // E3 import org.eclipse.debug.ui.CommonTab; - // E3 import org.eclipse.debug.ui.RefreshTab; import org.eclipse.ui.IWindowListener; - import org.eclipse.ui.IWorkbenchWindow; - import org.eclipse.ui.PlatformUI; import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil; ! import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; ! import org.eclipse.ui.externaltools.internal.program.launchConfigurations.BackgroundResourceRefresher; ! import org.eclipse.ui.externaltools.internal.program.launchConfigurations.ExternalToolsProgramMessages; import org.eclipse.ui.externaltools.internal.variable.ExpandVariableContext; ! ! import org.eclipse.jface.dialogs.MessageDialog; /** --- 6,24 ---- package org.python.pydev.debug.ui.launching; ! import java.io.IOException; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; + import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; import org.eclipse.ui.IWindowListener; import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil; ! // import org.eclipse.ui.externaltools.internal.program.launchConfigurations.BackgroundResourceRefresher; import org.eclipse.ui.externaltools.internal.variable.ExpandVariableContext; ! import org.python.pydev.debug.core.PydevDebugPlugin; /** *************** *** 50,172 **** { private static IWindowListener windowListener; - - /** - * A window listener that warns the user about any running programs when - * the workbench closes. Programs are killed when the VM exits. - */ - private class ProgramLaunchWindowListener implements IWindowListener { - public void windowActivated(IWorkbenchWindow window) { - } - public void windowDeactivated(IWorkbenchWindow window) { - } - public void windowClosed(IWorkbenchWindow window) { - IWorkbenchWindow windows[]= PlatformUI.getWorkbench().getWorkbenchWindows(); - if (windows.length > 1) { - // There are more windows still open. - return; - } - ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager(); - ILaunchConfigurationType programType= manager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE); - if (programType == null) { - return; - } - ILaunch launches[]= manager.getLaunches(); - ILaunchConfigurationType configType; - ILaunchConfiguration config; - for (int i = 0; i < launches.length; i++) { - try { - config= launches[i].getLaunchConfiguration(); - if (config == null) { - continue; - } - configType= config.getType(); - } catch (CoreException e) { - continue; - } - if (configType.equals(programType)) { - if (!launches[i].isTerminated()) { - MessageDialog.openWarning(window.getShell(), ExternalToolsProgramMessages.getString("ProgramLaunchDelegate.Workbench_Closing_1"), ExternalToolsProgramMessages.getString("ProgramLaunchDelegate.The_workbench_is_exiting")); //$NON-NLS-1$ //$NON-NLS-2$ - break; - } - } - } - } - public void windowOpened(IWorkbenchWindow window) { - } - } ! /* (non-Javadoc) ! * */ public void launch(ILaunchConfiguration conf, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { ! if (monitor.isCanceled()) ! return; ! ExpandVariableContext resourceContext = ExternalToolsUtil.getVariableContext(); ! // Get the basic parameters ! IPath location = ExternalToolsUtil.getLocation(conf, resourceContext); ! IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(conf, resourceContext); ! String interpreter = conf.getAttribute(Constants.ATTR_INTERPRETER, "python"); ! String[] arguments = ExternalToolsUtil.getArguments(conf, resourceContext); ! ! if (monitor.isCanceled()) ! return; ! ! // Set up the command-line arguments ! int cmdLineLength = 3; ! if (arguments != null) ! cmdLineLength += arguments.length; ! ! String[] cmdLine = new String[cmdLineLength]; ! cmdLine[0] = interpreter; ! // Next option is for unbuffered stdout, otherwise Eclipse will not see any output until done ! cmdLine[1] = InterpreterEditor.isJython(interpreter) ? "-i" : "-u"; ! cmdLine[2] = location.toOSString(); ! if (arguments != null) ! System.arraycopy(arguments, 0, cmdLine, 3, arguments.length); ! ! File workingDir = workingDirectory == null ? null : workingDirectory.toFile(); ! ! // E3 String[] envp = DebugPlugin.getDefault().getLaunchManager().getEnvironment(conf); ! ! if (monitor.isCanceled()) ! return; ! ! if (windowListener == null) { ! windowListener= new ProgramLaunchWindowListener(); ! PlatformUI.getWorkbench().addWindowListener(windowListener); ! } ! ! // Execute the process ! // E3 Process p = DebugPlugin.exec(cmdLine, workingDir, envp); ! //DebugPlugin.newProcess(launch, ) ! Process p = DebugPlugin.exec(cmdLine, workingDir); ! IProcess process = null; ! ! // add process type to process attributes ! Map processAttributes = new HashMap(); ! processAttributes.put(IProcess.ATTR_PROCESS_TYPE, Constants.PROCESS_TYPE); ! ! // org.eclipse.debug.internal.ui.views.console.ConsoleDocumentPartitioner.connect() attaches streams ! if (p != null) { ! monitor.beginTask(MessageFormat.format(ExternalToolsProgramMessages.getString("ProgramLaunchDelegate.3"), new String[] {conf.getName()}), IProgressMonitor.UNKNOWN); //$NON-NLS-1$ ! process = DebugPlugin.newProcess(launch, p, location.toOSString(), processAttributes); ! if (process == null) { ! p.destroy(); ! // E3 throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, IExternalToolConstants.ERR_INTERNAL_ERROR, ExternalToolsProgramMessages.getString("ProgramLaunchDelegate.4"), null)); //$NON-NLS-1$ ! throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, -1, ExternalToolsProgramMessages.getString("ProgramLaunchDelegate.4"), null)); //$NON-NLS-1$ ! } } ! process.setAttribute(IProcess.ATTR_CMDLINE, generateCommandLine(cmdLine)); ! if (ExternalToolsUtil.isBackground(conf)) { ! // refresh resources after process finishes ! if (ExternalToolsUtil.getRefreshScope(conf) != null) { ! BackgroundResourceRefresher refresher = new BackgroundResourceRefresher(conf, process, resourceContext); ! refresher.startBackgroundRefresh(); ! } ! // E3 if (CommonTab.isLaunchInBackground(conf)) { // E3 // refresh resources after process finishes --- 34,72 ---- { private static IWindowListener windowListener; ! /** ! * Launches the python process. ! * ! * Modelled after Ant & Java runners ! * see WorkbenchLaunchConfigurationDelegate::launch */ public void launch(ILaunchConfiguration conf, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { ! if (monitor == null) ! monitor = new NullProgressMonitor(); ! monitor.beginTask("Preparing configuration", 3); ! ExpandVariableContext resourceContext = ExternalToolsUtil.getVariableContext(); + PythonRunnerConfig runConfig = new PythonRunnerConfig(conf, mode, resourceContext); + PythonRunner runner = new PythonRunner(); ! monitor.worked(1); ! try { ! runner.run(runConfig, launch, monitor); ! } catch (IOException e) { ! e.printStackTrace(); ! throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Unexpected IO Exception in Pydev debugger", null)); } ! // ClassLoader save = cur.getContextClassLoader(); ! // cur.setContextClassLoader(getClass().getClassLoader()); ! // try { ! // PythonDebugClient test = new PythonDebugClient(); ! // test.init("localhost", 29000, -1, null, null, null); // do whatever needs the contextClassLoader ! // } catch (PythonDebugException e1) { ! // DebugPlugin.log(e1); ! // } finally { ! // cur.setContextClassLoader(save); ! // } // E3 if (CommonTab.isLaunchInBackground(conf)) { // E3 // refresh resources after process finishes *************** *** 175,223 **** // E3 refresher.startBackgroundRefresh(); // E3 } ! } else { ! // wait for process to exit ! while (!process.isTerminated()) { ! try { ! if (monitor.isCanceled()) { ! process.terminate(); ! break; ! } ! Thread.sleep(50); ! } catch (InterruptedException e) { ! } ! } ! ! // refresh resources // E3 RefreshTab.refreshResources(conf, monitor); - } - } - - private String generateCommandLine(String[] commandLine) { - if (commandLine.length < 1) - return ""; //$NON-NLS-1$ - StringBuffer buf= new StringBuffer(); - for (int i= 0; i < commandLine.length; i++) { - buf.append(' '); - char[] characters= commandLine[i].toCharArray(); - StringBuffer command= new StringBuffer(); - boolean containsSpace= false; - for (int j = 0; j < characters.length; j++) { - char character= characters[j]; - if (character == '\"') { - command.append('\\'); - } else if (character == ' ') { - containsSpace = true; - } - command.append(character); - } - if (containsSpace) { - buf.append('\"'); - buf.append(command); - buf.append('\"'); - } else { - buf.append(command); - } - } - return buf.toString(); } } --- 75,80 ---- // E3 refresher.startBackgroundRefresh(); // E3 } ! // refresh resources // E3 RefreshTab.refreshResources(conf, monitor); } } Index: LaunchShortcut.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/ui/launching/LaunchShortcut.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** LaunchShortcut.java 10 Jan 2004 03:13:18 -0000 1.2 --- LaunchShortcut.java 29 Mar 2004 17:07:25 -0000 1.3 *************** *** 92,96 **** status = new Status(IStatus.ERROR, "org.python.pydev.debug", 0, message, throwable); } ! ErrorDialog.openError(DebugPlugin.getActiveWorkbenchWindow().getShell(), "Python pydev.debug error", "Python launch failed", status); } --- 92,96 ---- status = new Status(IStatus.ERROR, "org.python.pydev.debug", 0, message, throwable); } ! ErrorDialog.openError(PydevDebugPlugin.getActiveWorkbenchWindow().getShell(), "Python pydev.debug error", "Python launch failed", status); } *************** *** 154,158 **** String baseDirectory = file.getRawLocation().removeLastSegments(1).toString(); String arguments = ""; ! String interpreter = DebugPlugin.getDefault().getInterpreters()[0]; workingCopy.setAttribute(Constants.ATTR_LOCATION,location); workingCopy.setAttribute(Constants.ATTR_WORKING_DIRECTORY,baseDirectory); --- 154,158 ---- String baseDirectory = file.getRawLocation().removeLastSegments(1).toString(); String arguments = ""; ! String interpreter = PydevDebugPlugin.getDefault().getInterpreters()[0]; workingCopy.setAttribute(Constants.ATTR_LOCATION,location); workingCopy.setAttribute(Constants.ATTR_WORKING_DIRECTORY,baseDirectory); *************** *** 223,227 **** IStatus status = new Status(IStatus.INFO, Constants.PLUGIN_ID, 0, "Hmm", null); //$NON-NLS-1$ String groupID = mode.equals("run") ? Constants.PYTHON_RUN_LAUNCH_GROUP : Constants.PYTHON_DEBUG_LAUNCH_GROUP; ! DebugUITools.openLaunchConfigurationDialog(DebugPlugin.getActiveWorkbenchWindow().getShell(), conf, groupID, null); } else { DebugUITools.launch(conf, mode); --- 223,227 ---- IStatus status = new Status(IStatus.INFO, Constants.PLUGIN_ID, 0, "Hmm", null); //$NON-NLS-1$ String groupID = mode.equals("run") ? Constants.PYTHON_RUN_LAUNCH_GROUP : Constants.PYTHON_DEBUG_LAUNCH_GROUP; ! DebugUITools.openLaunchConfigurationDialog(PydevDebugPlugin.getActiveWorkbenchWindow().getShell(), conf, groupID, null); } else { DebugUITools.launch(conf, mode); --- NEW FILE: SocketUtil.java --- /* * Author: atotic * Created on Mar 22, 2004 * License: Common Public License v1.0 */ package org.python.pydev.debug.ui.launching; import java.io.IOException; import java.net.ConnectException; import java.net.Socket; import java.util.Random; /** * Utility class to find a port to debug on. * * Straight copy of package org.eclipse.jdt.launching.SocketUtil. * I just could not figure out how to import that one. * No dependencies kept it on the classpath reliably */ public class SocketUtil { private static final Random fgRandom= new Random(System.currentTimeMillis()); /** * Returns a free port number on the specified host within the given range, * or -1 if none found. * * @param host name or IP addres of host on which to find a free port * @param searchFrom the port number from which to start searching * @param searchTo the port number at which to stop searching * @return a free port in the specified range, or -1 of none found */ public static int findUnusedLocalPort(String host, int searchFrom, int searchTo) { for (int i= 0; i < 10; i++) { Socket s= null; int port= getRandomPort(searchFrom, searchTo); try { s= new Socket(host, port); } catch (ConnectException e) { return port; } catch (IOException e) { } finally { if (s != null) { try { s.close(); } catch (IOException ioe) { } } } } return -1; } private static int getRandomPort(int low, int high) { return (int)(fgRandom.nextFloat() * (high-low)) + low; } } --- NEW FILE: PythonRunnerConfig.java --- /* * 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.net.URL; import java.util.Vector; 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.Status; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchManager; import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil; import org.eclipse.ui.externaltools.internal.variable.ExpandVariableContext; import org.python.pydev.debug.core.Constants; import org.python.pydev.debug.core.PydevDebugPlugin; import org.python.pydev.debug.ui.InterpreterEditor; /** * 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 IPath file; public String interpreter; public String[] arguments; public File workingDirectory; // debugging public boolean isDebug; private int debugPort = 0; // use getDebugPort public String debugScript; public int acceptTimeout = 5000; // miliseconds /** * Sets defaults. */ public PythonRunnerConfig(ILaunchConfiguration conf, String mode, ExpandVariableContext resourceContext) throws CoreException { isDebug = mode.equals(ILaunchManager.DEBUG_MODE); file = ExternalToolsUtil.getLocation(conf, resourceContext); interpreter = conf.getAttribute(Constants.ATTR_INTERPRETER, "python"); arguments = ExternalToolsUtil.getArguments(conf, resourceContext); IPath workingPath = ExternalToolsUtil.getWorkingDirectory(conf, resourceContext); workingDirectory = workingPath == null ? null : workingPath.toFile(); if (isDebug) { debugScript = getDebugScript(); // TODO debug socket port? } // E3 String[] envp = DebugPlugin.getDefault().getLaunchManager().getEnvironment(conf); } public int getDebugPort() throws CoreException { if (debugPort == 0) { debugPort= SocketUtil.findUnusedLocalPort("", 5000, 15000); //$NON-NLS-1$ if (debugPort == -1) throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Could not find a free socket for debugger", null)); } return debugPort; } public String getRunningName() { return file.lastSegment(); } /** * @throws CoreException if arguments are inconsistent */ public void verify() throws CoreException { if (file == null || interpreter == null) throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Invalid PythonRunnerConfig",null)); if (isDebug && ( acceptTimeout < 0 || debugPort < 0 || debugScript == null)) throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Invalid PythonRunnerConfig",null)); } /** * gets location of jpydaemon.py */ public static String getDebugScript() throws CoreException { IPath relative = new Path("pysrc").addTrailingSeparator().append("jpydaemon.py"); // IPath relative = new Path("pysrc").addTrailingSeparator().append("rpdb.py"); URL location = org.python.pydev.debug.core.PydevDebugPlugin.getDefault().find(relative); if (location == null) throw new CoreException(new Status(IStatus.ERROR, PydevDebugPlugin.getPluginID(), 0, "Internal pydev error: Cannot find jpydaemon.py", null)); return location.getPath(); } /** * Create a command line for launching. * @return command line ready to be exec'd */ public String[] getCommandLine() { Vector cmdArgs = new Vector(10); cmdArgs.add(interpreter); // Next option is for unbuffered stdout, otherwise Eclipse will not see any output until done cmdArgs.add(InterpreterEditor.isJython(interpreter) ? "-i" : "-u"); if (isDebug) { // rpdb cmdArgs.add(debugScript); // cmdArgs.add("-c"); // cmdArgs.add("-p"+Integer.toString(debugPort)); cmdArgs.add(debugScript); cmdArgs.add("localhost"); cmdArgs.add(Integer.toString(debugPort)); } cmdArgs.add(file.toOSString()); for (int i=0; arguments != null && i<arguments.length; i++) cmdArgs.add(arguments[i]); String[] retVal = new String[cmdArgs.size()]; cmdArgs.toArray(retVal); return retVal; } public String getCommandLineAsString() { String[] args = getCommandLine(); StringBuffer s = new StringBuffer(); for (int i=0; i< args.length; i++) { s.append(args[i]); s.append(" "); } return s.toString(); } } |