Commits

Tarek Ziadé committed 503ce7b

added an option to apply the markers to the current platform

  • Participants
  • Parent commits e2ee60e

Comments (0)

Files changed (3)

File src/distutils2/metadata.py

 class DistributionMetadata(object):
     """Distribution meta-data class (1.0 or 1.2).
     """
-    def __init__(self, path=None):
+    def __init__(self, path=None, platform_dependant=False):
         self._fields = {}
         self.version = None
         self.docutils_support = _HAS_DOCUTILS
+        self.platform_dependant = platform_dependant
         if path is not None:
             self.read(path)
 
 
         return reporter.messages
 
+    def _platform(self, value):
+        if not self.platform_dependant or ';' not in value:
+            return True, value
+        value, marker = value.split(';')
+        return _interpret(marker), value
+
     #
     # Public APIs
     #
             fields = _345_FIELDS
 
         for field in fields:
-            value = msg[field.lower()]
-            if value is None:
-                continue
-            self.set_field(field, value)
+            if field in _LISTFIELDS:
+                # we can have multiple lines
+                values = msg.get_all(field)
+                self.set_field(field, values)
+            else:
+                # single line
+                value = msg[field]
+                if value is not None:
+                    self.set_field(field, value)
 
         self.version = self._guessmetadata_version()
         self.set_field('Metadata-Version', self.version)
         if name not in self._fields:
             return self._default_value(name)
         if name in _UNICODEFIELDS:
-            return self._encode_field(self._fields[name])
+            value = self._fields[name]
+            return self._encode_field(value)
         elif name in _LISTFIELDS:
             value = self._fields[name]
             if value is None:
                 return []
-            return [self._encode_field(v) for v in value]
+            res = []
+            for val in value:
+                valid, val = self._platform(val)
+                if not valid:
+                    continue
+                res.append(self._encode_field(val))
+            return res
+
         elif name in _ELEMENTSFIELD:
-            value = self._fields[name]
+            valid, value = self._platform(self._fields[name])
+            if not valid:
+                return []
             if isinstance(value, str):
                 return value.split(',')
-        return self._fields[name]
+        valid, value = self._platform(self._fields[name])
+        if not valid:
+            return None
+        return value
 
     def check(self):
         """Checks if the metadata are compliant."""
 
 def _interpret(marker):
     """Interprets a marker and return a result given the environment."""
+    marker = marker.strip()
     operations = _CHAIN()
     tokenize(StringIO(marker).readline, operations.eat)
     return operations.result()

File src/distutils2/tests/PKG-INFO

-Metadata-Version: 1.0
+Metadata-Version: 1.2
 Name: CLVault
 Version: 0.5
 Summary: Command-Line utility to store and retrieve passwords
 Author: Tarek Ziade
 Author-email: tarek@ziade.org
 License: PSF
+Keywords: keyring,password,crypt
+Requires-Dist: foo; sys.platform == 'okook'
+Requires-Dist: bar; sys.platform == '%s'
+Platform: UNKNOWN
 Description: CLVault
-        =======
+       |=======
+       |
+       |CLVault uses Keyring to provide a command-line utility to safely store
+       |and retrieve passwords.
+       |
+       |Install it using pip or the setup.py script::
+       |
+       |    $ python setup.py install
+       |
+       |    $ pip install clvault
+       |
+       |Once it's installed, you will have three scripts installed in your
+       |Python scripts folder, you can use to list, store and retrieve passwords::
+       |
+       |    $ clvault-set blog
+       |    Set your password:
+       |    Set the associated username (can be blank): tarek
+       |    Set a description (can be blank): My blog password
+       |    Password set.
+       |
+       |    $ clvault-get blog
+       |    The username is "tarek"
+       |    The password has been copied in your clipboard
+       |
+       |    $ clvault-list
+       |    Registered services:
+       |    blog    My blog password
+       |
+       |
+       |*clvault-set* takes a service name then prompt you for a password, and some
+       |optional information about your service. The password is safely stored in
+       |a keyring while the description is saved in a ``.clvault`` file in your
+       |home directory. This file is created automatically the first time the command
+       |is used.
 
-        CLVault uses Keyring to provide a command-line utility to safely store
-        and retrieve passwords.
+       |*clvault-get* copies the password for a given service in your clipboard, and
+       |displays the associated user if any.
 
-        Install it using pip or the setup.py script::
+       |*clvault-list* lists all registered services, with their description when
+       |given.
+       |
+       |
+       |Project page: http://bitbucket.org/tarek/clvault
+       |
 
-        $ python setup.py install
 
-        $ pip install clvault
-
-        Once it's installed, you will have three scripts installed in your
-        Python scripts folder, you can use to list, store and retrieve passwords::
-
-        $ clvault-set blog
-        Set your password:
-        Set the associated username (can be blank): tarek
-        Set a description (can be blank): My blog password
-        Password set.
-
-        $ clvault-get blog
-        The username is "tarek"
-        The password has been copied in your clipboard
-
-        $ clvault-list
-        Registered services:
-        blog    My blog password
-
-
-        *clvault-set* takes a service name then prompt you for a password, and some
-        optional information about your service. The password is safely stored in
-        a keyring while the description is saved in a ``.clvault`` file in your
-        home directory. This file is created automatically the first time the command
-        is used.
-
-        *clvault-get* copies the password for a given service in your clipboard, and
-        displays the associated user if any.
-
-        *clvault-list* lists all registered services, with their description when
-        given.
-
-
-        Project page: http://bitbucket.org/tarek/clvault
-
-
-Keywords: keyring,password,crypt
-Platform: UNKNOWN

File src/distutils2/tests/test_metadata.py

         res = res.read()
         f = open(PKG_INFO)
         wanted = f.read()
+        self.assert_('Keywords: keyring,password,crypt' in res)
         f.close()
 
+    def test_metadata_markers(self):
+        # see if we can be platform-aware
+        PKG_INFO = os.path.join(os.path.dirname(__file__), 'PKG-INFO')
+        content = open(PKG_INFO).read()
+        content = content % sys.platform
+        metadata = DistributionMetadata(platform_dependant=True)
+        metadata.read_file(StringIO(content))
+        self.assertEquals(metadata['Requires-Dist'], ['bar'])
+
 def test_suite():
     return unittest2.makeSuite(DistributionMetadataTestCase)