Commits

Anonymous committed 8c1102a

changed API name (EggInfo -> Distribution) + better global APIs

  • Participants
  • Parent commits f6fede6

Comments (0)

Files changed (2)

 
 
 #
-# EggInfo class (with DistributionMetadata in it)
+# Distribution class (with DistributionMetadata in it)
 #
-class EggInfo(object):
+class Distribution(object):
 
     def __init__(self, path):
         self.info_path = path
         self._files = None
 
     def __str__(self):
-        return "EggInfo('%s')" % self.name
+        return "Distribution('%s')" % self.name
 
     def _load_record(self):
-        """Loads the RECORD file"""
+        """Loads the RECORD file."""
         if self._files is None:
             self._files = self._read_record()
 
         return open(local_path, binary and 'rb' or 'r')
 
 #
-# EggInfoDirectory represents a directory that contains egg-info files
+# DistributionDirectory represents a directory that contains egg-info files
 #
-class EggInfoDirectory(object):
+class DistributionDirectory(object):
 
     def __init__(self, path):
         self.path = path
-        self._egg_infos = []
+        self._dists = []
         # filling the list once (see if it's the best way)
         # to minimize I/O
         for element in os.listdir(self.path):
             fullpath = join(self.path, element)
             if is_egg_info(fullpath):
-                self._egg_infos.append(EggInfo(fullpath))
+                self._dists.append(Distribution(fullpath))
 
     #
     # container APIs
     #
     def __iter__(self):
-        return iter(self._egg_infos)
+        return iter(self._dists)
 
     #
     # public APIs
     #
     def file_users(self, path):
-        """Returns EggInfo instances for the projects that uses `path`."""
-        for egg_info in self:
-            if egg_info.uses(path):
-                yield egg_info
+        """Returns Distribution instances for the projects that uses `path`."""
+        for dist in self:
+            if dist.uses(path):
+                yield dist
 
     def owner(self, path):
         """Returns the owner of `path`."""
-        users = [egg_info for egg_info in self if egg_info.uses(path)]
+        users = [dist for dist in self if dist.uses(path)]
         if len(users) == 1:
             return users[0]
         return None
     _CACHED_DIRS.clear()
 
 
-class EggInfoDirectories(object):
+class DistributionDirectories(object):
 
     def __init__(self, use_cache=True):
         self._paths = []
-        self._egg_info_dirs = []
+        self._dist_dirs = []
         self.use_cache = use_cache
 
     #
     # container APIs
     #
     def __iter__(self):
-        return iter(self._egg_info_dirs)
+        return iter(self._dist_dirs)
 
     def append(self, path):
         if self.use_cache and path in _CACHED_DIRS:
-            egg_info_dir = _CACHED_DIRS[path]
+            dist_dir = _CACHED_DIRS[path]
         else:
-            egg_info_dir = EggInfoDirectory(path)
+            dist_dir = DistributionDirectory(path)
             if self.use_cache:
-                _CACHED_DIRS[path] = egg_info_dir
+                _CACHED_DIRS[path] = dist_dir
 
-        self._egg_info_dirs.append(egg_info_dir)
+        self._dist_dirs.append(dist_dir)
         self._paths.append(path)
 
     def clear(self):
-        self._egg_info_dirs.clear()
+        self._dist_dirs.clear()
         self._paths.clear()
 
-    def remove(self, egg_info_dir):
-        self._egg_info_dirs.remove(egg_info_dir)
-        self._paths.remove(egg_info_dir.path)
+    def remove(self, dist_dir):
+        self._dist_dirs.remove(dist_dir)
+        self._paths.remove(dist_dir.path)
 
     #
     # public APIs
         self.clear()
         self.load(paths)
 
-    def get_egg_infos(self):
+    def get_distributions(self):
         for directory in self:
-            for egg_info in directory:
-                yield egg_info
+            for dist in directory:
+                yield dist
 
-    def get_egg_info(self, project_name):
+    def get_distribution(self, project_name):
         """Returns an EggInfo instance for the given project name.
 
         If not found, returns None.
         """
         for directory in self:
-            for egg_info in directory:
-                if egg_info.name == project_name:
-                    return egg_info
+            for dist in directory:
+                if dist.name == project_name:
+                    return dist
 
     def get_file_users(self, path):
         """Iterates over all projects to find out which project uses the file.
         Return EggInfo instances.
         """
         for directory in self:
-            for egg_info in directory.file_users(path):
-                yield egg_info
+            for dist in directory.file_users(path):
+                yield dist
 
 #
-# high level APIs with a global EggInfoDirectories instance
+# high level APIs with a global DistributionDirectories instance loaded
+# on sys.path
 #
+_dist_dirs = DistributionDirectories()
+_dist_dirs.load(sys.path)
 
