[Python-checkins] distutils2: changed the metadata behavior so the version does not get set all the time

tarek.ziade python-checkins at python.org
Tue Mar 13 04:25:44 CET 2012


http://hg.python.org/distutils2/rev/812ba38e2f63
changeset:   1296:812ba38e2f63
user:        Tarek Ziade <tarek at ziade.org>
date:        Mon Mar 12 20:25:35 2012 -0700
summary:
  changed the metadata behavior so the version does not get set all the time

files:
  distutils2/database.py            |   3 +-
  distutils2/metadata.py            |  29 +++++++++---------
  distutils2/tests/test_metadata.py |  25 ++++++++++++---
  3 files changed, 36 insertions(+), 21 deletions(-)


diff --git a/distutils2/database.py b/distutils2/database.py
--- a/distutils2/database.py
+++ b/distutils2/database.py
@@ -380,7 +380,8 @@
             if self.metadata['Metadata-Version'] == '1.1':
                 # we can't have 1.1 metadata *and* Setuptools requires
                 for field in ('Obsoletes', 'Requires', 'Provides'):
-                    del self.metadata[field]
+                    if field in self.metadata:
+                        del self.metadata[field]
 
         reqs = []
 
diff --git a/distutils2/metadata.py b/distutils2/metadata.py
--- a/distutils2/metadata.py
+++ b/distutils2/metadata.py
@@ -49,7 +49,7 @@
 
 # preferred version. Hopefully will be changed
 # to 1.2 once PEP 345 is supported everywhere
