Skip to content

Commit

Permalink
Fix issue pypa#1433: parse requirements in markers
Browse files Browse the repository at this point in the history
* InstallRequirement supports PEP 426 markers
* RequirementSet.add_requirement() ignores an InstallRequirement if
  markers don't match.
  • Loading branch information
vstinner authored and dstufft committed Nov 20, 2014
1 parent 1da8b75 commit e236842
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pip/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class InstallRequirement(object):

def __init__(self, req, comes_from, source_dir=None, editable=False,
url=None, as_egg=False, update=True, prereleases=None,
editable_options=None, pycompile=True):
editable_options=None, pycompile=True, markers=None):
self.extras = ()
if isinstance(req, six.string_types):
req = pkg_resources.Requirement.parse(req)
Expand All @@ -61,6 +61,7 @@ def __init__(self, req, comes_from, source_dir=None, editable=False,
self.editable_options = editable_options
self.url = url
self.as_egg = as_egg
self.markers = markers
self._egg_info_path = None
# This holds the pkg_resources.Distribution object if this requirement
# is already available:
Expand Down Expand Up @@ -115,6 +116,13 @@ def from_line(cls, name, comes_from=None, prereleases=None):
requirement, directory containing 'setup.py', filename, or URL.
"""
url = None
if ';' in name:
name, markers = name.split(';', 1)
markers = markers.strip()
if not markers:
markers = None
else:
markers = None
name = name.strip()
req = None
path = os.path.normpath(os.path.abspath(name))
Expand Down Expand Up @@ -165,7 +173,8 @@ def from_line(cls, name, comes_from=None, prereleases=None):
else:
req = name

return cls(req, comes_from, url=url, prereleases=prereleases)
return cls(req, comes_from, url=url, prereleases=prereleases,
markers=markers)

def __str__(self):
if self.req:
Expand Down Expand Up @@ -726,6 +735,12 @@ def _clean_zip_name(self, name, prefix):
name = name.replace(os.path.sep, '/')
return name

def match_markers(self):
if self.markers is not None:
return markers.interpret(self.markers)
else:
return True

def install(self, install_options, global_options=(), root=None):
if self.editable:
self.install_editable(install_options, global_options)
Expand Down
5 changes: 5 additions & 0 deletions pip/req/req_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def __str__(self):
return ' '.join([str(req.req) for req in reqs])

def add_requirement(self, install_req):
if not install_req.match_markers():
logger.notify("Ignore %s: markers %r don't match",
install_req.name, install_req.markers)
return

name = install_req.name
install_req.as_egg = self.as_egg
install_req.use_user_site = self.use_user_site
Expand Down

0 comments on commit e236842

Please sign in to comment.