Skip to content

Commit

Permalink
Use packaging.version.Version to sort filenames by the version of the…
Browse files Browse the repository at this point in the history
… package they represent. Alternate implementation of that proposed in #829. Also ref #629.
  • Loading branch information
jaraco committed Nov 4, 2016
1 parent 51b10c3 commit b163acc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Expand Up @@ -2,6 +2,14 @@
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
-------

Expand Down
33 changes: 28 additions & 5 deletions pkg_resources/__init__.py
Expand Up @@ -36,6 +36,7 @@
import email.parser
import tempfile
import textwrap
import itertools
from pkgutil import get_importer

try:
Expand Down Expand Up @@ -1966,6 +1967,32 @@ def find_nothing(importer, path_item, only=False):
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):
"""Yield distributions accessible on a sys.path directory"""
path_item = _normalize_cached(path_item)
Expand All @@ -1979,11 +2006,7 @@ def find_on_path(importer, path_item, only=False):
)
else:
# scan for .egg and .egg-info in directory

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()
path_item_entries = _by_version_descending(os.listdir(path_item))
for entry in path_item_entries:
lower = entry.lower()
if lower.endswith('.egg-info') or lower.endswith('.dist-info'):
Expand Down

0 comments on commit b163acc

Please sign in to comment.