Example #1
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;
  }