-PKG_INFO_PREFERRED_VERSION = '1.0'
+PKG_INFO_PREFERRED_VERSION = '1.1'
 
 _LINE_PREFIX = re.compile('\n       \|')
 _241_FIELDS = ('Metadata-Version', 'Name', 'Version', 'Platform',
@@ -102,7 +102,12 @@
                 return True
         return False
 
-    keys = list(fields)
+    keys = []
+    for key, value in fields.items():
+        if value in ([], 'UNKNOWN', None):
+            continue
+        keys.append(key)
+
     possible_versions = ['1.0', '1.1', '1.2']
 
     # first let's try to see if a field is not part of one of the version
@@ -215,8 +220,9 @@
             self.read_file(fileobj)
         elif mapping is not None:
             self.update(mapping)
+            self.set_metadata_version()
 
-    def _set_best_version(self):
+    def set_metadata_version(self):
         self._fields['Metadata-Version'] = _best_version(self._fields)
 
     def _write_field(self, file, name, value):
@@ -234,7 +240,6 @@
             del self._fields[field_name]
         except KeyError:
             raise KeyError(name)
-        self._set_best_version()
 
     def __contains__(self, name):
         return (name in self._fields or
@@ -336,6 +341,7 @@
                 value = msg[field]
                 if value is not None and value != 'UNKNOWN':
                     self.set(field, value)
+        self.set_metadata_version()
 
     def write(self, filepath):
         """Write the metadata fields to filepath."""
@@ -347,7 +353,8 @@
 
     def write_file(self, fileobject):
         """Write the PKG-INFO format data to a file object."""
-        self._set_best_version()
+        self.set_metadata_version()
+
         for field in _version2fieldlist(self['Metadata-Version']):
             values = self.get(field)
             if field in _ELEMENTSFIELD:
@@ -374,14 +381,6 @@
         Keys that don't match a metadata field or that have an empty value are
         dropped.
         """
-        # XXX the code should just use self.set, which does tbe same checks and
-        # conversions already, but that would break packaging.pypi: it uses the
-        # update method, which does not call _set_best_version (which set
-        # does), and thus allows having a Metadata object (as long as you don't
-        # modify or write it) with extra fields from PyPI that are not fields
-        # defined in Metadata PEPs.  to solve it, the best_version system
-        # should be reworked so that it's called only for writing, or in a new
-        # strict mode, or with a new, more lax Metadata subclass in p7g.pypi
         def _set(key, value):
             if key in _ATTR2FIELD and value:
                 self.set(self._convert_name(key), value)
@@ -442,7 +441,6 @@
                 value = self._remove_line_prefix(value)
 
         self._fields[name] = value
-        self._set_best_version()
 
     def get(self, name, default=_MISSING):
         """Get a metadata field."""
@@ -484,6 +482,8 @@
     def check(self, strict=False, restructuredtext=False):
         """Check if the metadata is compliant. If strict is False then raise if
         no Name or Version are provided"""
+        self.set_metadata_version()
+
         # XXX should check the versions (if the file was loaded)
         missing, warnings = [], []
 
@@ -528,6 +528,7 @@
         Field names will be converted to use the underscore-lowercase style
         instead of hyphen-mixed case (i.e. home_page instead of Home-page).
         """
+        self.set_metadata_version()
         data = {
             'metadata_version': self['Metadata-Version'],
             'name': self['Name'],
diff --git a/distutils2/tests/test_metadata.py b/distutils2/tests/test_metadata.py
--- a/distutils2/tests/test_metadata.py
+++ b/distutils2/tests/test_metadata.py
@@ -54,7 +54,7 @@
         self.assertEqual(len(m.items()), 22)
 
         m = Metadata(mapping=dict(name='Test', version='1.0'))
-        self.assertEqual(len(m.items()), 11)
+        self.assertEqual(len(m.items()), 17)
 
         d = dict(m.items())
         self.assertRaises(TypeError, Metadata,
@@ -266,27 +266,32 @@
         self.assertNotIn('Obsoletes', metadata)
 
         metadata['Classifier'] = ['ok']
+        metadata.set_metadata_version()
         self.assertEqual(metadata['Metadata-Version'], '1.1')
 
         metadata = Metadata()
         metadata['Download-URL'] = 'ok'
+        metadata.set_metadata_version()
         self.assertEqual(metadata['Metadata-Version'], '1.1')
 
         metadata = Metadata()
         metadata['Obsoletes'] = 'ok'
+        metadata.set_metadata_version()
         self.assertEqual(metadata['Metadata-Version'], '1.1')
 
         del metadata['Obsoletes']
         metadata['Obsoletes-Dist'] = 'ok'
+        metadata.set_metadata_version()
         self.assertEqual(metadata['Metadata-Version'], '1.2')
-
-        self.assertRaises(MetadataConflictError, metadata.set,
-                          'Obsoletes', 'ok')
+        metadata.set('Obsoletes', 'ok')
+        self.assertRaises(MetadataConflictError,
+                          metadata.set_metadata_version)
 
         del metadata['Obsoletes']
         del metadata['Obsoletes-Dist']
+        metadata.set_metadata_version()
         metadata['Version'] = '1'
-        self.assertEqual(metadata['Metadata-Version'], '1.0')
+        self.assertEqual(metadata['Metadata-Version'], '1.1')
 
         # make sure the _best_version function works okay with
         # non-conflicting fields from 1.1 and 1.2 (i.e. we want only the
@@ -295,18 +300,25 @@
         metadata = Metadata()
         metadata['Requires-Python'] = '3'
         metadata['Classifier'] = ['Programming language :: Python :: 3']
+        metadata.set_metadata_version()
         self.assertEqual(metadata['Metadata-Version'], '1.2')
 
         PKG_INFO = os.path.join(os.path.dirname(__file__),
                                 'SETUPTOOLS-PKG-INFO')
         metadata = Metadata(PKG_INFO)
-        self.assertEqual(metadata['Metadata-Version'], '1.0')
+        self.assertEqual(metadata['Metadata-Version'], '1.1')
 
         PKG_INFO = os.path.join(os.path.dirname(__file__),
                                 'SETUPTOOLS-PKG-INFO2')
         metadata = Metadata(PKG_INFO)
         self.assertEqual(metadata['Metadata-Version'], '1.1')
 
+        # make sure an empty list for Obsoletes and Requires-dist gets ignored
+        metadata['Obsoletes'] = []
+        metadata['Requires-dist'] = []
+        metadata.set_metadata_version()
+        self.assertEqual(metadata['Metadata-Version'], '1.1')
+
         # Update the _fields dict directly to prevent 'Metadata-Version'
         # from being updated by the _set_best_version() method.
         metadata._fields['Metadata-Version'] = '1.618'
@@ -371,6 +383,7 @@
         metadata = Metadata()
         metadata['Project-URL'] = [('one', 'http://ok')]
         self.assertEqual(metadata['Project-URL'], [('one', 'http://ok')])
+        metadata.set_metadata_version()
         self.assertEqual(metadata['Metadata-Version'], '1.2')
 
         # make sure this particular field is handled properly when written

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


More information about the Python-checkins mailing list