gt:             manager = PydevPlugin.getIronpythonInterpreterManager();
context: /**
   * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
   * Licensed under the terms of the Eclipse Public License (EPL).
   * Please see the license.txt included with this distribution for details.
   * Any modifications to this file must keep this entire header intact.
   */
  /*
   * Author: atotic
   * Created on Mar 18, 2004
   */
  package org.python.pydev.debug.ui.launching;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  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.VariablesPlugin;
  import org.eclipse.debug.core.DebugPlugin;
  import org.eclipse.debug.core.ILaunch;
  import org.eclipse.debug.core.ILaunchConfiguration;
  import org.eclipse.debug.core.ILaunchManager;
  import org.eclipse.jface.preference.IPreferenceStore;
  import org.python.copiedfromeclipsesrc.JDTNotAvailableException;
  import org.python.copiedfromeclipsesrc.JavaVmLocationFinder;
  import org.python.pydev.core.IInterpreterInfo;
  import org.python.pydev.core.IInterpreterManager;
  import org.python.pydev.core.IPythonNature;
  import org.python.pydev.core.MisconfigurationException;
  import org.python.pydev.core.PythonNatureWithoutProjectException;
  import org.python.pydev.core.docutils.StringSubstitution;
  import org.python.pydev.core.docutils.StringUtils;
  import org.python.pydev.core.log.Log;
  import org.python.pydev.debug.codecoverage.PyCodeCoverageView;
  import org.python.pydev.debug.codecoverage.PyCoverage;
  import org.python.pydev.debug.codecoverage.PyCoveragePreferences;
  import org.python.pydev.debug.core.Constants;
  import org.python.pydev.debug.core.PydevDebugPlugin;
  import org.python.pydev.debug.model.remote.ListenConnector;
  import org.python.pydev.debug.pyunit.PyUnitServer;
  import org.python.pydev.debug.ui.launching.PythonRunnerCallbacks.CreatedCommandLineParams;
  import org.python.pydev.editor.preferences.PydevEditorPrefs;
  import org.python.pydev.plugin.PydevPlugin;
  import org.python.pydev.plugin.nature.PythonNature;
  import org.python.pydev.plugin.preferences.PydevPrefs;
  import org.python.pydev.pyunit.preferences.PyUnitPrefsPage2;
  import org.python.pydev.runners.SimpleRunner;
  import org.python.pydev.ui.dialogs.PyDialogHelpers;
  import org.python.pydev.ui.pythonpathconf.InterpreterInfo;
  
  import com.aptana.shared_core.io.FileUtils;
  import com.aptana.shared_core.net.LocalHost;
  import com.aptana.shared_core.string.FastStringBuffer;
  import com.aptana.shared_core.structure.Tuple;
  import com.aptana.shared_core.utils.PlatformUtils;
  import com.aptana.shared_core.utils.RunInUiThread;
  
  /**
   * 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 = "python code coverage run";
      public static final String RUN_REGULAR = "python regular run";
      public static final String RUN_UNITTEST = "pyton unittest run";
      public static final String RUN_JYTHON_UNITTEST = "jython unittest run";
      public static final String RUN_JYTHON = "jython regular run";
      public static final String RUN_IRONPYTHON = "iron python regular run";
      public static final String RUN_IRONPYTHON_UNITTEST = "iron python unittest run";
  
      public final IProject project;
      public final IPath[] resource;
      public final IPath interpreter;
      public final IInterpreterInfo interpreterLocation;
      private final String arguments;
      public final File workingDirectory;
      public final String pythonpathUsed;
  
      // debugging
      public final boolean isDebug;
      public final boolean isInteractive;
      public int acceptTimeout = 5000; // miliseconds
      public String[] envp = null;
  
      /** One of RUN_ enums */
      public final String run;
      private final ILaunchConfiguration configuration;
      private ListenConnector listenConnector;
      private PyUnitServer pyUnitServer;
  
      //    public boolean isCoverage(){
      //        return this.run.equals(RUN_COVERAGE);
      //    }
  
      public boolean isUnittest() {
          return this.run.equals(RUN_UNITTEST) || this.run.equals(RUN_JYTHON_UNITTEST)
                  || this.run.equals(RUN_IRONPYTHON_UNITTEST);
      }
  
      public boolean isJython() {
          return this.run.equals(RUN_JYTHON) || this.run.equals(RUN_JYTHON_UNITTEST);
      }
  
      public boolean isIronpython() {
          return this.run.equals(RUN_IRONPYTHON) || this.run.equals(RUN_IRONPYTHON_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, IPythonNature nature) throws CoreException {
          String locationsStr = configuration.getAttribute(Constants.ATTR_ALTERNATE_LOCATION, (String) null);
          if (locationsStr == null) {
              locationsStr = configuration.getAttribute(Constants.ATTR_LOCATION, (String) null);
          }
          if (locationsStr == null) {
              throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get location for run", null));
          }
  
          List<String> locations = StringUtils.splitAndRemoveEmptyTrimmed(locationsStr, '|');
          Path[] ret = new Path[locations.size()];
          int i = 0;
          for (String location : locations) {
              String expandedLocation = getStringSubstitution(nature).performStringSubstitution(location);
              if (expandedLocation == null || expandedLocation.length() == 0) {
                  throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR,
                          "Unable to get expanded location for run", null));
              } else {
                  ret[i] = new Path(expandedLocation);
              }
              i++;
          }
          return ret;
      }
  
      /**
       * 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, boolean makeArgumentsVariableSubstitution)
              throws CoreException {
          String arguments = configuration.getAttribute(Constants.ATTR_PROGRAM_ARGUMENTS, "");
          if (makeArgumentsVariableSubstitution) {
              return VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(arguments);
          } else {
              return arguments;
          }
      }
  
      /**
       * 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 StringSubstitution getStringSubstitution(IPythonNature nature) {
          return new StringSubstitution(nature);
      }
  
      /**
       * 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, IPythonNature nature)
              throws CoreException {
          IProject project = nature.getProject();
          String location = configuration.getAttribute(Constants.ATTR_WORKING_DIRECTORY,
                  "${project_loc:/" + project.getName() + "}");
          if (location != null) {
              String expandedLocation = getStringSubstitution(nature).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.
       * @throws MisconfigurationException 
       */
      public static IInterpreterInfo getInterpreterLocation(ILaunchConfiguration conf, IPythonNature nature,
              IInterpreterManager interpreterManager) throws InvalidRunException, CoreException,
              MisconfigurationException {
          String location = conf.getAttribute(Constants.ATTR_INTERPRETER, Constants.ATTR_INTERPRETER_DEFAULT);
  
          if (location != null && location.equals(Constants.ATTR_INTERPRETER_DEFAULT)) {
              if (nature != null && nature.getInterpreterType() == interpreterManager.getInterpreterType()) {
  
                  //When both, the interpreter for the launch and the nature have the same type, let's get the
                  //launch location from the project
                  try {
                      return nature.getProjectInterpreter();
                  } catch (PythonNatureWithoutProjectException e) {
                      throw new RuntimeException(e);
                  }
  
              } else {
  
                  //When it doesn't have the same type it means that we're trying to run as jython a python
                  //project (or vice-versa), so, we must get the interpreter from the interpreter manager!
                  return interpreterManager.getDefaultInterpreterInfo(true);
              }
  
          } else {
              IInterpreterInfo interpreterInfo = interpreterManager.getInterpreterInfo(location, null);
              if (interpreterInfo != null) {
                  return interpreterInfo;
              } else {
                  File file = new File(location);
                  if (!file.exists()) {
                      throw new InvalidRunException("Error. The interprer: " + location + " does not exist");
  
                  } else {
                      //it does not have information on the given interpreter!!
                      if (nature == null) {
                          throw new InvalidRunException("Error. The interpreter: >>" + location
                                  + "<< is not configured in the pydev preferences as a valid interpreter (null nature).");
                      } else {
                          throw new InvalidRunException("Error. The interpreter: >>" + location
                                  + "<< is not configured in the pydev preferences as a valid '" + nature.getVersion()
                                  + "' interpreter.");
                      }
                  }
              }
          }
      }
  
      /**
       * Expands and returns the python interpreter attribute of the given launch
       * configuration. The interpreter 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
       * @throws InvalidRunException 
       */
      private IPath getInterpreter(IInterpreterInfo location, ILaunchConfiguration configuration, IPythonNature nature)
              throws CoreException, InvalidRunException {
          if (location == null) {
              throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR,
                      "Unable to get python interpreter for run", null));
          } else {
              String expandedLocation = getStringSubstitution(nature).performStringSubstitution(
                      location.getExecutableOrJar());
              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);
              }
          }
      }
  
      /**
       * Gets the project that should be used for a launch configuration
       * @param conf the launch configuration from where the project should be gotten
       * @return the related IProject
       * @throws CoreException
       */
      public static IProject getProjectFromConfiguration(ILaunchConfiguration conf) throws CoreException {
          String projName = conf.getAttribute(Constants.ATTR_PROJECT, "");
          if (projName == null || projName.length() == 0) {
              throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Unable to get project for the run",
                      null));
          }
  
          IWorkspace w = ResourcesPlugin.getWorkspace();
          IProject p = w.getRoot().getProject(projName);
          if (p == null || !p.exists()) { // Ok, we could not find it out
              throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Could not get project: " + projName,
                      null));
          }
          return p;
      }
  
      /**
       * Can be used to extract the pythonpath used from a given configuration.
       * 
       * @param conf the configuration from where we want to get the pythonpath
       * @return a string with the pythonpath used (with | as a separator)
       * @throws CoreException
       * @throws InvalidRunException 
       * @throws MisconfigurationException 
       */
      public static String getPythonpathFromConfiguration(ILaunchConfiguration conf, IInterpreterManager manager)
              throws CoreException, InvalidRunException, MisconfigurationException {
          IProject p = getProjectFromConfiguration(conf);
          PythonNature pythonNature = PythonNature.getPythonNature(p);
          if (pythonNature == null) {
              throw new CoreException(PydevDebugPlugin.makeStatus(IStatus.ERROR, "Project should have a python nature: "
                      + p.getName(), null));
          }
          IInterpreterInfo l = getInterpreterLocation(conf, pythonNature, manager);
          return SimpleRunner.makePythonPathEnvString(pythonNature, l, manager);
      }
  
      public PythonRunnerConfig(ILaunchConfiguration conf, String mode, String run) throws CoreException,
              InvalidRunException, MisconfigurationException {
          this(conf, mode, run, true);
      }
  
      /**
       * Sets defaults.
       * @throws InvalidRunException 
       * @throws MisconfigurationException 
       */
      @SuppressWarnings("unchecked")
      public PythonRunnerConfig(ILaunchConfiguration conf, String mode, String run,
              boolean makeArgumentsVariableSubstitution) throws CoreException, InvalidRunException,
              MisconfigurationException {
          //1st thing, see if this is a valid run.
          project = getProjectFromConfiguration(conf);
  
          if (project == null) { //Ok, we could not find it out
              throw Log.log("Could not get project for configuration: " + conf);
          }
  
          // We need the project to find out the default interpreter from the InterpreterManager.
          IPythonNature pythonNature = PythonNature.getPythonNature(project);
          if (pythonNature == null) {
              CoreException e = Log.log("No python nature for project: " + project.getName());
              throw e;
          }
  
          //now, go on configuring other things
          this.configuration = conf;
          this.run = run;
          isDebug = mode.equals(ILaunchManager.DEBUG_MODE);
          isInteractive = mode.equals("interactive");
  
          resource = getLocation(conf, pythonNature);
          arguments = getArguments(conf, makeArgumentsVariableSubstitution);
          IPath workingPath = getWorkingDirectory(conf, pythonNature);
          workingDirectory = workingPath == null ? null : workingPath.toFile();
          acceptTimeout = PydevPrefs.getPreferences().getInt(PydevEditorPrefs.CONNECT_TIMEOUT);
  
          interpreterLocation = getInterpreterLocation(conf, pythonNature, this.getRelatedInterpreterManager());
          interpreter = getInterpreter(interpreterLocation, conf, pythonNature);
  
          //make the environment
          ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
          envp = launchManager.getEnvironment(conf);
          IInterpreterManager manager;
          if (isJython()) {
              manager = PydevPlugin.getJythonInterpreterManager();
          } else if (isIronpython()) {
  