Commit 51d68aa5 authored by PJ Eby's avatar PJ Eby

``safe_name()`` now allows dots in project names, and there is a new

``to_filename()`` function that escapes project names and versions for
safe use in constructing egg filenames from a Distribution object's
metadata.

Note that allowing dots may now cause problems for projects with '.' in
the name that were previously installed, since such projects had to be
spelled with a '-' before.  The '-' name will no longer match the '.'
project, and there is no real room for backward compatibility here.  :(

--HG--
branch : setuptools
extra : convert_revision : svn%3A6015fed2-1504-0410-9fe1-9d1591cc4771/sandbox/trunk/setuptools%4041994
parent f95da2a3
...@@ -61,7 +61,7 @@ __all__ = [ ...@@ -61,7 +61,7 @@ __all__ = [
# Parsing functions and string utilities # Parsing functions and string utilities
'parse_requirements', 'parse_version', 'safe_name', 'safe_version', 'parse_requirements', 'parse_version', 'safe_name', 'safe_version',
'get_platform', 'compatible_platforms', 'yield_lines', 'split_sections', 'get_platform', 'compatible_platforms', 'yield_lines', 'split_sections',
'safe_extra', 'safe_extra', 'to_filename',
# filesystem utilities # filesystem utilities
'ensure_directory', 'normalize_path', 'ensure_directory', 'normalize_path',
...@@ -821,9 +821,9 @@ def get_default_cache(): ...@@ -821,9 +821,9 @@ def get_default_cache():
def safe_name(name): def safe_name(name):
"""Convert an arbitrary string to a standard distribution name """Convert an arbitrary string to a standard distribution name
Any runs of non-alphanumeric characters are replaced with a single '-'. Any runs of non-alphanumeric/. characters are replaced with a single '-'.
""" """
return re.sub('[^A-Za-z0-9]+', '-', name) return re.sub('[^A-Za-z0-9.]+', '-', name)
def safe_version(version): def safe_version(version):
...@@ -842,15 +842,15 @@ def safe_extra(extra): ...@@ -842,15 +842,15 @@ def safe_extra(extra):
Any runs of non-alphanumeric characters are replaced with a single '_', Any runs of non-alphanumeric characters are replaced with a single '_',
and the result is always lowercased. and the result is always lowercased.
""" """
return re.sub('[^A-Za-z0-9]+', '_', extra).lower() return re.sub('[^A-Za-z0-9.]+', '_', extra).lower()
def to_filename(name):
"""Convert a project or version name to its filename-escaped form
Any '-' characters are currently replaced with '_'.
"""
return name.replace('-','_')
...@@ -1529,7 +1529,7 @@ def yield_lines(strs): ...@@ -1529,7 +1529,7 @@ def yield_lines(strs):
LINE_END = re.compile(r"\s*(#.*)?$").match # whitespace and comment LINE_END = re.compile(r"\s*(#.*)?$").match # whitespace and comment
CONTINUE = re.compile(r"\s*\\\s*(#.*)?$").match # line continuation CONTINUE = re.compile(r"\s*\\\s*(#.*)?$").match # line continuation
DISTRO = re.compile(r"\s*((\w|-)+)").match # Distribution or option DISTRO = re.compile(r"\s*((\w|[-.])+)").match # Distribution or extra
VERSION = re.compile(r"\s*(<=?|>=?|==|!=)\s*((\w|[-.])+)").match # ver. info VERSION = re.compile(r"\s*(<=?|>=?|==|!=)\s*((\w|[-.])+)").match # ver. info
COMMA = re.compile(r"\s*,").match # comma between items COMMA = re.compile(r"\s*,").match # comma between items
OBRACKET = re.compile(r"\s*\[").match OBRACKET = re.compile(r"\s*\[").match
...@@ -1846,7 +1846,7 @@ class Distribution(object): ...@@ -1846,7 +1846,7 @@ class Distribution(object):
def egg_name(self): def egg_name(self):
"""Return what this distribution's standard .egg filename should be""" """Return what this distribution's standard .egg filename should be"""
filename = "%s-%s-py%s" % ( filename = "%s-%s-py%s" % (
self.project_name.replace('-','_'), self.version.replace('-','_'), to_filename(self.project_name), to_filename(self.version),
self.py_version or PY_MAJOR self.py_version or PY_MAJOR
) )
......
...@@ -1445,6 +1445,12 @@ Parsing Utilities ...@@ -1445,6 +1445,12 @@ Parsing Utilities
similar to ``safe_name()`` except that non-alphanumeric runs are replaced similar to ``safe_name()`` except that non-alphanumeric runs are replaced
by a single underbar (``_``), and the result is lowercased. by a single underbar (``_``), and the result is lowercased.
``to_filename(name_or_version)``
Escape a name or version string so it can be used in a dash-separated
filename (or ``#egg=name-version`` tag) without ambiguity. You
should only pass in values that were returned by ``safe_name()`` or
``safe_version()``.
Platform Utilities Platform Utilities
------------------ ------------------
...@@ -1511,6 +1517,13 @@ File/Path Utilities ...@@ -1511,6 +1517,13 @@ File/Path Utilities
Release Notes/Change History Release Notes/Change History
---------------------------- ----------------------------
0.6a10
* ``safe_name()`` now allows dots in project names.
* There is a new ``to_filename()`` function that escapes project names and
versions for safe use in constructing egg filenames from a Distribution
object's metadata.
0.6a9 0.6a9
* Don't raise an error when an invalid (unfinished) distribution is found * Don't raise an error when an invalid (unfinished) distribution is found
unless absolutely necessary. Warn about skipping invalid/unfinished eggs unless absolutely necessary. Warn about skipping invalid/unfinished eggs
......
...@@ -406,7 +406,7 @@ class ParseTests(TestCase): ...@@ -406,7 +406,7 @@ class ParseTests(TestCase):
self.assertEqual(safe_name("WSGI Utils"), "WSGI-Utils") self.assertEqual(safe_name("WSGI Utils"), "WSGI-Utils")
self.assertEqual(safe_name("WSGI Utils"), "WSGI-Utils") self.assertEqual(safe_name("WSGI Utils"), "WSGI-Utils")
self.assertEqual(safe_name("Money$$$Maker"), "Money-Maker") self.assertEqual(safe_name("Money$$$Maker"), "Money-Maker")
self.assertEqual(safe_name("peak.web"), "peak-web") self.assertNotEqual(safe_name("peak.web"), "peak-web")
def testSafeVersion(self): def testSafeVersion(self):
self.assertEqual(safe_version("1.2-1"), "1.2-1") self.assertEqual(safe_version("1.2-1"), "1.2-1")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment