Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa2b442

Browse files
committed
Factor out the distribution file-system safe name functions from install_distinfo to allow all metadata consumers access to them.
1 parent 4c3124c commit aa2b442

3 files changed

Lines changed: 16 additions & 36 deletions

File tree

Lib/packaging/command/install_distinfo.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def finalize_options(self):
6363

6464
metadata = self.distribution.metadata
6565

66-
basename = "%s-%s.dist-info" % (
67-
to_filename(safe_name(metadata['Name'])),
68-
to_filename(safe_version(metadata['Version'])))
66+
basename = metadata.get_fullname(filesafe=True) + ".dist-info"
6967

7068
self.distinfo_dir = os.path.join(self.distinfo_dir, basename)
7169

@@ -145,31 +143,3 @@ def run(self):
145143

146144
def get_outputs(self):
147145
return self.outfiles
148-
149-
150-
# The following functions are taken from setuptools' pkg_resources module.
151-
152-
def safe_name(name):
153-
"""Convert an arbitrary string to a standard distribution name
154-
155-
Any runs of non-alphanumeric/. characters are replaced with a single '-'.
156-
"""
157-
return re.sub('[^A-Za-z0-9.]+', '-', name)
158-
159-
160-
def safe_version(version):
161-
"""Convert an arbitrary string to a standard version string
162-
163-
Spaces become dots, and all other non-alphanumeric characters become
164-
dashes, with runs of multiple dashes condensed to a single dash.
165-
"""
166-
version = version.replace(' ', '.')
167-
return re.sub('[^A-Za-z0-9.]+', '-', version)
168-
169-
170-
def to_filename(name):
171-
"""Convert a project or version name to its filename-escaped form
172-
173-
Any '-' characters are currently replaced with '_'.
174-
"""
175-
return name.replace('-', '_')

Lib/packaging/dist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ def get_option_dict(self, command):
228228
d = self.command_options[command] = {}
229229
return d
230230

231-
def get_fullname(self):
232-
return self.metadata.get_fullname()
231+
def get_fullname(self, filesafe=False):
232+
return self.metadata.get_fullname(filesafe)
233233

234234
def dump_option_dicts(self, header=None, commands=None, indent=""):
235235
from pprint import pformat

Lib/packaging/metadata.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def _has_marker(keys, markers):
182182

183183
_MISSING = object()
184184

185+
_FILESAFE = re.compile('[^A-Za-z0-9.]+')
185186

186187
class Metadata:
187188
"""The metadata of a release.
@@ -285,9 +286,18 @@ def _remove_line_prefix(self, value):
285286
#
286287
# Public API
287288
#
288-
def get_fullname(self):
289-
"""Return the distribution name with version"""
290-
return '%s-%s' % (self['Name'], self['Version'])
289+
def get_fullname(self, filesafe=False):
290+
"""Return the distribution name with version.
291+
292+
If filesafe is true, return a filename-escaped form."""
293+
name, version = self['Name'], self['Version']
294+
if filesafe:
295+
# For both name and version any runs of non-alphanumeric or '.'
296+
# characters are replaced with a single '-'. Additionally any
297+
# spaces in the version string become '.'
298+
name = _FILESAFE.sub('-', name)
299+
version = _FILESAFE.sub('-', version.replace(' ', '.'))
300+
return '%s-%s' % (name, version)
291301

292302
def is_metadata_field(self, name):
293303
"""return True if name is a valid metadata key"""

0 commit comments

Comments
 (0)