Skip to content

Commit

Permalink
Shift requirement parsing inside Requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
s-t-e-v-e-n-k committed Mar 1, 2016
1 parent a46fd83 commit 0ed33b7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 41 deletions.
42 changes: 18 additions & 24 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2693,15 +2693,11 @@ def _compute_dependencies(self):
reqs = []
# Including any condition expressions
for req in self._parsed_pkg_info.get_all('Requires-Dist') or []:
current_req = packaging.requirements.Requirement(req)
specs = _parse_requirement_specs(current_req)
parsed = Requirement(current_req.name, specs, current_req.extras)
parsed._marker = current_req.marker
reqs.append(parsed)
reqs.extend(parse_requirements(req))

def reqs_for_extra(extra):
for req in reqs:
if not req._marker or req._marker.evaluate({'extra': extra}):
if not req.marker or req.marker.evaluate({'extra': extra}):
yield req

common = frozenset(reqs_for_extra(None))
Expand Down Expand Up @@ -2739,10 +2735,6 @@ def __str__(self):
return ' '.join(self.args)


def _parse_requirement_specs(req):
return [(spec.operator, spec.version) for spec in req.specifier]


def parse_requirements(strs):
"""Yield ``Requirement`` objects for each specification in `strs`
Expand All @@ -2759,33 +2751,35 @@ def parse_requirements(strs):
if line.endswith('\\'):
line = line[:-2].strip()
line += next(lines)
req = packaging.requirements.Requirement(line)
specs = _parse_requirement_specs(req)
yield Requirement(req.name, specs, req.extras)
yield Requirement(line)


class Requirement:
def __init__(self, project_name, specs, extras):
def __init__(self, requirement_string):
"""DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
self.unsafe_name, project_name = project_name, safe_name(project_name)
try:
self.req = packaging.requirements.Requirement(requirement_string)
except packaging.requirements.InvalidRequirement as e:
raise RequirementParseError(str(e))
self.unsafe_name = self.req.name
project_name = safe_name(self.req.name)
self.project_name, self.key = project_name, project_name.lower()
self.specifier = packaging.specifiers.SpecifierSet(
",".join(["".join([x, y]) for x, y in specs])
)
self.specs = specs
self.extras = tuple(map(safe_extra, extras))
self.specifier = self.req.specifier
self.specs = [
(spec.operator, spec.version) for spec in self.req.specifier]
self.extras = tuple(map(safe_extra, self.req.extras))
self.marker = self.req.marker
self.url = self.req.url
self.hashCmp = (
self.key,
self.specifier,
frozenset(self.extras),
str(self.marker)
)
self.__hash = hash(self.hashCmp)

def __str__(self):
extras = ','.join(self.extras)
if extras:
extras = '[%s]' % extras
return '%s%s%s' % (self.project_name, extras, self.specifier)
return str(self.req)

def __eq__(self, other):
return (
Expand Down
25 changes: 13 additions & 12 deletions pkg_resources/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,22 +353,22 @@ def testBasics(self):
r = Requirement.parse("Twisted>=1.2")
assert str(r) == "Twisted>=1.2"
assert repr(r) == "Requirement.parse('Twisted>=1.2')"
assert r == Requirement("Twisted", [('>=','1.2')], ())
assert r == Requirement("twisTed", [('>=','1.2')], ())
assert r != Requirement("Twisted", [('>=','2.0')], ())
assert r != Requirement("Zope", [('>=','1.2')], ())
assert r != Requirement("Zope", [('>=','3.0')], ())
assert r != Requirement.parse("Twisted[extras]>=1.2")
assert r == Requirement("Twisted>=1.2")
assert r == Requirement("twisTed>=1.2")
assert r != Requirement("Twisted>=2.0")
assert r != Requirement("Zope>=1.2")
assert r != Requirement("Zope>=3.0")
assert r != Requirement("Twisted[extras]>=1.2")

def testOrdering(self):
r1 = Requirement("Twisted", [('==','1.2c1'),('>=','1.2')], ())
r2 = Requirement("Twisted", [('>=','1.2'),('==','1.2c1')], ())
r1 = Requirement("Twisted==1.2c1,>=1.2")
r2 = Requirement("Twisted>=1.2,==1.2c1")
assert r1 == r2
assert str(r1) == str(r2)
assert str(r2) == "Twisted==1.2c1,>=1.2"

def testBasicContains(self):
r = Requirement("Twisted", [('>=','1.2')], ())
r = Requirement("Twisted>=1.2")
foo_dist = Distribution.from_filename("FooPkg-1.3_1.egg")
twist11 = Distribution.from_filename("Twisted-1.1.egg")
twist12 = Distribution.from_filename("Twisted-1.2.egg")
Expand All @@ -394,6 +394,7 @@ def testOptionsAndHashing(self):
"twisted",
packaging.specifiers.SpecifierSet(">=1.2"),
frozenset(["foo","bar"]),
'None'
))
)

Expand Down Expand Up @@ -485,17 +486,17 @@ def testSimpleRequirements(self):
assert (
list(parse_requirements('Twis-Ted>=1.2-1'))
==
[Requirement('Twis-Ted',[('>=','1.2-1')], ())]
[Requirement('Twis-Ted>=1.2-1')]
)
assert (
list(parse_requirements('Twisted >=1.2, \ # more\n<2.0'))
==
[Requirement('Twisted',[('>=','1.2'),('<','2.0')], ())]
[Requirement('Twisted>=1.2,<2.0')]
)
assert (
Requirement.parse("FooBar==1.99a3")
==
Requirement("FooBar", [('==','1.99a3')], ())
Requirement("FooBar==1.99a3")
)
with pytest.raises(ValueError):
Requirement.parse(">=2.3")
Expand Down
5 changes: 1 addition & 4 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,7 @@ def process_distribution(self, requirement, dist, deps=True, *info):
elif requirement is None or dist not in requirement:
# if we wound up with a different version, resolve what we've got
distreq = dist.as_requirement()
requirement = requirement or distreq
requirement = Requirement(
distreq.project_name, distreq.specs, requirement.extras
)
requirement = Requirement(str(distreq.req))
log.info("Processing dependencies for %s", requirement)
try:
distros = WorkingSet([]).resolve(
Expand Down
4 changes: 3 additions & 1 deletion setuptools/tests/test_dist_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def test_conditional_dependencies(self):

for d in pkg_resources.find_distributions(self.tmpdir):
assert d.requires() == requires[:1]
assert d.requires(extras=('baz',)) == requires
assert d.requires(extras=('baz',)) == [
requires[0],
pkg_resources.Requirement.parse('quux>=1.1;extra=="baz"')]
assert d.extras == ['baz']

metadata_template = DALS("""
Expand Down

0 comments on commit 0ed33b7

Please sign in to comment.