Example #1
0
  @Override
  protected Throwable exec(File f) {
    System.out.println(org.python.pydev.shared_core.string.StringUtils.format("Running: %s", f));
    Tuple<String, String> output =
        new SimpleIronpythonRunner()
            .runAndGetOutput(
                new String[] {
                  TestDependent.IRONPYTHON_EXE,
                  "-u",
                  IInterpreterManager.IRONPYTHON_DEFAULT_INTERNAL_SHELL_VM_ARGS,
                  FileUtils.getFileAbsolutePath(f)
                },
                f.getParentFile(),
                null,
                null,
                "utf-8");

    System.out.println(
        org.python.pydev.shared_core.string.StringUtils.format(
            "stdout:%s\nstderr:%s", output.o1, output.o2));

    if (output.o2.toLowerCase().indexOf("failed") != -1
        || output.o2.toLowerCase().indexOf("traceback") != -1) {
      throw new AssertionError(output.toString());
    }
    return null;
  }
Example #2
0
  /** Gets completions for jedi library (https://github.com/davidhalter/jedi) */
  public List<CompiledToken> getJediCompletions(
      File editorFile, PySelection ps, String charset, List<String> pythonpath) throws Exception {

    FastStringBuffer read = null;
    String str =
        StringUtils.join(
            "|",
            new String[] {
              String.valueOf(ps.getCursorLine()),
              String.valueOf(ps.getCursorColumn()),
              charset,
              FileUtils.getFileAbsolutePath(editorFile),
              StringUtils.replaceNewLines(ps.getDoc().get(), "\n")
            });

    str = URLEncoder.encode(str, ENCODING_UTF_8);

    try (AutoCloseable permit = acquire("getJediCompletions")) {
      internalChangePythonPath(pythonpath);
      read = this.writeAndGetResults("@@MSG_JEDI:", str, "\nEND@@");
    }

    Tuple<String, List<String[]>> theCompletions = ShellConvert.convertStringToCompletions(read);
    ArrayList<CompiledToken> lst = new ArrayList<>(theCompletions.o2.size());
    for (String[] s : theCompletions.o2) {
      // new CompiledToken(rep, doc, args, parentPackage, type);
      lst.add(new CompiledToken(s[0], s[1], "", "", Integer.parseInt(s[3])));
    }
    return lst;
  }
  public void run(IAction action) {
    PyEdit pyEdit = getPyEdit();
    PySelection ps = new PySelection(pyEdit);
    if (ps.getSelLength() != 0) {
      return;
    }
    try {
      IDocument doc = ps.getDoc();
      char c = doc.getChar(ps.getAbsoluteCursorOffset() - 1);
      boolean opening = StringUtils.isOpeningPeer(c);
      boolean closing = org.python.pydev.shared_core.string.StringUtils.isClosingPeer(c);

      if (opening || closing) {
        PythonPairMatcher matcher = new PythonPairMatcher();
        IRegion match = matcher.match(doc, ps.getAbsoluteCursorOffset());
        if (match != null) {
          if (closing) {
            pyEdit.setSelection(match.getOffset() + 1, 0);
          } else { // opening
            pyEdit.setSelection(match.getOffset() + match.getLength(), 0);
          }
        }
      }
    } catch (BadLocationException e) {
      return;
    }
  }
 /**
  * We want to apply it on \n or on '.'
  *
  * <p>When . is entered, the user will finish (and apply) the current completion and request a new
  * one with '.'
  *
  * <p>If not added, it won't request the new one (and will just stop the current)
  */
 @Override
 public char[] getTriggerCharacters() {
   char[] chars = VAR_TRIGGER;
   if (PyCodeCompletionPreferencesPage.applyCompletionOnLParen()) {
     chars = StringUtils.addChar(chars, '(');
   }
   if (PyCodeCompletionPreferencesPage.applyCompletionOnRParen()) {
     chars = StringUtils.addChar(chars, ')');
   }
   return chars;
 }
