Beispiel #1
0
  /**
   * This method should traverse the pythonpath passed and return a structure with the info that
   * could be collected about the files that are related to python modules.
   */
  public ModulesFoundStructure getModulesFoundStructure(IProgressMonitor monitor) {
    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }
    List<String> pythonpathList = getPythonpath();

    ModulesFoundStructure ret = new ModulesFoundStructure();

    for (Iterator<String> iter = pythonpathList.iterator(); iter.hasNext(); ) {
      String element = iter.next();

      if (monitor.isCanceled()) {
        break;
      }

      // the slow part is getting the files... not much we can do (I think).
      File root = new File(element);
      PyFileListing below = getModulesBelow(root, monitor);
      if (below != null) {

        Iterator<PyFileInfo> e1 = below.getFoundPyFileInfos().iterator();
        while (e1.hasNext()) {
          PyFileInfo pyFileInfo = e1.next();
          File file = pyFileInfo.getFile();
          String scannedModuleName = pyFileInfo.getModuleName();

          String modName;
          if (scannedModuleName.length() != 0) {
            modName =
                new StringBuffer(scannedModuleName)
                    .append('.')
                    .append(stripExtension(file.getName()))
                    .toString();
          } else {
            modName = stripExtension(file.getName());
          }
          if (isValidModuleLastPart(FullRepIterable.getLastPart(modName))) {
            ret.regularModules.put(file, modName);
          }
        }

      } else { // ok, it was null, so, maybe this is not a folder, but zip file with java classes...
        ModulesFoundStructure.ZipContents zipContents = getFromZip(root, monitor);
        if (zipContents != null) {
          ret.zipContents.add(zipContents);
        }
      }
    }
    return ret;
  }
Beispiel #2
0
  /**
   * This method returns all modules that can be obtained from a root File.
   *
   * @param monitor keep track of progress (and cancel)
   * @return the listing with valid module files considering that root is a root path in the
   *     pythonpath. May return null if the passed file does not exist or is not a directory (e.g.:
   *     zip file)
   */
  public static PyFileListing getModulesBelow(File root, IProgressMonitor monitor) {
    if (!root.exists()) {
      return null;
    }

    if (root.isDirectory()) {
      FileFilter filter =
          new FileFilter() {

            public boolean accept(File pathname) {
              if (pathname.isFile()) {
                return isValidFileMod(REF.getFileAbsolutePath(pathname));
              } else if (pathname.isDirectory()) {
                return isFileOrFolderWithInit(pathname);
              } else {
                return false;
              }
            }
          };
      return PyFileListing.getPyFilesBelow(root, filter, monitor, true);
    }
    return null;
  }
Beispiel #3
0
  /**
   * Returns the directories and python files in a list.
   *
   * @param addSubFolders indicates if sub-folders should be added
   * @param canonicalFolders used to know if we entered a loop in the listing (with symlinks)
   * @return An object with the results of making that listing.
   */
  private static PyFileListing getPyFilesBelow(
      PyFileListing result,
      File file,
      FileFilter filter,
      IProgressMonitor monitor,
      boolean addSubFolders,
      int level,
      boolean checkHasInit,
      String currModuleRep,
      Set<File> canonicalFolders) {

    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }

    if (file != null && file.exists()) {
      // only check files that actually exist

      if (file.isDirectory()) {
        if (level != 0) {
          FastStringBuffer newModuleRep = new FastStringBuffer(currModuleRep, 128);
          if (newModuleRep.length() != 0) {
            newModuleRep.append(".");
          }
          newModuleRep.append(file.getName());
          currModuleRep = newModuleRep.toString();
        }

        // check if it is a symlink loop
        try {
          File canonicalizedDir = file.getCanonicalFile();
          if (!canonicalizedDir.equals(file)) {
            if (canonicalFolders.contains(canonicalizedDir)) {
              return result;
            }
          }
          canonicalFolders.add(canonicalizedDir);
        } catch (IOException e) {
          PydevPlugin.log(e);
        }

        File[] files = null;

        if (filter != null) {
          files = file.listFiles(filter);
        } else {
          files = file.listFiles();
        }

        boolean hasInit = false;

        List<File> foldersLater = new LinkedList<File>();

        for (File file2 : files) {

          if (monitor.isCanceled()) {
            break;
          }

          if (file2.isFile()) {
            result.addPyFileInfo(new PyFileInfo(file2, currModuleRep));

            monitor.worked(1);
            monitor.setTaskName("Found:" + file2.toString());

            if (checkHasInit && hasInit == false) {
              // only check if it has __init__ if really needed
              if (PythonPathHelper.isValidInitFile(file2.getName())) {
                hasInit = true;
              }
            }

          } else {
            foldersLater.add(file2);
          }
        }

        if (!checkHasInit || hasInit || level == 0) {
          result.foldersFound.add(file);

          for (File folder : foldersLater) {

            if (monitor.isCanceled()) {
              break;
            }

            if (folder.isDirectory() && addSubFolders) {

              getPyFilesBelow(
                  result,
                  folder,
                  filter,
                  monitor,
                  addSubFolders,
                  level + 1,
                  checkHasInit,
                  currModuleRep,
                  canonicalFolders);

              monitor.worked(1);
            }
          }
        }

      } else if (file.isFile()) {
        result.addPyFileInfo(new PyFileInfo(file, currModuleRep));

      } else {
        throw new RuntimeException("Not dir nor file... what is it?");
      }
    }

    return result;
  }