Skip to content

Commit

Permalink
let pkg_resources do the parsing of the specs. When multiple specs ar…
Browse files Browse the repository at this point in the history
…e found prefer order is "<=", "==", ">=" and next the version number.
  • Loading branch information
specialunderwear committed Apr 12, 2012
1 parent fa062f0 commit 4d7c5ea
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions lib/fpm/package/pyfpm/get_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4d7c5ea

Please sign in to comment.