Commits

Carl Meyer  committed 0c8002e

demo REQUIRED metadata

  • Participants
  • Parent commits e2dec42

Comments (0)

Files changed (4)

 """
 from __future__ import with_statement
 import os
-from os.path import join, splitext, isdir
+from os.path import join, splitext, isdir, isfile
 from os import listdir
 from string import maketrans
 import csv
         self.container, self.info_path = os.path.split(path)
         self.pkg_info_path = join(path, 'PKG-INFO')
         self.record_path = join(path, 'RECORD')
+        self.required_path = join(path, 'REQUIRED')
         pkginfo = self._open_pkginfo()
         self.metadata = _DistributionMetadata(pkginfo)
         self.name = self.metadata.name
         self._files = None
+        self._required = None
 
     def __str__(self):
         return "Distribution('%s')" % self.name
     def _open_file(self, file, mode):
         return open(file, mode)
 
+    def _file_exists(self, path):
+        return isfile(path)
+
     def _read_record(self):
         """Reads RECORD."""
         files = []
             return path
         return join(self.container, path)
 
+    @property
+    def required(self):
+        """Returns True if this distribution was installed by direct user
+        request, or False if the package was only installed as a
+        dependency of other packages."""
+        if self._required == None:
+            self._required = self._file_exists(self.required_path)
+        return self._required
+
     def get_installed_files(self, local=False):
         """Iterates over the RECORD entries.
 
     def _open_record(self):
         return self.zipfile.open(self.record_path)
 
-    def _open_file(self, path, mode):
+    def _clean_path(self, path):
         if path.startswith(self.container):
             path = path[len(self.container)+1:]
-        return self.zipfile.open(path, mode)
+        return path
+    
+    def _open_file(self, path, mode):
+        return self.zipfile.open(self._clean_path(path), mode)
+
+    def _file_exists(self, path):
+        try:
+            self.zipfile.getinfo(self._clean_path(path))
+            return True
+        except KeyError:
+            return False
 
 #
 # DistributionDir represents a directory that contains egg-info files

File site-packages.zip

Binary file modified.

File site-packages/processing-0.52.egg-info/REQUIRED

+# This distribution was installed by user request, not solely as a dependency.

File test_pkgutil.py

     project = get_distribution('mercurial')
     assert_equals(project.name, 'mercurial')
 
+    assert not project.required
+
     project = get_distribution('processing')
     assert_equals(project.name, 'processing')
 
+    assert project.required
+
     dist = get_distribution('mercurial')
     assert_equals(str(dist), "Distribution('mercurial')")
 
     project = get_distribution('mercurial')
     assert_equals(project.name, 'mercurial')
 
+    assert not project.required
+    
     project = get_distribution('processing')
     assert_equals(project.name, 'processing')
 
+    assert project.required
+
     dist = get_distribution('mercurial')
     assert_equals(str(dist), "Distribution('mercurial')")