[Python-checkins] cpython (merge 3.3 -> default): Merge with 3.3 for Issue #19544 and Issue #7457

jason.coombs python-checkins at python.org
Mon Nov 11 00:22:22 CET 2013


http://hg.python.org/cpython/rev/28059d8b395b
changeset:   87043:28059d8b395b
parent:      87041:bab0cbf86835
parent:      87042:e19441e540ca
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Sun Nov 10 18:21:49 2013 -0500
summary:
  Merge with 3.3 for Issue #19544 and Issue #7457

files:
  Doc/distutils/examples.rst       |  42 +++++++++
  Lib/distutils/dist.py            |  90 ++++++++++++++++---
  Lib/distutils/tests/test_dist.py |  29 ++++++-
  Misc/NEWS                        |   4 +
  4 files changed, 147 insertions(+), 18 deletions(-)


diff --git a/Doc/distutils/examples.rst b/Doc/distutils/examples.rst
--- a/Doc/distutils/examples.rst
+++ b/Doc/distutils/examples.rst
@@ -284,6 +284,48 @@
     warning: check: Title underline too short. (line 2)
     warning: check: Could not finish the parsing.
 
+Reading the metadata
+=====================
+
+The :func:`distutils.core.setup` function provides a command-line interface
+that allows you to query the metadata fields of a project through the
+`setup.py` script of a given project::
+
+    $ python setup.py --name
+    distribute
+
+This call reads the `name` metadata by running the
+:func:`distutils.core.setup`  function. Although, when a source or binary
+distribution is created with Distutils, the metadata fields are written
+in a static file called :file:`PKG-INFO`. When a Distutils-based project is
+installed in Python, the :file:`PKG-INFO` file is copied alongside the modules
+and packages of the distribution under :file:`NAME-VERSION-pyX.X.egg-info`,
+where `NAME` is the name of the project, `VERSION` its version as defined
+in the Metadata, and `pyX.X` the major and minor version of Python like
+`2.7` or `3.2`.
+
+You can read back this static file, by using the
+:class:`distutils.dist.DistributionMetadata` class and its
+:func:`read_pkg_file` method::
+
+    >>> from distutils.dist import DistributionMetadata
+    >>> metadata = DistributionMetadata()
+    >>> metadata.read_pkg_file(open('distribute-0.6.8-py2.7.egg-info'))
+    >>> metadata.name
+    'distribute'
+    >>> metadata.version
+    '0.6.8'
+    >>> metadata.description
+    'Easily download, build, install, upgrade, and uninstall Python packages'
+
+Notice that the class can also be instanciated with a metadata file path to
+loads its values::
+
+    >>> pkg_info_path = 'distribute-0.6.8-py2.7.egg-info'
+    >>> DistributionMetadata(pkg_info_path).name
+    'distribute'
+
+
 .. % \section{Multiple extension modules}
 .. % \label{multiple-ext}
 
