diff --git a/lib/fpm/package/pyfpm/get_metadata.py b/lib/fpm/package/pyfpm/get_metadata.py index f2f305fd1d..7bf25a5382 100644 --- a/lib/fpm/package/pyfpm/get_metadata.py +++ b/lib/fpm/package/pyfpm/get_metadata.py @@ -2,11 +2,23 @@ import json import re import time +import pkg_resources # Note, the last time I coded python daily was at Google, so it's entirely # possible some of my techniques below are outdated or bad. # If you have fixes, let me know. +def select_smallest(first, second): + first_operator, first_version = first + second_operator, second_version = second + if second_operator < first_operator: + return second + elif second_operator == first_operator and second_version < first_version: + return second + + return first + + class get_metadata(Command): description = "get package metadata" user_options = [] @@ -45,44 +57,17 @@ def run(self): data["architecture"] = "all" # end if - dependencies = None - try: - dependencies = self.distribution.install_requires - except: - pass - - # In some cases (Mysql-Python) 'dependencies' is none, not empty. - if dependencies is None: - dependencies = [] - - # Some cases (like paramiko) dependencies is actually just a string, not a - # list - if isinstance(dependencies, str): - dependencies = [dependencies] - final_deps = [] - dep_re = re.compile("([^<>= ]+)(?:\s*([<>=]{1,2})\s*([^,]*))?(?:,\s*([<>=]{1,2})\s*(.*))?$") - for dep in dependencies: - # python deps are strings that look like: - # "packagename" - # "packagename >= version" - # Replace 'packagename' with 'python#{suffix}-packagename' - m = dep_re.match(dep) - if m is None: - print "Bad dep: %s" % dep - time.sleep(3) - elif m.groups()[1] is None: - name, cond, version = m.groups()[0], ">=", 0 - else: - groups = m.groups() - name, cond, version = groups[0:3] - if groups[3] is not None: - final_deps.append("%s %s %s" % (groups[0], groups[3], groups[4])) - # end if - # end if - - final_deps.append("%s %s %s" % (name, cond, version)) - # end for i in dependencies + for dep in pkg_resources.parse_requirements(self.distribution.install_requires): + try: + operator, version = reduce(select_smallest, dep.specs) + final_deps.append("%s %s %s" % ( + dep.project_name, + "=" if operator == "==" else operator, + version + )) + except TypeError as e: + final_deps.append(dep.project_name) data["dependencies"] = final_deps