Example #5
0
  /*
   * 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;
  }
Example #6
0
 /** @return a tuple with the process created and a string representation of the cmdarray. */
 public Tuple<Process, String> run(
     String[] cmdarray, File workingDir, IPythonNature nature, IProgressMonitor monitor) {
   if (monitor == null) {
     monitor = new NullProgressMonitor();
   }
   String executionString = getArgumentsAsStr(cmdarray);
   monitor.setTaskName("Executing: " + executionString);
   monitor.worked(5);
   Process process = null;
   try {
     monitor.setTaskName("Making pythonpath environment..." + executionString);
     String[] envp = null;
     if (nature != null) {
       envp =
           getEnvironment(
               nature,
               nature.getProjectInterpreter(),
               nature.getRelatedInterpreterManager()); // Don't remove as it *should* be updated
       // based on the nature)
     }
     // Otherwise, use default (used when configuring the interpreter for instance).
     monitor.setTaskName("Making exec..." + executionString);
     if (workingDir != null) {
       if (!workingDir.isDirectory()) {
         throw new RuntimeException(
             org.python.pydev.shared_core.string.StringUtils.format(
                 "Working dir must be an existing directory (received: %s)", workingDir));
       }
     }
     process = createProcess(cmdarray, envp, workingDir);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
   return new Tuple<Process, String>(process, executionString);
 }
Example #7
0
 public void update(String customFilters) {
   List<String> splittedCustomFilters = StringUtils.splitAndRemoveEmptyTrimmed(customFilters, ',');
   StringMatcherSimple[] temp = new StringMatcherSimple[splittedCustomFilters.size()];
   for (int i = 0; i < temp.length; i++) {
     temp[i] = new StringMatcherSimple(splittedCustomFilters.get(i).trim());
   }
   filters = temp;
 }
Example #8
0
 public void testRead() throws IOException {
   File file =
       new File(
           TestDependent.TEST_PYDEV_REFACTORING_PLUGIN_LOC + "/tests/python/utils/smallfile.txt");
   String contents = FileUtils.read(file);
   contents = StringUtils.replaceNewLines(contents, "\n");
   assertEquals("This\nis\na\nsmall\ntext\nfile.", contents);
 }
 /**
  * Saves the current list of commands in the preferences, adding the one passed as a parameter if
  * it is not null.
  */
 private void saveCurrentCommands(String newCommand) {
   ArrayList<String> newCommands = new ArrayList<String>(input);
   if (newCommand != null && !input.contains(newCommand)) {
     newCommands.add(newCommand);
   }
   newCommands.remove(NEW_ENTRY_TEXT); // never save this entry.
   preferenceStore.setValue(
       preferenceKey, org.python.pydev.shared_core.string.StringUtils.join("|", newCommands));
 }
Example #10
0
 private void compare(Integer[] is, ArrayList<Integer> offsets) {
   for (int i = 0; i < is.length; i++) {
     if (!is[i].equals(offsets.get(i))) {
       fail(
           org.python.pydev.shared_core.string.StringUtils.format(
               "%s != %s (%s)",
               is[i], offsets.get(i), Arrays.deepToString(is) + " differs from " + offsets));
     }
   }
 }
 @Override
 public String getOutgoing() {
   if (file != null) {
     return makeCommand(
         AbstractDebuggerCommand.CMD_IGNORE_THROWN_EXCEPTION_AT,
         sequence,
         StringUtils.join("|", FileUtils.getFileAbsolutePath(file), this.lineNumber));
   } else {
     // Bulk-creation
     Collection<String> ignoreThrownExceptions =
         PyExceptionBreakPointManager.getInstance()
             .ignoreCaughtExceptionsWhenThrownFrom
             .getIgnoreThrownExceptions();
     return makeCommand(
         AbstractDebuggerCommand.CMD_IGNORE_THROWN_EXCEPTION_AT,
         sequence,
         "REPLACE:" + StringUtils.join("||", ignoreThrownExceptions));
   }
 }
Example #12
0
  public static Map<Integer, String> loadDictFrom(
      FastBufferedReader reader, FastStringBuffer buf, ObjectsPoolMap objectsPoolMap)
      throws IOException {
    int size = StringUtils.parsePositiveInt(reader.readLine());
    HashMap<Integer, String> map = new HashMap<Integer, String>(size + 5);

    FastStringBuffer line;
    int val = 0;
    while (true) {
      line = reader.readLine();
      if (line == null) {
        return map;

      } else if (line.startsWith("-- ")) {
        if (line.startsWith("-- END DICTIONARY")) {
          return map;
        }
        throw new RuntimeException("Unexpected line: " + line);

      } else {
        int length = line.length();
        // line is str=int
        for (int i = 0; i < length; i++) {
          char c = line.charAt(i);
          if (c == '=') {
            val = StringUtils.parsePositiveInt(buf);
            buf.clear();
          } else {
            buf.appendResizeOnExc(c);
          }
        }
        String bufStr = buf.toString();
        String interned = objectsPoolMap.get(bufStr);
        if (interned == null) {
          interned = bufStr;
          objectsPoolMap.put(bufStr, bufStr);
        }
        map.put(val, interned);
        buf.clear();
      }
    }
  }
Example #13
0
  /**
   * @return list with tuples: new String[]{token, description}
   * @throws CoreException
   */
  public Tuple<String, List<String[]>> getImportCompletions(String str, List<String> pythonpath)
      throws Exception {
    FastStringBuffer read = null;

    str = URLEncoder.encode(str, ENCODING_UTF_8);

    try (AutoCloseable permit = acquire(StringUtils.join("", "getImportCompletions: ", str))) {
      internalChangePythonPath(pythonpath);
      read = this.writeAndGetResults("@@IMPORTS:", str, "\nEND@@");
    }
    return ShellConvert.convertStringToCompletions(read);
  }
  protected File getFileStructure(File file, String withoutLastPart) {
    File f = file;
    List<String> split = StringUtils.dotSplit(withoutLastPart);
    int size = split.size();
    for (int i = 0; i < size; i++) {
      if (i == size - 1) {
        f = new File(f, split.get(i) + FileTypesPreferencesPage.getDefaultDottedPythonExtension());

      } else {
        f = new File(f, split.get(i));
      }
    }
    return f;
  }
Example #15
0
 @Override
 public String getDocStr() {
   if (javaElement instanceof IMember) {
     IMember member = (IMember) javaElement;
     try {
       return StringUtils.extractTextFromHTML(extractJavadoc(member, new NullProgressMonitor()));
     } catch (JavaModelException e) {
       // just ignore it in this case (that may happen when no docstring is available)
     } catch (Exception e) {
       Log.log("Error getting completion for " + member, e);
     }
   }
   return null;
 }
  /**
   * The line delimiters must match the platform for the bolds to be correct, so, in this function
   * we remove the ones existing and add the ones dependent on the platform
   */
  protected String correctLineDelimiters(String str) {
    FastStringBuffer buf = new FastStringBuffer();
    for (String s : StringUtils.splitInLines(str)) {

      boolean found = false;
      while (s.endsWith("\r") || s.endsWith("\n")) {
        found = true;
        s = s.substring(0, s.length() - 1);
      }
      buf.append(s);
      if (found) {
        buf.append(LINE_DELIM);
      }
    }
    str = buf.toString();
    return str;
  }
Example #17
0
  /** @param pythonpath */
  private void internalChangePythonPath(List<String> pythonpath) throws Exception {
    if (finishedForGood) {
      throw new RuntimeException(
          "Shells are already finished for good, so, it is an invalid state to try to change its dir.");
    }
    synchronized (lockLastPythonPath) {
      String pythonpathStr =
          StringUtils.join("|", pythonpath.toArray(new String[pythonpath.size()]));

      if (lastPythonPath != null && lastPythonPath.equals(pythonpathStr)) {
        return;
      }
      // Note: ignore results
      writeAndGetResults(
          "@@CHANGE_PYTHONPATH:", URLEncoder.encode(pythonpathStr, ENCODING_UTF_8), "\nEND@@");
      lastPythonPath = pythonpathStr;
    }
  }
  /**
   * @param preferenceStore the store from where we should get the preferences key.
   * @param preferenceKey this is a key where the value is a string with substrings separated by
   *     '|'.
   * @param shellMementoId the id for saving the memento settings for the dialog.
   */
  public SelectExistingOrCreateNewDialog(
      Shell parent, IPreferenceStore preferenceStore, String preferenceKey, String shellMementoId) {

    super(parent, new ToStringLabelProvider(), new StringFromListContentProvider());
    this.memento = new DialogMemento(parent, shellMementoId);
    this.preferenceStore = preferenceStore;

    final String initialValue = preferenceStore.getString(preferenceKey);

    this.preferenceKey = preferenceKey;

    this.setInput(StringUtils.split(initialValue, '|'));
    this.setAllowMultiple(false);
    this.setValidator(createValidator());
    this.setHelpAvailable(false);
    // as we have many special things about deleting, filtering in this class, it's important that
    // elements are up to date.
    this.updateInThread = false;
  }
Example #19
0
  /**
   * @param paths the paths to be added
   * @return a String suitable to be added to the PYTHONPATH environment variable.
   */
  public static String makePythonPathEnvFromPaths(Collection<String> inPaths) {
    ArrayList<String> paths = new ArrayList<String>(inPaths);
    try {
      // whenever we launch a file from pydev, we must add the sitecustomize to the pythonpath so
      // that
      // the default encoding (for the console) can be set.
      // see:
      // http://sourceforge.net/tracker/index.php?func=detail&aid=1580766&group_id=85796&atid=577329

      paths.add(
          0,
          FileUtils.getFileAbsolutePath(PydevPlugin.getScriptWithinPySrc("pydev_sitecustomize")));
    } catch (CoreException e) {
      Log.log(e);
    }

    String separator = getPythonPathSeparator();
    return org.python.pydev.shared_core.string.StringUtils.join(separator, paths);
  }
Example #20
0
  /** Actually remove the python nature from the project. */
  public void run(IAction action) {
    if (selectedProject == null) {
      return;
    }

    if (!MessageDialog.openConfirm(
        null,
        "Confirm Remove Pydev Nature",
        org.python.pydev.shared_core.string.StringUtils.format(
            "Are you sure that you want to remove the Pydev nature from %s?",
            selectedProject.getName()))) {
      return;
    }

    try {
      PythonNature.removeNature(selectedProject, null);
    } catch (Throwable e) {
      Log.log(e);
    }
  }
Example #21
0
  private FastStringBuffer writeAndGetResults(String... str) throws CoreException {
    try {
      synchronized (ioLock) {
        this.write(StringUtils.join("", str));
        FastStringBuffer read = this.read();
        return read;
      }
    } catch (NullPointerException e) {
      // still not started...
      restartShell();
      return null;

    } catch (Exception e) {
      if (DebugSettings.DEBUG_CODE_COMPLETION) {
        Log.log(IStatus.ERROR, "ERROR getting completions.", e);
      }

      restartShell();
      return null;
    }
  }
Example #22
0
  public static void buildKeysForZipContents(
      PyPublicTreeMap<ModulesKey, ModulesKey> keys, ZipContents zipContents) {
    for (String filePathInZip : zipContents.foundFileZipPaths) {
      String modName = StringUtils.stripExtension(filePathInZip).replace('/', '.');
      if (DEBUG_ZIP) {
        System.out.println("Found in zip:" + modName);
      }
      ModulesKey k = new ModulesKeyForZip(modName, zipContents.zipFile, filePathInZip, true);
      keys.put(k, k);

      if (zipContents.zipContentsType == ZipContents.ZIP_CONTENTS_TYPE_JAR) {
        // folder modules are only created for jars (because for python files, the __init__.py is
        // required).
        for (String s :
            new FullRepIterable(
                FullRepIterable.getWithoutLastPart(
                    modName))) { // the one without the last part was already added
          k = new ModulesKeyForZip(s, zipContents.zipFile, s.replace('.', '/'), false);
          keys.put(k, k);
        }
      }
    }
  }
Example #23
0
  /**
   * @param systemModulesManager
   * @param workspaceMetadataFile
   * @throws IOException
   */
  public static void loadFromFile(ModulesManager modulesManager, File workspaceMetadataFile)
      throws IOException {
    if (workspaceMetadataFile.exists() && !workspaceMetadataFile.isDirectory()) {
      throw new IOException("Expecting: " + workspaceMetadataFile + " to be a directory.");
    }
    File modulesKeysFile = new File(workspaceMetadataFile, "modulesKeys");
    File pythonpatHelperFile = new File(workspaceMetadataFile, "pythonpath");
    if (!modulesKeysFile.isFile()) {
      throw new IOException("Expecting: " + modulesKeysFile + " to exist (and be a file).");
    }
    if (!pythonpatHelperFile.isFile()) {
      throw new IOException("Expecting: " + pythonpatHelperFile + " to exist (and be a file).");
    }

    String fileContents = FileUtils.getFileContents(modulesKeysFile);
    if (!fileContents.startsWith(MODULES_MANAGER_V2)) {
      throw new RuntimeException(
          "Could not load modules manager from " + modulesKeysFile + " (version changed).");
    }

    HashMap<Integer, String> intToString = new HashMap<Integer, String>();
    fileContents = fileContents.substring(MODULES_MANAGER_V2.length());
    if (fileContents.startsWith("--COMMON--\n")) {
      String header = fileContents.substring("--COMMON--\n".length());
      header = header.substring(0, header.indexOf("--END-COMMON--\n"));
      fileContents =
          fileContents.substring(
              fileContents.indexOf("--END-COMMON--\n") + "--END-COMMON--\n".length());

      for (String line : StringUtils.iterLines(header)) {
        line = line.trim();
        List<String> split = StringUtils.split(line, '=');
        if (split.size() == 2) {
          try {
            int i = Integer.parseInt(split.get(0));
            intToString.put(i, split.get(1));
          } catch (NumberFormatException e) {
            Log.log(e);
          }
        }
      }
      if (fileContents.startsWith(MODULES_MANAGER_V2)) {
        fileContents = fileContents.substring(MODULES_MANAGER_V2.length());
      }
    }

    handleFileContents(modulesManager, fileContents, intToString);

    if (modulesManager.pythonPathHelper == null) {
      throw new IOException(
          "Pythonpath helper not properly restored. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }
    modulesManager.pythonPathHelper.loadFromFile(pythonpatHelperFile);

    if (modulesManager.pythonPathHelper.getPythonpath() == null) {
      throw new IOException(
          "Pythonpath helper pythonpath not properly restored. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }

    if (modulesManager.pythonPathHelper.getPythonpath().size() == 0) {
      throw new IOException(
          "Pythonpath helper pythonpath restored with no contents. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }

    if (modulesManager.modulesKeys.size()
        < 2) { // if we have few modules, that may indicate a problem...
      // if the project is really small, modulesManager will be fast, otherwise, it'll fix the
      // problem.
      // Note: changed to a really low value because we now make a check after it's restored
      // anyways.
      throw new IOException(
          "Only "
              + modulesManager.modulesKeys.size()
              + " modules restored in I/O. "
              + modulesManager.getClass().getName()
              + " dir:"
              + workspaceMetadataFile);
    }
  }
Example #24
0
  /**
   * This method returns the module that corresponds to the path passed as a parameter.
   *
   * @param name the name of the module we're looking for (e.g.: mod1.mod2)
   * @param dontSearchInit is used in a negative form because initially it was isLookingForRelative,
   *     but it actually defines if we should look in __init__ modules too, so, the name matches the
   *     old signature.
   *     <p>NOTE: isLookingForRelative description was: when looking for relative imports, we don't
   *     check for __init__
   * @return the module represented by this name
   */
  protected IModule getModule(
      boolean acceptCompiledModule, String name, IPythonNature nature, boolean dontSearchInit) {
    synchronized (lockTemporaryModules) {
      SortedMap<Integer, IModule> map = temporaryModules.get(name);
      if (map != null && map.size() > 0) {
        if (DEBUG_TEMPORARY_MODULES) {
          System.out.println("Returning temporary module: " + name);
        }
        return map.get(map.lastKey());
      }
    }
    AbstractModule n = null;
    ModulesKey keyForCacheAccess = new ModulesKey(null, null);

    if (!dontSearchInit) {
      if (n == null) {
        keyForCacheAccess.name =
            (String) StringUtils.join(".", new String[] {name, "__init__"}, null);
        n = cache.getObj(keyForCacheAccess, this);
        if (n != null) {
          name = keyForCacheAccess.name;
        }
      }
    }
    if (n == null) {
      keyForCacheAccess.name = name;
      n = cache.getObj(keyForCacheAccess, this);
    }

    if (n instanceof SourceModule) {
      // ok, module exists, let's check if it is synched with the filesystem version...
      SourceModule s = (SourceModule) n;
      if (!s.isSynched()) {
        // change it for an empty and proceed as usual.
        n = (AbstractModule) addModule(createModulesKey(s.getName(), s.getFile()));
      }
    }

    if (n instanceof EmptyModule) {
      EmptyModule e = (EmptyModule) n;

      if (e.f != null) {

        if (!e.f.exists()) {
          // if the file does not exist anymore, just remove it.
          keyForCacheAccess.name = name;
          keyForCacheAccess.file = e.f;
          doRemoveSingleModule(keyForCacheAccess);
          n = null;

        } else {
          // file exists
          n = checkOverride(name, nature, n);

          if (n instanceof EmptyModule) {
            // ok, handle case where the file is actually from a zip file...
            if (e instanceof EmptyModuleForZip) {
              EmptyModuleForZip emptyModuleForZip = (EmptyModuleForZip) e;

              if (emptyModuleForZip.pathInZip.endsWith(".class") || !emptyModuleForZip.isFile) {
                // handle java class... (if it's a class or a folder in a jar)
                n = JythonModulesManagerUtils.createModuleFromJar(emptyModuleForZip);
                n = decorateModule(n, nature);

              } else if (FileTypesPreferencesPage.isValidDll(emptyModuleForZip.pathInZip)) {
                // .pyd
                n = new CompiledModule(name, this);
                n = decorateModule(n, nature);

              } else if (PythonPathHelper.isValidSourceFile(emptyModuleForZip.pathInZip)) {
                // handle python file from zip... we have to create it getting the contents from the
                // zip file
                try {
                  IDocument doc =
                      FileUtilsFileBuffer.getDocFromZip(
                          emptyModuleForZip.f, emptyModuleForZip.pathInZip);
                  // NOTE: The nature (and so the grammar to be used) must be defined by this
                  // modules
                  // manager (and not by the initial caller)!!
                  n =
                      AbstractModule.createModuleFromDoc(
                          name, emptyModuleForZip.f, doc, this.getNature(), false);
                  SourceModule zipModule = (SourceModule) n;
                  zipModule.zipFilePath = emptyModuleForZip.pathInZip;
                  n = decorateModule(n, nature);
                } catch (Exception exc1) {
                  Log.log(exc1);
                  n = null;
                }
              }

            } else {
              // regular case... just go on and create it.
              try {
                // NOTE: The nature (and so the grammar to be used) must be defined by this modules
                // manager (and not by the initial caller)!!
                n = AbstractModule.createModule(name, e.f, this.getNature(), true);
                n = decorateModule(n, nature);
              } catch (IOException exc) {
                keyForCacheAccess.name = name;
                keyForCacheAccess.file = e.f;
                doRemoveSingleModule(keyForCacheAccess);
                n = null;
              } catch (MisconfigurationException exc) {
                Log.log(exc);
                n = null;
              }
            }
          }
        }

      } else { // ok, it does not have a file associated, so, we treat it as a builtin (this can
               // happen in java jars)
        n = checkOverride(name, nature, n);
        if (n instanceof EmptyModule) {
          if (acceptCompiledModule) {
            n = new CompiledModule(name, this);
            n = decorateModule(n, nature);
          } else {
            return null;
          }
        }
      }

      if (n != null) {
        doAddSingleModule(createModulesKey(name, e.f), n);
      } else {
        Log.log(("The module " + name + " could not be found nor created!"));
      }
    }

    if (n instanceof EmptyModule) {
      throw new RuntimeException("Should not be an empty module anymore: " + n);
    }
    if (n instanceof SourceModule) {
      SourceModule sourceModule = (SourceModule) n;
      // now, here's a catch... it may be a bootstrap module...
      if (sourceModule.isBootstrapModule()) {
        // if it's a bootstrap module, we must replace it for the related compiled module.
        n = new CompiledModule(name, this);
        n = decorateModule(n, nature);
      }
    }

    return n;
  }
Example #25
0
 private String escapeSlashes(String text) {
   return org.python.pydev.shared_core.string.StringUtils.replaceAll(text, "\\", "\\\\\\\\");
 }
Example #26
0
  /*
   * (non-Javadoc)
   * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
   */
  public void initializeFrom(ILaunchConfiguration configuration) {

    try {
      String id = configuration.getType().getIdentifier();
      IInterpreterManager manager = null;
      if (Constants.ID_JYTHON_LAUNCH_CONFIGURATION_TYPE.equals(id)
          || Constants.ID_JYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE.equals(id)) {
        manager = PydevPlugin.getJythonInterpreterManager();

      } else if (Constants.ID_IRONPYTHON_LAUNCH_CONFIGURATION_TYPE.equals(id)
          || Constants.ID_IRONPYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE.equals(id)) {
        manager = PydevPlugin.getIronpythonInterpreterManager();

      } else if (Constants.ID_PYTHON_REGULAR_LAUNCH_CONFIGURATION_TYPE.equals(id)
          || Constants.ID_PYTHON_COVERAGE_LAUNCH_CONFIGURATION_TYPE.equals(id)
          || Constants.ID_PYTHON_UNITTEST_LAUNCH_CONFIGURATION_TYPE.equals(id)) {
        manager = PydevPlugin.getPythonInterpreterManager();
      } else {
        // Get from the project
        try {
          // could throw core exception if project does not exist.
          IProject project = PythonRunnerConfig.getProjectFromConfiguration(configuration);
          PythonNature nature = PythonNature.getPythonNature(project);
          if (nature != null) {
            manager = PydevPlugin.getInterpreterManager(nature);
          }
        } catch (Exception e) {
          Log.log(e);
        }

        if (manager == null) {
          Log.log("Could not recognize: '" + id + "' using default python interpreter manager.");
          manager = PydevPlugin.getPythonInterpreterManager();
        }
      }
      String pythonPath = PythonRunnerConfig.getPythonpathFromConfiguration(configuration, manager);

      fPythonPathList.removeAll();
      java.util.List<String> paths = SimpleRunner.splitPythonpath(pythonPath);
      for (String p : paths) {
        fPythonPathList.add(p);
      }
      setErrorMessage(null);
    } catch (Exception e) {
      // Exceptions here may have several reasons
      // - The interpreter is incorrectly configured
      // - The arguments use an unresolved variable.
      // In each case, the exception contains a meaningful message, that is displayed
      Log.log(e);

      String message = e.getMessage();
      if (message == null) {
        message = "null (see error log for the traceback).";
      }
      String errorMsg =
          org.python.pydev.shared_core.string.StringUtils.replaceNewLines(message, " ");

      fPythonPathList.removeAll();
      fPythonPathList.add(errorMsg);
      setErrorMessage(errorMsg);
    }
  }
Example #27
0
  public static PyPublicTreeMap<String, Set<IInfo>> loadTreeFrom(
      final FastBufferedReader reader,
      final Map<Integer, String> dictionary,
      FastStringBuffer buf,
      ObjectsPoolMap objectsPoolMap,
      IPythonNature nature)
      throws IOException {
    PyPublicTreeMap<String, Set<IInfo>> tree = new PyPublicTreeMap<String, Set<IInfo>>();
    final int size = StringUtils.parsePositiveInt(reader.readLine());

    try {

      final Entry[] entries = new Entry[size];
      // each line is something as: cub|CubeColourDialog!13&999@CUBIC!263@cube!202&999@
      // note: the path (2nd int in record) is optional
      for (int iEntry = 0; iEntry < size; iEntry++) {
        buf.clear();
        FastStringBuffer line = reader.readLine();
        if (line == null || line.startsWith("-- ")) {
          throw new RuntimeException("Unexpected line: " + line);
        }
        char[] internalCharsArray = line.getInternalCharsArray();
        int length = line.length();
        String key = null;
        String infoName = null;
        String path = null;

        int i = 0;

        OUT:
        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '|':
              key = ObjectsInternPool.internLocal(objectsPoolMap, buf.toString());
              buf.clear();
              i++;
              break OUT;
            default:
              buf.appendResizeOnExc(c);
          }
        }

        int hashSize = 0;
        OUT2:
        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '|':
              hashSize = StringUtils.parsePositiveInt(buf);
              buf.clear();
              i++;
              break OUT2;
            default:
              buf.appendResizeOnExc(c);
          }
        }
        HashSet<IInfo> set = new HashSet<IInfo>(hashSize);

        for (; i < length; i++) {
          char c = internalCharsArray[i];
          switch (c) {
            case '!':
              infoName = ObjectsInternPool.internLocal(objectsPoolMap, buf.toString());
              buf.clear();
              break;

            case '&':
              path = dictionary.get(StringUtils.parsePositiveInt(buf));
              buf.clear();
              break;

            case '@':
              int dictKey = StringUtils.parsePositiveInt(buf);
              byte type = (byte) dictKey;
              type &=
                  0x07; // leave only the 3 least significant bits there (this is the type -- value
              // from 0 - 8).

              dictKey =
                  (dictKey
                      >> 3); // the entry in the dict doesn't have the least significant bits there.
              buf.clear();
              String moduleDeclared = dictionary.get(dictKey);
              if (moduleDeclared == null) {
                throw new AssertionError("Unable to find key: " + dictKey);
              }
              if (infoName == null) {
                throw new AssertionError("Info name may not be null. Line: " + line);
              }
              switch (type) {
                case IInfo.CLASS_WITH_IMPORT_TYPE:
                  set.add(new ClassInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.METHOD_WITH_IMPORT_TYPE:
                  set.add(new FuncInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
                  set.add(new AttrInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.NAME_WITH_IMPORT_TYPE:
                  set.add(new NameInfo(infoName, moduleDeclared, path, false, nature));
                  break;
                case IInfo.MOD_IMPORT_TYPE:
                  set.add(new ModInfo(infoName, false, nature));
                  break;
                default:
                  Log.log("Unexpected type: " + type);
              }
              break;
            default:
              buf.appendResizeOnExc(c);
          }
        }

        entries[iEntry] = new MapEntry(key, set);
      }

      tree.buildFromSorted(
          size,
          new Iterator() {
            private int iNext;

            @Override
            public boolean hasNext() {
              return iNext > size;
            }

            @Override
            public Object next() {
              Object o = entries[iNext];
              iNext++;
              return o;
            }

            @Override
            public void remove() {}
          },
          null,
          null);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    return tree;
  }
Example #28
0
  /**
   * 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()) {
      manager = PydevPlugin.getIronpythonInterpreterManager();
    } else {
      manager = PydevPlugin.getPythonInterpreterManager();
    }

    boolean win32 = PlatformUtils.isWindowsPlatform();

    if (envp == null) {
      // ok, the user has done nothing to the environment, just get all the default environment
      // which has the pythonpath in it
      envp = SimpleRunner.getEnvironment(pythonNature, interpreterLocation, manager);

    } else {
      // 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);

      String pythonpath =
          SimpleRunner.makePythonPathEnvString(pythonNature, interpreterLocation, manager);
      updateVar(pythonNature, manager, win32, envMap, "PYTHONPATH", pythonpath);
      if (isJython()) {
        // Also update the classpath env variable.
        updateVar(pythonNature, manager, win32, envMap, "CLASSPATH", pythonpath);
        // And the jythonpath env variable
        updateVar(pythonNature, manager, win32, envMap, "JYTHONPATH", pythonpath);

      } else if (isIronpython()) {
        // Also update the ironpythonpath env variable.
        updateVar(pythonNature, manager, win32, envMap, "IRONPYTHONPATH", pythonpath);
      }

      // And we also must get the environment variables specified in the interpreter manager.
      envp = interpreterLocation.updateEnv(envp, envMap.keySet());
    }

    boolean hasDjangoNature = project.hasNature(PythonNature.DJANGO_NATURE_ID);

    String settingsModule = null;
    Map<String, String> variableSubstitution = null;
    final String djangoSettingsKey = "DJANGO_SETTINGS_MODULE";
    String djangoSettingsEnvEntry = null;
    try {
      variableSubstitution = pythonNature.getPythonPathNature().getVariableSubstitution();
      settingsModule = variableSubstitution.get(djangoSettingsKey);
      if (settingsModule != null) {
        if (settingsModule.trim().length() > 0) {
          djangoSettingsEnvEntry = djangoSettingsKey + "=" + settingsModule.trim();
        }
      }
    } catch (Exception e1) {
      Log.log(e1);
    }
    if (djangoSettingsEnvEntry == null && hasDjangoNature) {
      // Default if not specified (only add it if the nature is there).
      djangoSettingsEnvEntry = djangoSettingsKey + "=" + project.getName() + ".settings";
    }

    // Note: set flag even if not debugging as the user may use remote-debugging later on.
    boolean geventSupport =
        DebugPrefsPage.getGeventDebugging()
            && pythonNature.getInterpreterType() == IPythonNature.INTERPRETER_TYPE_PYTHON;

    // Now, set the pythonpathUsed according to what's in the environment.
    String p = "";
    for (int i = 0; i < envp.length; i++) {
      String s = envp[i];
      Tuple<String, String> tup = StringUtils.splitOnFirst(s, '=');
      String var = tup.o1;
      if (win32) {
        // On windows it doesn't matter, always consider uppercase.
        var = var.toUpperCase();
      }

      if (var.equals("PYTHONPATH")) {
        p = tup.o2;

      } else if (var.equals(djangoSettingsKey)) {
        // Update it.
        if (djangoSettingsEnvEntry != null) {
          envp[i] = djangoSettingsEnvEntry;
          djangoSettingsEnvEntry = null;
        }
      }

      if (geventSupport) {
        if (var.equals("GEVENT_SUPPORT")) {
          // Flag already set in the environment
          geventSupport = false;
        }
      }
    }

    // Still not added, let's do that now.
    if (djangoSettingsEnvEntry != null) {
      envp = StringUtils.addString(envp, djangoSettingsEnvEntry);
    }
    if (geventSupport) {
      envp = StringUtils.addString(envp, "GEVENT_SUPPORT=True");
    }
    this.pythonpathUsed = p;
  }
Example #29
0
 public PydevRootPrefs() {
   setDescription(
       org.python.pydev.shared_core.string.StringUtils.format(
           "PyDev version: %s", PydevPlugin.version));
 }
Example #30
0
  public List<IMessage> getMessages(
      SourceModule module,
      IDocument document,
      IProgressMonitor monitor,
      IAnalysisPreferences prefs) {
    try {

      if (prefs.getSeverityForType(IAnalysisPreferences.TYPE_PEP8) < IMarker.SEVERITY_WARNING) {
        return messages;
      }
      this.prefs = prefs;
      this.document = document;
      messageToIgnore = prefs.getRequiredMessageToIgnore(IAnalysisPreferences.TYPE_PEP8);
      File pep8Loc = JythonModules.getPep8Location();

      if (pep8Loc == null) {
        Log.log("Unable to get pep8 module.");
        return messages;
      }

      IAdaptable projectAdaptable = prefs.getProjectAdaptable();
      if (AnalysisPreferencesPage.useSystemInterpreter(projectAdaptable)) {
        String parameters = AnalysisPreferencesPage.getPep8CommandLineAsStr(projectAdaptable);
        String output =
            PyFormatStd.runWithPep8BaseScript(document.get(), parameters, "pep8.py", "");
        List<String> splitInLines = StringUtils.splitInLines(output, false);

        for (String line : splitInLines) {
          try {
            List<String> lst = StringUtils.split(line, ':', 4);
            int lineNumber = Integer.parseInt(lst.get(1));
            int offset = Integer.parseInt(lst.get(2)) - 1;
            String text = lst.get(3);
            this.reportError(lineNumber, offset, text, null);
          } catch (Exception e) {
            Log.log("Error parsing line: " + line, e);
          }
        }
        return messages;
      }

      String[] pep8CommandLine = AnalysisPreferencesPage.getPep8CommandLine(projectAdaptable);
      FastStringBuffer args = new FastStringBuffer(pep8CommandLine.length * 20);
      for (String string : pep8CommandLine) {
        args.append(',').append("r'").append(string).append('\'');
      }

      // It's important that the interpreter is created in the Thread and not outside the thread
      // (otherwise
      // it may be that the output ends up being shared, which is not what we want.)
      boolean useConsole = AnalysisPreferencesPage.useConsole(projectAdaptable);
      IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(useConsole, false);
      String file = StringUtils.replaceAllSlashes(module.getFile().getAbsolutePath());
      interpreter.set("visitor", this);

      List<String> splitInLines = StringUtils.splitInLines(document.get());
      interpreter.set("lines", splitInLines);
      PyObject tempReportError = reportError;
      if (tempReportError != null) {
        interpreter.set("ReportError", tempReportError);
      } else {
        interpreter.set("ReportError", Py.None);
      }
      PyObject pep8Module = JythonModules.getPep8Module(interpreter);
      interpreter.set("pep8", pep8Module);

      String formatted = StringUtils.format(EXECUTE_PEP8, file, args.toString(), file);
      interpreter.exec(formatted);
      if (reportError == null) {
        synchronized (lock) {
          if (reportError == null) {
            reportError = interpreter.get("ReportError");
          }
        }
      }

    } catch (Exception e) {
      Log.log("Error analyzing: " + module, e);
    }

    return messages;
  }