cpython: e19441e540ca (original) (raw)
Mercurial > cpython
changeset 87042:e19441e540ca 3.3
Issue 19544 and Issue #7457: Restore the read_pkg_file method to distutils.dist.DistributionMetadata accidentally removed in the undo of distutils2. [#7457]
Jason R. Coombs jaraco@jaraco.com | |
---|---|
date | Sun, 10 Nov 2013 18:15:03 -0500 |
parents | 79b8b7c5fe8a |
children | 28059d8b395b 5e98c4e9c909 |
files | Doc/distutils/examples.rst Lib/distutils/dist.py Lib/distutils/tests/test_dist.py Misc/NEWS |
diffstat | 4 files changed, 147 insertions(+), 18 deletions(-)[+] [-] Doc/distutils/examples.rst 42 Lib/distutils/dist.py 90 Lib/distutils/tests/test_dist.py 29 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Doc/distutils/examples.rst
+++ b/Doc/distutils/examples.rst
@@ -284,6 +284,48 @@ by using the :mod:docutils
parser::
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::
+
+
+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::
+
metadata.read_pkg_file(open('distribute-0.6.8-py2.7.egg-info'))
- 'distribute'
- '0.6.8'
- '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:: +
+ + .. % \section{Multiple extension modules} .. % \label{multiple-ext}
--- a/Lib/distutils/dist.py +++ b/Lib/distutils/dist.py @@ -5,6 +5,7 @@ being built/installed/distributed. """ import sys, os, re +from email import message_from_file try: import warnings @@ -999,25 +1000,80 @@ class DistributionMetadata: "provides", "requires", "obsoletes", )
- def init (self):
self.name = None[](#l2.16)
self.version = None[](#l2.17)
self.author = None[](#l2.18)
self.author_email = None[](#l2.19)
- def init(self, path=None):
if path is not None:[](#l2.21)
self.read_pkg_file(open(path))[](#l2.22)
else:[](#l2.23)
self.name = None[](#l2.24)
self.version = None[](#l2.25)
self.author = None[](#l2.26)
self.author_email = None[](#l2.27)
self.maintainer = None[](#l2.28)
self.maintainer_email = None[](#l2.29)
self.url = None[](#l2.30)
self.license = None[](#l2.31)
self.description = None[](#l2.32)
self.long_description = None[](#l2.33)
self.keywords = None[](#l2.34)
self.platforms = None[](#l2.35)
self.classifiers = None[](#l2.36)
self.download_url = None[](#l2.37)
# PEP 314[](#l2.38)
self.provides = None[](#l2.39)
self.requires = None[](#l2.40)
self.obsoletes = None[](#l2.41)
- def read_pkg_file(self, file):
"""Reads the metadata values from a file object."""[](#l2.44)
msg = message_from_file(file)[](#l2.45)
def _read_field(name):[](#l2.47)
value = msg[name][](#l2.48)
if value == 'UNKNOWN':[](#l2.49)
return None[](#l2.50)
return value[](#l2.51)
def _read_list(name):[](#l2.53)
values = msg.get_all(name, None)[](#l2.54)
if values == []:[](#l2.55)
return None[](#l2.56)
return values[](#l2.57)
metadata_version = msg['metadata-version'][](#l2.59)
self.name = _read_field('name')[](#l2.60)
self.version = _read_field('version')[](#l2.61)
self.description = _read_field('summary')[](#l2.62)
# we are filling author only.[](#l2.63)
self.author = _read_field('author')[](#l2.64) self.maintainer = None[](#l2.65)
self.author_email = _read_field('author-email')[](#l2.66) self.maintainer_email = None[](#l2.67)
self.url = None[](#l2.68)
self.license = None[](#l2.69)
self.description = None[](#l2.70)
self.long_description = None[](#l2.71)
self.keywords = None[](#l2.72)
self.platforms = None[](#l2.73)
self.classifiers = None[](#l2.74)
self.download_url = None[](#l2.75)
# PEP 314[](#l2.76)
self.provides = None[](#l2.77)
self.requires = None[](#l2.78)
self.obsoletes = None[](#l2.79)
self.url = _read_field('home-page')[](#l2.80)
self.license = _read_field('license')[](#l2.81)
if 'download-url' in msg:[](#l2.83)
self.download_url = _read_field('download-url')[](#l2.84)
else:[](#l2.85)
self.download_url = None[](#l2.86)
self.long_description = _read_field('description')[](#l2.88)
self.description = _read_field('summary')[](#l2.89)
if 'keywords' in msg:[](#l2.91)
self.keywords = _read_field('keywords').split(',')[](#l2.92)
self.platforms = _read_list('platform')[](#l2.94)
self.classifiers = _read_list('classifier')[](#l2.95)
# PEP 314 - these fields only exist in 1.1[](#l2.97)
if metadata_version == '1.1':[](#l2.98)
self.requires = _read_list('requires')[](#l2.99)
self.provides = _read_list('provides')[](#l2.100)
self.obsoletes = _read_list('obsoletes')[](#l2.101)
else:[](#l2.102)
self.requires = None[](#l2.103)
self.provides = None[](#l2.104)
self.obsoletes = None[](#l2.105)
def write_pkg_info(self, base_dir): """Write the PKG-INFO file into the release tree.
--- a/Lib/distutils/tests/test_dist.py +++ b/Lib/distutils/tests/test_dist.py @@ -8,7 +8,7 @@ import textwrap 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 @@ class MetadataTestCase(support.TempdirMa self.assertTrue(output)
- def test_read_metadata(self):
attrs = {"name": "package",[](#l3.17)
"version": "1.0",[](#l3.18)
"long_description": "desc",[](#l3.19)
"description": "xxx",[](#l3.20)
"download_url": "http://example.com",[](#l3.21)
"keywords": ['one', 'two'],[](#l3.22)
"requires": ['foo']}[](#l3.23)
dist = Distribution(attrs)[](#l3.25)
metadata = dist.metadata[](#l3.26)
# write it then reloads it[](#l3.28)
PKG_INFO = io.StringIO()[](#l3.29)
metadata.write_pkg_file(PKG_INFO)[](#l3.30)
PKG_INFO.seek(0)[](#l3.31)
metadata.read_pkg_file(PKG_INFO)[](#l3.32)
self.assertEquals(metadata.name, "package")[](#l3.34)
self.assertEquals(metadata.version, "1.0")[](#l3.35)
self.assertEquals(metadata.description, "xxx")[](#l3.36)
self.assertEquals(metadata.download_url, 'http://example.com')[](#l3.37)
self.assertEquals(metadata.keywords, ['one', 'two'])[](#l3.38)
self.assertEquals(metadata.platforms, ['UNKNOWN'])[](#l3.39)
self.assertEquals(metadata.obsoletes, None)[](#l3.40)
self.assertEquals(metadata.requires, ['foo'])[](#l3.41)
+ def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(DistributionTestCase))
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,10 @@ Core and Builtins Library ------- +- Issue #19544 and Issue #7457: Restore the read_pkg_file method to