[Python-checkins] distutils2: Test for Distribution.get_distinfo_file.

tarek.ziade python-checkins at python.org
Mon Apr 5 23:09:19 CEST 2010


tarek.ziade pushed 265757f103bd to distutils2:

http://hg.python.org/distutils2/rev/265757f103bd
changeset:   104:265757f103bd
user:        pumazi
date:        Wed Mar 31 23:05:34 2010 -0400
summary:     Test for Distribution.get_distinfo_file.
files:       src/distutils2/_backport/pkgutil.py, src/distutils2/_backport/tests/test_pkgutil.py

diff --git a/src/distutils2/_backport/pkgutil.py b/src/distutils2/_backport/pkgutil.py
--- a/src/distutils2/_backport/pkgutil.py
+++ b/src/distutils2/_backport/pkgutil.py
@@ -9,6 +9,7 @@
 import os.path
 from csv import reader as csv_reader
 from types import ModuleType
+from distutils2.errors import DistutilsError
 from distutils2.metadata import DistributionMetadata
 from distutils2.version import suggest_normalized_version
 
@@ -591,6 +592,8 @@
 # PEP 376 Implementation #
 ##########################
 
+DIST_FILES = ('INSTALLER', 'METADATA', 'RECORD', 'REQUESTED',)
+
 class Distribution(object):
     """Created with the *path* of the ``.dist-info`` directory provided to the
     constructor. It reads the metadata contained in METADATA when it is
@@ -663,7 +666,27 @@
                            mode (r).
         :rtype: file object
         """
-        pass
+        open_flags = 'r'
+        if binary:
+            open_flags += 'b'
+
+        # Check if it is an absolute path
+        if path.find(os.sep) >= 0:
+            # it's an absolute path?
+            distinfo_dirname, path = path.split(os.sep)[-2:]
+            if distinfo_dirname != self.path.split(os.sep)[-1]:
+                raise DistutilsError("Requested dist-info file does not "
+                    "belong to the %s distribution. '%s' was requested." \
+                    % (self.name, os.sep.join([distinfo_dirname, path])))
+
+        # The file must be relative
+        if path not in DIST_FILES:
+            raise DistutilsError("Requested an invalid dist-info file: "
+                "%s" % path)
+
+        # Convert the relative path back to absolute
+        path = os.path.join(self.path, path)
+        return open(path, open_flags)
 
     def get_distinfo_files(self, local=False):
         """
diff --git a/src/distutils2/_backport/tests/test_pkgutil.py b/src/distutils2/_backport/tests/test_pkgutil.py
--- a/src/distutils2/_backport/tests/test_pkgutil.py
+++ b/src/distutils2/_backport/tests/test_pkgutil.py
@@ -114,6 +114,38 @@
         self.assertTrue(dist.uses(true_path))
         self.assertFalse(dist.uses(false_path))
 
+    def test_get_distinfo_file(self):
+        """Test the retrieval of dist-info file objects."""
+        from distutils2._backport.pkgutil import Distribution
+        distinfo_name = 'choxie-2.0.0.9'
+        other_distinfo_name = 'grammar-1.0a4'
+        distinfo_dir = os.path.join(self.fake_dists_path,
+            distinfo_name + '.dist-info')
+        dist = Distribution(distinfo_dir)
+        # Test for known good file matches
+        distinfo_files = [
+            # Relative paths
+            'INSTALLER', 'METADATA',
+            # Absolute paths
+            os.path.join(distinfo_dir, 'RECORD'),
+            os.path.join(distinfo_dir, 'REQUESTED'),
+            ]
+
+        for distfile in distinfo_files:
+            value = dist.get_distinfo_file(distfile)
+            self.assertTrue(isinstance(value, file))
+            # Is it the correct file?
+            self.assertEqual(value.name, os.path.join(distinfo_dir, distfile))
+
+        from distutils2.errors import DistutilsError
+        # Test an absolute path that is part of another distributions dist-info
+        other_distinfo_file = os.path.join(self.fake_dists_path,
+            other_distinfo_name + '.dist-info', 'REQUESTED')
+        self.assertRaises(DistutilsError, dist.get_distinfo_file,
+            other_distinfo_file)
+        # Test for a file that does not exist and should not exist
+        self.assertRaises(DistutilsError, dist.get_distinfo_file, 'ENTRYPOINTS')
+
 
 class TestPkgUtilFunctions(unittest2.TestCase):
     """Tests for the new functionality added in PEP 376."""

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list