[Python-checkins] cpython: Factor out the distribution file-system safe name functions from

alexis.metaireau python-checkins at python.org
Mon Sep 12 19:36:24 CEST 2011


http://hg.python.org/cpython/rev/a03da3dc478e
changeset:   72364:a03da3dc478e
parent:      72360:76edde9b63ae
user:        Jeremy Kloth <jeremy.kloth at gmail.com>
date:        Mon Sep 12 11:12:42 2011 -0600
summary:
  Factor out the distribution file-system safe name functions from install_distinfo to allow all metadata consumers access to them.

files:
  Lib/packaging/command/install_distinfo.py |  32 +----------
  Lib/packaging/dist.py                     |   4 +-
  Lib/packaging/metadata.py                 |  16 ++++-
  3 files changed, 16 insertions(+), 36 deletions(-)


diff --git a/Lib/packaging/command/install_distinfo.py b/Lib/packaging/command/install_distinfo.py
--- a/Lib/packaging/command/install_distinfo.py
+++ b/Lib/packaging/command/install_distinfo.py
@@ -63,9 +63,7 @@
 
         metadata = self.distribution.metadata
 
-        basename = "%s-%s.dist-info" % (
-            to_filename(safe_name(metadata['Name'])),
-            to_filename(safe_version(metadata['Version'])))
+        basename = metadata.get_fullname(filesafe=True) + ".dist-info"
 
         self.distinfo_dir = os.path.join(self.distinfo_dir, basename)
 
@@ -145,31 +143,3 @@
 
     def get_outputs(self):
         return self.outfiles
-
-
-# The following functions are taken from setuptools' pkg_resources module.
-
-def safe_name(name):
-    """Convert an arbitrary string to a standard distribution name
-
-    Any runs of non-alphanumeric/. characters are replaced with a single '-'.
-    """
-    return re.sub('[^A-Za-z0-9.]+', '-', name)
-
-
-def safe_version(version):
-    """Convert an arbitrary string to a standard version string
-
-    Spaces become dots, and all other non-alphanumeric characters become
-    dashes, with runs of multiple dashes condensed to a single dash.
-    """
-    version = version.replace(' ', '.')
-    return re.sub('[^A-Za-z0-9.]+', '-', version)
-
-
-def to_filename(name):
-    """Convert a project or version name to its filename-escaped form
-
-    Any '-' characters are currently replaced with '_'.
-    """
-    return name.replace('-', '_')
diff --git a/Lib/packaging/dist.py b/Lib/packaging/dist.py
--- a/Lib/packaging/dist.py
+++ b/Lib/packaging/dist.py
@@ -228,8 +228,8 @@
             d = self.command_options[command] = {}
         return d
 
-    def get_fullname(self):
-        return self.metadata.get_fullname()
+    def get_fullname(self, filesafe=False):
+        return self.metadata.get_fullname(filesafe)
 
     def dump_option_dicts(self, header=None, commands=None, indent=""):
         from pprint import pformat
diff --git a/Lib/packaging/metadata.py b/Lib/packaging/metadata.py
--- a/Lib/packaging/metadata.py
+++ b/Lib/packaging/metadata.py
@@ -182,6 +182,7 @@
 
 _MISSING = object()
 
+_FILESAFE = re.compile('[^A-Za-z0-9.]+')
 
 class Metadata:
     """The metadata of a release.
@@ -285,9 +286,18 @@
     #
     # Public API
     #
-    def get_fullname(self):
-        """Return the distribution name with version"""
-        return '%s-%s' % (self['Name'], self['Version'])
+    def get_fullname(self, filesafe=False):
+        """Return the distribution name with version.
+
+        If filesafe is true, return a filename-escaped form."""
+        name, version = self['Name'], self['Version']
+        if filesafe:
+            # For both name and version any runs of non-alphanumeric or '.'
+            # characters are replaced with a single '-'.  Additionally any
+            # spaces in the version string become '.'
+            name = _FILESAFE.sub('-', name)
+            version = _FILESAFE.sub('-', version.replace(' ', '.'))
+        return '%s-%s' % (name, version)
 
     def is_metadata_field(self, name):
         """return True if name is a valid metadata key"""

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


More information about the Python-checkins mailing list