From 954df6a8fba515baab7940c617eb919c28fdd2f2 Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Mon, 5 Jan 2015 12:14:52 +0000 Subject: Treat hyphens and underscores equivalently There isn't yet an official spec for distribution names in python, however there is a draft at http://legacy.python.org/dev/peps/pep-0426/#name In particular, "All comparisons of distribution names MUST be case insensitive, and MUST consider hyphens and underscores to be equivalent." pkg_resource.parse_requirements will replace any underscores in the package name as hyphens, so when we search pypi we need to look for the package name with underscores as well as with hyphens. --- baserockimport/exts/importer_python_common.py | 23 +++++++++++++++++------ baserockimport/exts/python.find_deps | 6 +++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/baserockimport/exts/importer_python_common.py b/baserockimport/exts/importer_python_common.py index 18f0847..1bbc6e8 100644 --- a/baserockimport/exts/importer_python_common.py +++ b/baserockimport/exts/importer_python_common.py @@ -53,15 +53,25 @@ def name_or_closest(client, package_name): If no case insensitive match can be found then we return None''' - results = client.package_releases(package_name) + # According to http://legacy.python.org/dev/peps/pep-0426/#name + # "All comparisons of distribution names MUST be case insensitive, + # and MUST consider hyphens and underscores to be equivalent." + # + # so look for both the hyphenated version that is passed to this function + # and the underscored version. + underscored_package_name = package_name.replace('-', '_') - if len(results) > 0: - logging.debug('Found package %s' % package_name) - return package_name + for name in [package_name, underscored_package_name]: + results = client.package_releases(name) + + if len(results) > 0: + logging.debug('Found package %s' % name) + return name logging.debug("Couldn't find exact match for %s," "searching for a similar match" % package_name) - results = client.search({'name': package_name}) + + results = client.search({'name': [package_name, underscored_package_name]}) logging.debug("Got the following similarly named packages '%s': %s" % (package_name, str([(result['name'], result['version']) @@ -70,7 +80,8 @@ def name_or_closest(client, package_name): logging.debug('Filtering for exact case-insensitive matches') results = [result for result in results - if result['name'].lower() == package_name.lower()] + if result['name'].lower() in + [package_name.lower(), underscored_package_name.lower()]] logging.debug('Filtered results: %s' % results) diff --git a/baserockimport/exts/python.find_deps b/baserockimport/exts/python.find_deps index cca0947..2b6b618 100755 --- a/baserockimport/exts/python.find_deps +++ b/baserockimport/exts/python.find_deps @@ -301,8 +301,12 @@ def find_runtime_deps(source, name, version=None, use_requirements_file=False): logging.debug("Resolved specs for %s: %s" % (name, ss)) logging.debug("Removing root package from specs") + # filter out "root" package - specsets = {k: v for (k, v) in ss.iteritems() if k != name} + # hyphens and underscores are treated as equivalents + # in distribution names + specsets = {k: v for (k, v) in ss.iteritems() + if k not in [name, name.replace('_', '-')]} versions = resolve_versions(specsets) logging.debug('Resolved versions: %s' % versions) -- cgit v1.2.1