[Python-checkins] af2f3db02606 in distutils2: a bad predicate just display a warning --
tarek.ziade
hg at python.org
Fri Mar 5 00:31:24 CET 2010
tarek.ziade pushed af2f3db02606 to distutils2:
http://hg.python.org/distutils2/rev/af2f3db02606
changeset: 72:af2f3db02606
tag: tip
user: Tarek Ziade <tarek at ziade.org>
date: Thu Mar 04 21:04:49 2010 +0100
summary: a bad predicate just display a warning --
diff --git a/docs/source/metadata.rst b/docs/source/metadata.rst
--- a/docs/source/metadata.rst
+++ b/docs/source/metadata.rst
@@ -2,20 +2,17 @@
Metadata
========
-Distutils2 provides a :class:`DistributionMetadata` class that can read and
-write Metadata files. It also supports PEP 345 environment markers and
-checks that version numbers provided in the fields are PEP 386 compatible
-when required.
+Distutils2 provides a :class:`DistributionMetadata` class that can read and
+write Metadata files. This class is compatible with all versions of Metadata:
+- 1.0 : PEP 241
+- 1.1 : PEP 314
+- 1.2 : PEP 345
-Supported formats
-=================
+The PEP 345 implementation supports the micro-language for the environment
+markers, and displays warnings when versions that are supposed to be
+PEP 386 are violating the scheme.
-The class can read and write Metadata v1.0 and v1.2
-files. When a v1.1 file is read, it is transformed into 1.0 or 1.2 depending
-on the fields provided. 1.1 fields are ignored in that case.
-
-XXX explain why
Reading metadata
================
@@ -34,11 +31,11 @@
>>> metadata['Requires-Dist']
["pywin32; sys.platform == 'win32'", "Sphinx"]
-The fields that supports environment markers (XXX) can be automatically
-ignored if the object is instanciated using the ``platform_dependant`` option.
+The fields that supports environment markers can be automatically ignored if
+the object is instanciated using the ``platform_dependant`` option.
:class:`DistributionMetadata` will interpret in the case the markers and will
-automatically remove the fields that are not compliant with the running
-environment. Here's an example under Mac OS X. The win32 dependency
+automatically remove the fields that are not compliant with the running
+environment. Here's an example under Mac OS X. The win32 dependency
we saw earlier is ignored::
>>> from distutils2.metadata import DistributionMetadata
@@ -46,9 +43,53 @@
>>> metadata['Requires-Dist']
['bar']
+If you want to provide your own execution context, let's say to test the
+Metadata under a particular environment that is not the current environment,
+you can provide your own values in the ``execution_context`` option, which
+is the dict that may contain one or more keys of the context the micro-language
+expects.
+
+Here's an example, simulating a win32 environment::
+
+ >>> from distutils2.metadata import DistributionMetadata
+ >>> context = {'sys.platform': 'win32'}
+ >>> metadata = DistributionMetadata('PKG-INFO', platform_dependant=True,
+ ... execution_context=context)
+ ...
+ >>> metadata['Requires-Dist'] = ["pywin32; sys.platform == 'win32'",
+ ... "Sphinx"]
+ ...
+ >>> metadata['Requires-Dist']
+ ['pywin32', 'Sphinx']
+
Writing metadata
================
-XXX demonstrate here how version numbers are checked when a key is set.
+Writing metadata can be done using the ``write`` API::
+ >>> metadata.write('/to/my/PKG-INFO')
+
+The class will pick the best version for the metadata, depending on the values
+provided. If all the values provided exists in all versions, teh class will
+used :attr:`metadata.PKG_INFO_PREFERRED_VERSION`. It is set by default to 1.0.
+
+
+Conflict checking and best version
+==================================
+
+Some fields in PEP 345 have to follow a version scheme in their versions
+predicate. When the scheme is violated, a warning is emited::
+
+ >>> from distutils2.metadata import DistributionMetadata
+ >>> metadata = DistributionMetadata()
+ >>> metadata['Requires-Dist'] = ['Funky (Groovie)']
+ "Funky (Groovie)" is not a valid predicate
+ >>> metadata['Requires-Dist'] = ['Funky (1.2)']
+
+
+
+XXX talk about check()
+
+
+
diff --git a/src/distutils2/errors.py b/src/distutils2/errors.py
--- a/src/distutils2/errors.py
+++ b/src/distutils2/errors.py
@@ -93,3 +93,16 @@
class MetadataUnrecognizedVersionError(DistutilsError):
"""Unknown metadata version number."""
+class IrrationalVersionError(Exception):
+ """This is an irrational version."""
+ pass
+
+class HugeMajorVersionNumError(IrrationalVersionError):
+ """An irrational version because the major version number is huge
+ (often because a year or date was used).
+
+ See `error_on_huge_major_num` option in `NormalizedVersion` for details.
+ This guard can be disabled by setting that option False.
+ """
+ pass
+
diff --git a/src/distutils2/metadata.py b/src/distutils2/metadata.py
--- a/src/distutils2/metadata.py
+++ b/src/distutils2/metadata.py
@@ -58,6 +58,7 @@
from email import message_from_file
from tokenize import tokenize, NAME, OP, STRING, ENDMARKER
+from distutils2.log import warn
from distutils2.util import rfc822_escape
from distutils2.version import is_valid_predicate
from distutils2.errors import (MetadataConflictError,
@@ -352,7 +353,7 @@
for v in value:
# check that the values are valid predicates
if not is_valid_predicate(v.split(';')[0]):
- raise ValueError('"%s" is not a valid predicate' % v)
+ warn('"%s" is not a valid predicate' % v)
if name in _LISTFIELDS + _ELEMENTSFIELD:
if isinstance(value, str):
value = value.split(',')
diff --git a/src/distutils2/tests/test_metadata.py b/src/distutils2/tests/test_metadata.py
--- a/src/distutils2/tests/test_metadata.py
+++ b/src/distutils2/tests/test_metadata.py
@@ -134,6 +134,13 @@
metadata.read_file(StringIO(open(PKG_INFO).read()))
self.assertEquals(metadata['Metadata-Version'], '1.1')
+ def test_warnings(self):
+ metadata = DistributionMetadata()
+
+ # this should raise a warning
+ # XXX how to test this on 2.4 ?
+ metadata['Requires-Dist'] = ['Funky (Groovie)']
+
def test_suite():
diff --git a/src/distutils2/version.py b/src/distutils2/version.py
--- a/src/distutils2/version.py
+++ b/src/distutils2/version.py
@@ -1,18 +1,7 @@
import sys
import re
-class IrrationalVersionError(Exception):
- """This is an irrational version."""
- pass
-
-class HugeMajorVersionNumError(IrrationalVersionError):
- """An irrational version because the major version number is huge
- (often because a year or date was used).
-
- See `error_on_huge_major_num` option in `NormalizedVersion` for details.
- This guard can be disabled by setting that option False.
- """
- pass
+from distutils2.errors import IrrationalVersionError, HugeMajorVersionNumError
# A marker used in the second and third parts of the `parts` tuple, for
# versions that don't have those segments, to sort properly. An example
@@ -373,7 +362,7 @@
def is_valid_predicate(predicate):
try:
VersionPredicate(predicate)
- except ValueError:
+ except (ValueError, IrrationalVersionError):
return False
else:
return True
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list