diff --git a/Lib/distutils/dist.py b/Lib/distutils/dist.py
--- a/Lib/distutils/dist.py
+++ b/Lib/distutils/dist.py
@@ -5,6 +5,7 @@
 """
 
 import sys, os, re
+from email import message_from_file
 
 try:
     import warnings
@@ -999,25 +1000,80 @@
                          "provides", "requires", "obsoletes",
                          )
 
-    def __init__ (self):
-        self.name = None
-        self.version = None
-        self.author = None
-        self.author_email = None
+    def __init__(self, path=None):
+        if path is not None:
+            self.read_pkg_file(open(path))
+        else:
+            self.name = None
+            self.version = None
+            self.author = None
+            self.author_email = None
+            self.maintainer = None
+            self.maintainer_email = None
+            self.url = None
+            self.license = None
+            self.description = None
+            self.long_description = None
+            self.keywords = None
+            self.platforms = None
+            self.classifiers = None
+            self.download_url = None
+            # PEP 314
+            self.provides = None
+            self.requires = None
+            self.obsoletes = None
+
+    def read_pkg_file(self, file):
+        """Reads the metadata values from a file object."""
+        msg = message_from_file(file)
+
+        def _read_field(name):
+            value = msg[name]
+            if value == 'UNKNOWN':
+                return None
+            return value
+
+        def _read_list(name):
+            values = msg.get_all(name, None)
+            if values == []:
+                return None
+            return values
+
+        metadata_version = msg['metadata-version']
+        self.name = _read_field('name')
+        self.version = _read_field('version')
+        self.description = _read_field('summary')
+        # we are filling author only.
+        self.author = _read_field('author')
         self.maintainer = None
+        self.author_email = _read_field('author-email')
         self.maintainer_email = None
-        self.url = None
-        self.license = None
-        self.description = None
-        self.long_description = None
-        self.keywords = None
-        self.platforms = None
-        self.classifiers = None
-        self.download_url = None
-        # PEP 314
-        self.provides = None
-        self.requires = None
-        self.obsoletes = None
+        self.url = _read_field('home-page')
+        self.license = _read_field('license')
+
+        if 'download-url' in msg:
+            self.download_url = _read_field('download-url')
+        else:
+            self.download_url = None
+
+        self.long_description = _read_field('description')
+        self.description = _read_field('summary')
+
+        if 'keywords' in msg:
+            self.keywords = _read_field('keywords').split(',')
+
+        self.platforms = _read_list('platform')
+        self.classifiers = _read_list('classifier')
+
+        # PEP 314 - these fields only exist in 1.1
+        if metadata_version == '1.1':
+            self.requires = _read_list('requires')
+            self.provides = _read_list('provides')
+            self.obsoletes = _read_list('obsoletes')
+        else:
+            self.requires = None
+            self.provides = None
+            self.obsoletes = None
 
     def write_pkg_info(self, base_dir):
         """Write the PKG-INFO file into the release tree.
diff --git a/Lib/distutils/tests/test_dist.py b/Lib/distutils/tests/test_dist.py
--- a/Lib/distutils/tests/test_dist.py
+++ b/Lib/distutils/tests/test_dist.py
@@ -8,7 +8,7 @@
 
 from unittest import mock
 
-from distutils.dist import Distribution, fix_help_options
+from distutils.dist import Distribution, fix_help_options, DistributionMetadata
 from distutils.cmd import Command
 
 from test.support import TESTFN, captured_stdout, run_unittest
@@ -388,6 +388,33 @@
         self.assertTrue(output)
 
 
+    def test_read_metadata(self):
+        attrs = {"name": "package",
+                 "version": "1.0",
+                 "long_description": "desc",
+                 "description": "xxx",
+                 "download_url": "http://example.com",
+                 "keywords": ['one', 'two'],
+                 "requires": ['foo']}
+
+        dist = Distribution(attrs)
+        metadata = dist.metadata
+
+        # write it then reloads it
+        PKG_INFO = io.StringIO()
+        metadata.write_pkg_file(PKG_INFO)
+        PKG_INFO.seek(0)
+        metadata.read_pkg_file(PKG_INFO)
+
+        self.assertEquals(metadata.name, "package")
+        self.assertEquals(metadata.version, "1.0")
+        self.assertEquals(metadata.description, "xxx")
+        self.assertEquals(metadata.download_url, 'http://example.com')
+        self.assertEquals(metadata.keywords, ['one', 'two'])
+        self.assertEquals(metadata.platforms, ['UNKNOWN'])
+        self.assertEquals(metadata.obsoletes, None)
+        self.assertEquals(metadata.requires, ['foo'])
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(DistributionTestCase))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,10 @@
 Library
 -------
 
+- Issue #19544 and Issue #7457: Restore the read_pkg_file method to
+  distutils.dist.DistributionMetadata accidentally removed in the undo of
+  distutils2.
+
 - Issue #16685: Added support for any bytes-like objects in the audioop module.
   Removed support for strings.
 

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


More information about the Python-checkins mailing list