-egg_infos = EggInfoDirectories()
+def get_distributions():
+    return _dist_dirs.get_distributions()
 
-def get_egg_infos(paths=sys.path):
-    egg_infos.load(paths)
-    return egg_infos.get_egg_infos()
+def get_distribution(project_name):
+    return _dist_dirs.get_distribution(project_name)
 
-def get_egg_info(project_name, paths=sys.path):
-    egg_infos.load(paths)
-    return egg_infos.get_egg_info(project_name)
+def get_file_users(path):
+    return _dist_dirs.get_file_users(path)
 
-def get_file_users(path, paths=sys.path):
-    egg_infos.load(paths)
-    return egg_infos.get_file_users(path)
-
-

File test_pkgutil.py

 from nose.tools import *
+import pkgutil
 from pkgutil import *
 import sys
 import os
 def setup():
     sys.old = sys.path
     sys.path = [SITE_PKG]
+    pkgutil._dist_dirs = DistributionDirectories()
+    pkgutil._dist_dirs.load([SITE_PKG])
 
 def teardown():
     sys.path = sys.old
     del sys.old
 
-def test_get_egg_infos():
-    projects = list(get_egg_infos([SITE_PKG]))
+def test_distributions():
+    projects = list(get_distributions())
     assert_equals(len(projects), 2)
 
-def test_egg_info_directory():
-    assert_equals(get_egg_info('xxx', [SITE_PKG]), None)
+def test_distribution():
+    assert_equals(get_distribution('xxx'), None)
 
-    project = get_egg_info('mercurial', [SITE_PKG])
+    project = get_distribution('mercurial')
     assert_equals(project.name, 'mercurial')
 
-    project = get_egg_info('processing', [SITE_PKG])
+    project = get_distribution('processing')
     assert_equals(project.name, 'processing')
 
-def test_egg_info():
+    dist = get_distribution('mercurial')
+    assert_equals(str(dist), "Distribution('mercurial')")
 
-    egg_info = get_egg_info('mercurial', [SITE_PKG])
-    assert_equals(str(egg_info), "EggInfo('mercurial')")
-
-    files = list(egg_info.get_installed_files())
+    files = list(dist.get_installed_files())
     assert_equals(len(files), 4)
     assert_equals(files[0], ('mercurial/filelog.py', '98676876876876', '12'))
 
-    files = list(egg_info.get_installed_files(local=True))
+    files = list(dist.get_installed_files(local=True))
 
-    site_packages = os.path.split(egg_info.info_path)[0]
+    site_packages = os.path.split(dist.info_path)[0]
     location = os.path.join(site_packages, 'mercurial', 'filelog.py')
     assert_equals(files[0], (location, '98676876876876', '12'))
 
-    f = egg_info.get_egg_info_file('RECORD')
+    f = dist.get_egg_info_file('RECORD')
     record = os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info', 'RECORD')
     assert_equals(open(record).read(), f.read())
 
-    assert egg_info.uses('mercurial/filelog.py')
-    assert not egg_info.uses('mercurial/filelog.sasasasa')
+    assert dist.uses('mercurial/filelog.py')
+    assert not dist.uses('mercurial/filelog.sasasasa')
 
-    assert_equals(list(egg_info.get_egg_info_files()),
+    assert_equals(list(dist.get_egg_info_files()),
                   ['mercurial-1.0.1.egg-info/PKG_INFO',
                    'mercurial-1.0.1.egg-info/RECORD'])
 
-
-    assert_equals(list(egg_info.get_egg_info_files(local=True)),
+    assert_equals(list(dist.get_egg_info_files(local=True)),
                   [os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info/PKG_INFO'),
                    os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info/RECORD')])
 
 
 def test_directory():
-    dir = EggInfoDirectory(SITE_PKG)
+    dir = DistributionDirectory(SITE_PKG)
 
-    egg_info = dir.owner('mercurial/filelog.pyc')
-    assert egg_info.name == 'mercurial'
+    dist = dir.owner('mercurial/filelog.pyc')
+    assert dist.name == 'mercurial'
     assert dir.owner('mercurial/filelog.py') is None
 
 def test_get_file_users():
-
-    users = list(get_file_users('mercurial/filelog.py', [SITE_PKG]))
+    users = list(get_file_users('mercurial/filelog.py'))
     assert_equals(len(users), 2)
 
 def test_egg_info_dirname():
     assert egg_info_dirname('zlib', '2.5.2') == 'zlib-2.5.2.egg-info'
     assert egg_info_dirname('python-ldap', '2.5'), 'python_ldap-2.5.egg-info'
     assert egg_info_dirname('python-ldap', '2.5 a---5'), 'python_ldap-2.5.a_5.egg-info'
+