Commit b163acc7 authored by Jason R. Coombs's avatar Jason R. Coombs

Use packaging.version.Version to sort filenames by the version of the package...

Use packaging.version.Version to sort filenames by the version of the package they represent. Alternate implementation of that proposed in #829. Also ref #629.
parent 51b10c39
...@@ -2,6 +2,14 @@ ...@@ -2,6 +2,14 @@
CHANGES CHANGES
======= =======
v28.8.0
-------
* #629: Per the discussion, refine the sorting to use version
value order for more accurate detection of the latest
available version when scanning for packages. See also
#829.
v28.7.1 v28.7.1
------- -------
......
...@@ -36,6 +36,7 @@ import plistlib ...@@ -36,6 +36,7 @@ import plistlib
import email.parser import email.parser
import tempfile import tempfile
import textwrap import textwrap
import itertools
from pkgutil import get_importer from pkgutil import get_importer
try: try:
...@@ -1966,6 +1967,32 @@ def find_nothing(importer, path_item, only=False): ...@@ -1966,6 +1967,32 @@ def find_nothing(importer, path_item, only=False):
register_finder(object, find_nothing) register_finder(object, find_nothing)
def _by_version_descending(names):
"""
Given a list of filenames, return them in descending order
by version number.
>>> names = 'bar', 'foo', 'Python-2.7.10.egg', 'Python-2.7.2.egg'
>>> _by_version_descending(names)
['Python-2.7.10.egg', 'Python-2.7.2.egg', 'foo', 'bar']
>>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.egg'
>>> _by_version_descending(names)
['Setuptools-1.2.3.egg', 'Setuptools-1.2.3b1.egg']
>>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.post1.egg'
>>> _by_version_descending(names)
['Setuptools-1.2.3.post1.egg', 'Setuptools-1.2.3b1.egg']
"""
def _by_version(name):
"""
Parse each component of the filename
"""
name, ext = os.path.splitext(name)
parts = itertools.chain(name.split('-'), [ext])
return [packaging.version.parse(part) for part in parts]
return sorted(names, key=_by_version, reverse=True)
def find_on_path(importer, path_item, only=False): def find_on_path(importer, path_item, only=False):
"""Yield distributions accessible on a sys.path directory""" """Yield distributions accessible on a sys.path directory"""
path_item = _normalize_cached(path_item) path_item = _normalize_cached(path_item)
...@@ -1979,11 +2006,7 @@ def find_on_path(importer, path_item, only=False): ...@@ -1979,11 +2006,7 @@ def find_on_path(importer, path_item, only=False):
) )
else: else:
# scan for .egg and .egg-info in directory # scan for .egg and .egg-info in directory
path_item_entries = _by_version_descending(os.listdir(path_item))
path_item_entries = os.listdir(path_item)
# Reverse so we find the newest version of a distribution,
path_item_entries.sort()
path_item_entries.reverse()
for entry in path_item_entries: for entry in path_item_entries:
lower = entry.lower() lower = entry.lower()
if lower.endswith('.egg-info') or lower.endswith('.dist-info'): if lower.endswith('.egg-info') or lower.endswith('.dist-info'):
......
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