mirror of https://github.com/pypa/pip
Make environment marker parsing consistent (#3626)
* Update pkg_resources to setuptools 20.4, remove _markerlib _markerlib was part of what we pulled in from setuptools and was depended on by pkg_resources; it's totally gone from setuptools so it can totally go here. * Use packaging.markers to evaluate markers instead of distlib * Update packaging to 16.7
This commit is contained in:
parent
ae6a886331
commit
449914f9dd
|
@ -92,18 +92,17 @@ situation that we expect pip to be used and not mandate some external mechanism
|
|||
such as OS packages.
|
||||
|
||||
|
||||
_markerlib and pkg_resources
|
||||
----------------------------
|
||||
pkg_resources
|
||||
-------------
|
||||
|
||||
_markerlib and pkg_resources has been pulled in from setuptools 19.4
|
||||
pkg_resources has been pulled in from setuptools 20.4
|
||||
|
||||
|
||||
Modifications
|
||||
-------------
|
||||
|
||||
* html5lib has been modified to import six from pip._vendor
|
||||
* pkg_resources has been modified to import _markerlib from pip._vendor
|
||||
* markerlib has been modified to import its API from pip._vendor
|
||||
* pkg_resources has been modified to import its externs from pip._vendor
|
||||
* CacheControl has been modified to import its dependencies from pip._vendor
|
||||
* packaging has been modified to import its dependencies from pip._vendor.
|
||||
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
try:
|
||||
import ast
|
||||
from pip._vendor._markerlib.markers import default_environment, compile, interpret
|
||||
except ImportError:
|
||||
if 'ast' in globals():
|
||||
raise
|
||||
def default_environment():
|
||||
return {}
|
||||
def compile(marker):
|
||||
def marker_fn(environment=None, override=None):
|
||||
# 'empty markers are True' heuristic won't install extra deps.
|
||||
return not marker.strip()
|
||||
marker_fn.__doc__ = marker
|
||||
return marker_fn
|
||||
def interpret(marker, environment=None, override=None):
|
||||
return compile(marker)()
|
|
@ -1,119 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""Interpret PEP 345 environment markers.
|
||||
|
||||
EXPR [in|==|!=|not in] EXPR [or|and] ...
|
||||
|
||||
where EXPR belongs to any of those:
|
||||
|
||||
python_version = '%s.%s' % (sys.version_info[0], sys.version_info[1])
|
||||
python_full_version = sys.version.split()[0]
|
||||
os.name = os.name
|
||||
sys.platform = sys.platform
|
||||
platform.version = platform.version()
|
||||
platform.machine = platform.machine()
|
||||
platform.python_implementation = platform.python_implementation()
|
||||
a free string, like '2.6', or 'win32'
|
||||
"""
|
||||
|
||||
__all__ = ['default_environment', 'compile', 'interpret']
|
||||
|
||||
import ast
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
import weakref
|
||||
|
||||
_builtin_compile = compile
|
||||
|
||||
try:
|
||||
from platform import python_implementation
|
||||
except ImportError:
|
||||
if os.name == "java":
|
||||
# Jython 2.5 has ast module, but not platform.python_implementation() function.
|
||||
def python_implementation():
|
||||
return "Jython"
|
||||
else:
|
||||
raise
|
||||
|
||||
|
||||
# restricted set of variables
|
||||
_VARS = {'sys.platform': sys.platform,
|
||||
'python_version': '%s.%s' % sys.version_info[:2],
|
||||
# FIXME parsing sys.platform is not reliable, but there is no other
|
||||
# way to get e.g. 2.7.2+, and the PEP is defined with sys.version
|
||||
'python_full_version': sys.version.split(' ', 1)[0],
|
||||
'os.name': os.name,
|
||||
'platform.version': platform.version(),
|
||||
'platform.machine': platform.machine(),
|
||||
'platform.python_implementation': python_implementation(),
|
||||
'extra': None # wheel extension
|
||||
}
|
||||
|
||||
for var in list(_VARS.keys()):
|
||||
if '.' in var:
|
||||
_VARS[var.replace('.', '_')] = _VARS[var]
|
||||
|
||||
def default_environment():
|
||||
"""Return copy of default PEP 385 globals dictionary."""
|
||||
return dict(_VARS)
|
||||
|
||||
class ASTWhitelist(ast.NodeTransformer):
|
||||
def __init__(self, statement):
|
||||
self.statement = statement # for error messages
|
||||
|
||||
ALLOWED = (ast.Compare, ast.BoolOp, ast.Attribute, ast.Name, ast.Load, ast.Str)
|
||||
# Bool operations
|
||||
ALLOWED += (ast.And, ast.Or)
|
||||
# Comparison operations
|
||||
ALLOWED += (ast.Eq, ast.Gt, ast.GtE, ast.In, ast.Is, ast.IsNot, ast.Lt, ast.LtE, ast.NotEq, ast.NotIn)
|
||||
|
||||
def visit(self, node):
|
||||
"""Ensure statement only contains allowed nodes."""
|
||||
if not isinstance(node, self.ALLOWED):
|
||||
raise SyntaxError('Not allowed in environment markers.\n%s\n%s' %
|
||||
(self.statement,
|
||||
(' ' * node.col_offset) + '^'))
|
||||
return ast.NodeTransformer.visit(self, node)
|
||||
|
||||
def visit_Attribute(self, node):
|
||||
"""Flatten one level of attribute access."""
|
||||
new_node = ast.Name("%s.%s" % (node.value.id, node.attr), node.ctx)
|
||||
return ast.copy_location(new_node, node)
|
||||
|
||||
def parse_marker(marker):
|
||||
tree = ast.parse(marker, mode='eval')
|
||||
new_tree = ASTWhitelist(marker).generic_visit(tree)
|
||||
return new_tree
|
||||
|
||||
def compile_marker(parsed_marker):
|
||||
return _builtin_compile(parsed_marker, '<environment marker>', 'eval',
|
||||
dont_inherit=True)
|
||||
|
||||
_cache = weakref.WeakValueDictionary()
|
||||
|
||||
def compile(marker):
|
||||
"""Return compiled marker as a function accepting an environment dict."""
|
||||
try:
|
||||
return _cache[marker]
|
||||
except KeyError:
|
||||
pass
|
||||
if not marker.strip():
|
||||
def marker_fn(environment=None, override=None):
|
||||
""""""
|
||||
return True
|
||||
else:
|
||||
compiled_marker = compile_marker(parse_marker(marker))
|
||||
def marker_fn(environment=None, override=None):
|
||||
"""override updates environment"""
|
||||
if override is None:
|
||||
override = {}
|
||||
if environment is None:
|
||||
environment = default_environment()
|
||||
environment.update(override)
|
||||
return eval(compiled_marker, environment)
|
||||
marker_fn.__doc__ = marker
|
||||
_cache[marker] = marker_fn
|
||||
return _cache[marker]
|
||||
|
||||
def interpret(marker, environment=None):
|
||||
return compile(marker)(environment)
|
|
@ -12,7 +12,7 @@ __title__ = "packaging"
|
|||
__summary__ = "Core utilities for Python packages"
|
||||
__uri__ = "https://github.com/pypa/packaging"
|
||||
|
||||
__version__ = "16.6"
|
||||
__version__ = "16.7"
|
||||
|
||||
__author__ = "Donald Stufft and individual contributors"
|
||||
__email__ = "donald@stufft.io"
|
||||
|
|
|
@ -80,9 +80,18 @@ VARIABLE = (
|
|||
L("platform.version") | # PEP-345
|
||||
L("platform.machine") | # PEP-345
|
||||
L("platform.python_implementation") | # PEP-345
|
||||
L("python_implementation") | # undocumented setuptools legacy
|
||||
L("extra")
|
||||
)
|
||||
VARIABLE.setParseAction(lambda s, l, t: Variable(t[0].replace('.', '_')))
|
||||
ALIASES = {
|
||||
'os.name': 'os_name',
|
||||
'sys.platform': 'sys_platform',
|
||||
'platform.version': 'platform_version',
|
||||
'platform.machine': 'platform_machine',
|
||||
'platform.python_implementation': 'platform_python_implementation',
|
||||
'python_implementation': 'platform_python_implementation'
|
||||
}
|
||||
VARIABLE.setParseAction(lambda s, l, t: Variable(ALIASES.get(t[0], t[0])))
|
||||
|
||||
VERSION_CMP = (
|
||||
L("===") |
|
||||
|
|
|
@ -46,7 +46,7 @@ except ImportError:
|
|||
import imp as _imp
|
||||
|
||||
from pip._vendor import six
|
||||
from pip._vendor.six.moves import urllib, map
|
||||
from pip._vendor.six.moves import urllib, map, filter
|
||||
|
||||
# capture these to bypass sandboxing
|
||||
from os import utime
|
||||
|
@ -60,10 +60,11 @@ except ImportError:
|
|||
from os import open as os_open
|
||||
from os.path import isdir, split
|
||||
|
||||
# Avoid try/except due to potential problems with delayed import mechanisms.
|
||||
if sys.version_info >= (3, 3) and sys.implementation.name == "cpython":
|
||||
try:
|
||||
import importlib.machinery as importlib_machinery
|
||||
else:
|
||||
# access attribute to force import under delayed import mechanisms.
|
||||
importlib_machinery.__name__
|
||||
except ImportError:
|
||||
importlib_machinery = None
|
||||
|
||||
try:
|
||||
|
@ -74,11 +75,10 @@ except ImportError:
|
|||
from pip._vendor import packaging
|
||||
__import__('pip._vendor.packaging.version')
|
||||
__import__('pip._vendor.packaging.specifiers')
|
||||
__import__('pip._vendor.packaging.requirements')
|
||||
__import__('pip._vendor.packaging.markers')
|
||||
|
||||
|
||||
filter = six.moves.filter
|
||||
map = six.moves.map
|
||||
|
||||
if (3, 0) < sys.version_info < (3, 3):
|
||||
msg = (
|
||||
"Support for Python 3.0-3.2 has been dropped. Future versions "
|
||||
|
@ -1176,22 +1176,23 @@ class ResourceManager:
|
|||
old_exc = sys.exc_info()[1]
|
||||
cache_path = self.extraction_path or get_default_cache()
|
||||
|
||||
err = ExtractionError("""Can't extract file(s) to egg cache
|
||||
tmpl = textwrap.dedent("""
|
||||
Can't extract file(s) to egg cache
|
||||
|
||||
The following error occurred while trying to extract file(s) to the Python egg
|
||||
cache:
|
||||
The following error occurred while trying to extract file(s) to the Python egg
|
||||
cache:
|
||||
|
||||
%s
|
||||
{old_exc}
|
||||
|
||||
The Python egg cache directory is currently set to:
|
||||
The Python egg cache directory is currently set to:
|
||||
|
||||
%s
|
||||
{cache_path}
|
||||
|
||||
Perhaps your account does not have write access to this directory? You can
|
||||
change the cache directory by setting the PYTHON_EGG_CACHE environment
|
||||
variable to point to an accessible directory.
|
||||
""" % (old_exc, cache_path)
|
||||
)
|
||||
Perhaps your account does not have write access to this directory? You can
|
||||
change the cache directory by setting the PYTHON_EGG_CACHE environment
|
||||
variable to point to an accessible directory.
|
||||
""").lstrip()
|
||||
err = ExtractionError(tmpl.format(**locals()))
|
||||
err.manager = self
|
||||
err.cache_path = cache_path
|
||||
err.original_error = old_exc
|
||||
|
@ -1386,202 +1387,34 @@ def to_filename(name):
|
|||
return name.replace('-','_')
|
||||
|
||||
|
||||
class MarkerEvaluation(object):
|
||||
values = {
|
||||
'os_name': lambda: os.name,
|
||||
'sys_platform': lambda: sys.platform,
|
||||
'python_full_version': platform.python_version,
|
||||
'python_version': lambda: platform.python_version()[:3],
|
||||
'platform_version': platform.version,
|
||||
'platform_machine': platform.machine,
|
||||
'platform_python_implementation': platform.python_implementation,
|
||||
'python_implementation': platform.python_implementation,
|
||||
}
|
||||
def invalid_marker(text):
|
||||
"""
|
||||
Validate text as a PEP 508 environment marker; return an exception
|
||||
if invalid or False otherwise.
|
||||
"""
|
||||
try:
|
||||
evaluate_marker(text)
|
||||
except SyntaxError as e:
|
||||
e.filename = None
|
||||
e.lineno = None
|
||||
return e
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def is_invalid_marker(cls, text):
|
||||
"""
|
||||
Validate text as a PEP 426 environment marker; return an exception
|
||||
if invalid or False otherwise.
|
||||
"""
|
||||
try:
|
||||
cls.evaluate_marker(text)
|
||||
except SyntaxError as e:
|
||||
return cls.normalize_exception(e)
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def normalize_exception(exc):
|
||||
"""
|
||||
Given a SyntaxError from a marker evaluation, normalize the error
|
||||
message:
|
||||
- Remove indications of filename and line number.
|
||||
- Replace platform-specific error messages with standard error
|
||||
messages.
|
||||
"""
|
||||
subs = {
|
||||
'unexpected EOF while parsing': 'invalid syntax',
|
||||
'parenthesis is never closed': 'invalid syntax',
|
||||
}
|
||||
exc.filename = None
|
||||
exc.lineno = None
|
||||
exc.msg = subs.get(exc.msg, exc.msg)
|
||||
return exc
|
||||
def evaluate_marker(text, extra=None):
|
||||
"""
|
||||
Evaluate a PEP 508 environment marker.
|
||||
Return a boolean indicating the marker result in this environment.
|
||||
Raise SyntaxError if marker is invalid.
|
||||
|
||||
@classmethod
|
||||
def and_test(cls, nodelist):
|
||||
# MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
|
||||
items = [
|
||||
cls.interpret(nodelist[i])
|
||||
for i in range(1, len(nodelist), 2)
|
||||
]
|
||||
return functools.reduce(operator.and_, items)
|
||||
This implementation uses the 'pyparsing' module.
|
||||
"""
|
||||
try:
|
||||
marker = packaging.markers.Marker(text)
|
||||
return marker.evaluate()
|
||||
except packaging.markers.InvalidMarker as e:
|
||||
raise SyntaxError(e)
|
||||
|
||||
@classmethod
|
||||
def test(cls, nodelist):
|
||||
# MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
|
||||
items = [
|
||||
cls.interpret(nodelist[i])
|
||||
for i in range(1, len(nodelist), 2)
|
||||
]
|
||||
return functools.reduce(operator.or_, items)
|
||||
|
||||
@classmethod
|
||||
def atom(cls, nodelist):
|
||||
t = nodelist[1][0]
|
||||
if t == token.LPAR:
|
||||
if nodelist[2][0] == token.RPAR:
|
||||
raise SyntaxError("Empty parentheses")
|
||||
return cls.interpret(nodelist[2])
|
||||
msg = "Language feature not supported in environment markers"
|
||||
raise SyntaxError(msg)
|
||||
|
||||
@classmethod
|
||||
def comparison(cls, nodelist):
|
||||
if len(nodelist) > 4:
|
||||
msg = "Chained comparison not allowed in environment markers"
|
||||
raise SyntaxError(msg)
|
||||
comp = nodelist[2][1]
|
||||
cop = comp[1]
|
||||
if comp[0] == token.NAME:
|
||||
if len(nodelist[2]) == 3:
|
||||
if cop == 'not':
|
||||
cop = 'not in'
|
||||
else:
|
||||
cop = 'is not'
|
||||
try:
|
||||
cop = cls.get_op(cop)
|
||||
except KeyError:
|
||||
msg = repr(cop) + " operator not allowed in environment markers"
|
||||
raise SyntaxError(msg)
|
||||
return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3]))
|
||||
|
||||
@classmethod
|
||||
def get_op(cls, op):
|
||||
ops = {
|
||||
symbol.test: cls.test,
|
||||
symbol.and_test: cls.and_test,
|
||||
symbol.atom: cls.atom,
|
||||
symbol.comparison: cls.comparison,
|
||||
'not in': lambda x, y: x not in y,
|
||||
'in': lambda x, y: x in y,
|
||||
'==': operator.eq,
|
||||
'!=': operator.ne,
|
||||
'<': operator.lt,
|
||||
'>': operator.gt,
|
||||
'<=': operator.le,
|
||||
'>=': operator.ge,
|
||||
}
|
||||
if hasattr(symbol, 'or_test'):
|
||||
ops[symbol.or_test] = cls.test
|
||||
return ops[op]
|
||||
|
||||
@classmethod
|
||||
def evaluate_marker(cls, text, extra=None):
|
||||
"""
|
||||
Evaluate a PEP 426 environment marker on CPython 2.4+.
|
||||
Return a boolean indicating the marker result in this environment.
|
||||
Raise SyntaxError if marker is invalid.
|
||||
|
||||
This implementation uses the 'parser' module, which is not implemented
|
||||
on
|
||||
Jython and has been superseded by the 'ast' module in Python 2.6 and
|
||||
later.
|
||||
"""
|
||||
return cls.interpret(parser.expr(text).totuple(1)[1])
|
||||
|
||||
@staticmethod
|
||||
def _translate_metadata2(env):
|
||||
"""
|
||||
Markerlib implements Metadata 1.2 (PEP 345) environment markers.
|
||||
Translate the variables to Metadata 2.0 (PEP 426).
|
||||
"""
|
||||
return dict(
|
||||
(key.replace('.', '_'), value)
|
||||
for key, value in env.items()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _markerlib_evaluate(cls, text):
|
||||
"""
|
||||
Evaluate a PEP 426 environment marker using markerlib.
|
||||
Return a boolean indicating the marker result in this environment.
|
||||
Raise SyntaxError if marker is invalid.
|
||||
"""
|
||||
from pip._vendor import _markerlib
|
||||
|
||||
env = cls._translate_metadata2(_markerlib.default_environment())
|
||||
try:
|
||||
result = _markerlib.interpret(text, env)
|
||||
except NameError as e:
|
||||
raise SyntaxError(e.args[0])
|
||||
return result
|
||||
|
||||
if 'parser' not in globals():
|
||||
# Fall back to less-complete _markerlib implementation if 'parser' module
|
||||
# is not available.
|
||||
evaluate_marker = _markerlib_evaluate
|
||||
|
||||
@classmethod
|
||||
def interpret(cls, nodelist):
|
||||
while len(nodelist)==2: nodelist = nodelist[1]
|
||||
try:
|
||||
op = cls.get_op(nodelist[0])
|
||||
except KeyError:
|
||||
raise SyntaxError("Comparison or logical expression expected")
|
||||
return op(nodelist)
|
||||
|
||||
@classmethod
|
||||
def evaluate(cls, nodelist):
|
||||
while len(nodelist)==2: nodelist = nodelist[1]
|
||||
kind = nodelist[0]
|
||||
name = nodelist[1]
|
||||
if kind==token.NAME:
|
||||
try:
|
||||
op = cls.values[name]
|
||||
except KeyError:
|
||||
raise SyntaxError("Unknown name %r" % name)
|
||||
return op()
|
||||
if kind==token.STRING:
|
||||
s = nodelist[1]
|
||||
if not cls._safe_string(s):
|
||||
raise SyntaxError(
|
||||
"Only plain strings allowed in environment markers")
|
||||
return s[1:-1]
|
||||
msg = "Language feature not supported in environment markers"
|
||||
raise SyntaxError(msg)
|
||||
|
||||
@staticmethod
|
||||
def _safe_string(cand):
|
||||
return (
|
||||
cand[:1] in "'\"" and
|
||||
not cand.startswith('"""') and
|
||||
not cand.startswith("'''") and
|
||||
'\\' not in cand
|
||||
)
|
||||
|
||||
invalid_marker = MarkerEvaluation.is_invalid_marker
|
||||
evaluate_marker = MarkerEvaluation.evaluate_marker
|
||||
|
||||
class NullProvider:
|
||||
"""Try to implement resources and metadata for arbitrary PEP 302 loaders"""
|
||||
|
@ -1727,10 +1560,13 @@ class DefaultProvider(EggProvider):
|
|||
with open(path, 'rb') as stream:
|
||||
return stream.read()
|
||||
|
||||
register_loader_type(type(None), DefaultProvider)
|
||||
@classmethod
|
||||
def _register(cls):
|
||||
loader_cls = getattr(importlib_machinery, 'SourceFileLoader',
|
||||
type(None))
|
||||
register_loader_type(loader_cls, cls)
|
||||
|
||||
if importlib_machinery is not None:
|
||||
register_loader_type(importlib_machinery.SourceFileLoader, DefaultProvider)
|
||||
DefaultProvider._register()
|
||||
|
||||
|
||||
class EmptyProvider(NullProvider):
|
||||
|
@ -2136,7 +1972,7 @@ def find_on_path(importer, path_item, only=False):
|
|||
break
|
||||
register_finder(pkgutil.ImpImporter, find_on_path)
|
||||
|
||||
if importlib_machinery is not None:
|
||||
if hasattr(importlib_machinery, 'FileFinder'):
|
||||
register_finder(importlib_machinery.FileFinder, find_on_path)
|
||||
|
||||
_declare_state('dict', _namespace_handlers={})
|
||||
|
@ -2182,19 +2018,29 @@ def _handle_ns(packageName, path_item):
|
|||
path = module.__path__
|
||||
path.append(subpath)
|
||||
loader.load_module(packageName)
|
||||
|
||||
# Rebuild mod.__path__ ensuring that all entries are ordered
|
||||
# corresponding to their sys.path order
|
||||
sys_path= [(p and _normalize_cached(p) or p) for p in sys.path]
|
||||
def sort_key(p):
|
||||
parts = p.split(os.sep)
|
||||
parts = parts[:-(packageName.count('.') + 1)]
|
||||
return sys_path.index(_normalize_cached(os.sep.join(parts)))
|
||||
|
||||
path.sort(key=sort_key)
|
||||
module.__path__[:] = [_normalize_cached(p) for p in path]
|
||||
_rebuild_mod_path(path, packageName, module)
|
||||
return subpath
|
||||
|
||||
|
||||
def _rebuild_mod_path(orig_path, package_name, module):
|
||||
"""
|
||||
Rebuild module.__path__ ensuring that all entries are ordered
|
||||
corresponding to their sys.path order
|
||||
"""
|
||||
sys_path = [_normalize_cached(p) for p in sys.path]
|
||||
def position_in_sys_path(path):
|
||||
"""
|
||||
Return the ordinal of the path based on its position in sys.path
|
||||
"""
|
||||
path_parts = path.split(os.sep)
|
||||
module_parts = package_name.count('.') + 1
|
||||
parts = path_parts[:-module_parts]
|
||||
return sys_path.index(_normalize_cached(os.sep.join(parts)))
|
||||
|
||||
orig_path.sort(key=position_in_sys_path)
|
||||
module.__path__[:] = [_normalize_cached(p) for p in orig_path]
|
||||
|
||||
|
||||
def declare_namespace(packageName):
|
||||
"""Declare that package 'packageName' is a namespace package"""
|
||||
|
||||
|
@ -2253,7 +2099,7 @@ def file_ns_handler(importer, path_item, packageName, module):
|
|||
register_namespace_handler(pkgutil.ImpImporter, file_ns_handler)
|
||||
register_namespace_handler(zipimport.zipimporter, file_ns_handler)
|
||||
|
||||
if importlib_machinery is not None:
|
||||
if hasattr(importlib_machinery, 'FileFinder'):
|
||||
register_namespace_handler(importlib_machinery.FileFinder, file_ns_handler)
|
||||
|
||||
|
||||
|
@ -2303,18 +2149,6 @@ def yield_lines(strs):
|
|||
for s in yield_lines(ss):
|
||||
yield s
|
||||
|
||||
# whitespace and comment
|
||||
LINE_END = re.compile(r"\s*(#.*)?$").match
|
||||
# line continuation
|
||||
CONTINUE = re.compile(r"\s*\\\s*(#.*)?$").match
|
||||
# Distribution or extra
|
||||
DISTRO = re.compile(r"\s*((\w|[-.])+)").match
|
||||
# ver. info
|
||||
VERSION = re.compile(r"\s*(<=?|>=?|===?|!=|~=)\s*((\w|[-.*_!+])+)").match
|
||||
# comma between items
|
||||
COMMA = re.compile(r"\s*,").match
|
||||
OBRACKET = re.compile(r"\s*\[").match
|
||||
CBRACKET = re.compile(r"\s*\]").match
|
||||
MODULE = re.compile(r"\w+(\.\w+)*$").match
|
||||
EGG_NAME = re.compile(
|
||||
r"""
|
||||
|
@ -2853,34 +2687,18 @@ class DistInfoDistribution(Distribution):
|
|||
self.__dep_map = self._compute_dependencies()
|
||||
return self.__dep_map
|
||||
|
||||
def _preparse_requirement(self, requires_dist):
|
||||
"""Convert 'Foobar (1); baz' to ('Foobar ==1', 'baz')
|
||||
Split environment marker, add == prefix to version specifiers as
|
||||
necessary, and remove parenthesis.
|
||||
"""
|
||||
parts = requires_dist.split(';', 1) + ['']
|
||||
distvers = parts[0].strip()
|
||||
mark = parts[1].strip()
|
||||
distvers = re.sub(self.EQEQ, r"\1==\2\3", distvers)
|
||||
distvers = distvers.replace('(', '').replace(')', '')
|
||||
return (distvers, mark)
|
||||
|
||||
def _compute_dependencies(self):
|
||||
"""Recompute this distribution's dependencies."""
|
||||
from pip._vendor._markerlib import compile as compile_marker
|
||||
dm = self.__dep_map = {None: []}
|
||||
|
||||
reqs = []
|
||||
# Including any condition expressions
|
||||
for req in self._parsed_pkg_info.get_all('Requires-Dist') or []:
|
||||
distvers, mark = self._preparse_requirement(req)
|
||||
parsed = next(parse_requirements(distvers))
|
||||
parsed.marker_fn = compile_marker(mark)
|
||||
reqs.append(parsed)
|
||||
reqs.extend(parse_requirements(req))
|
||||
|
||||
def reqs_for_extra(extra):
|
||||
for req in reqs:
|
||||
if req.marker_fn(override={'extra':extra}):
|
||||
if not req.marker or req.marker.evaluate({'extra': extra}):
|
||||
yield req
|
||||
|
||||
common = frozenset(reqs_for_extra(None))
|
||||
|
@ -2926,85 +2744,38 @@ def parse_requirements(strs):
|
|||
# create a steppable iterator, so we can handle \-continuations
|
||||
lines = iter(yield_lines(strs))
|
||||
|
||||
def scan_list(ITEM, TERMINATOR, line, p, groups, item_name):
|
||||
|
||||
items = []
|
||||
|
||||
while not TERMINATOR(line, p):
|
||||
if CONTINUE(line, p):
|
||||
try:
|
||||
line = next(lines)
|
||||
p = 0
|
||||
except StopIteration:
|
||||
msg = "\\ must not appear on the last nonblank line"
|
||||
raise RequirementParseError(msg)
|
||||
|
||||
match = ITEM(line, p)
|
||||
if not match:
|
||||
msg = "Expected " + item_name + " in"
|
||||
raise RequirementParseError(msg, line, "at", line[p:])
|
||||
|
||||
items.append(match.group(*groups))
|
||||
p = match.end()
|
||||
|
||||
match = COMMA(line, p)
|
||||
if match:
|
||||
# skip the comma
|
||||
p = match.end()
|
||||
elif not TERMINATOR(line, p):
|
||||
msg = "Expected ',' or end-of-list in"
|
||||
raise RequirementParseError(msg, line, "at", line[p:])
|
||||
|
||||
match = TERMINATOR(line, p)
|
||||
# skip the terminator, if any
|
||||
if match:
|
||||
p = match.end()
|
||||
return line, p, items
|
||||
|
||||
for line in lines:
|
||||
match = DISTRO(line)
|
||||
if not match:
|
||||
raise RequirementParseError("Missing distribution spec", line)
|
||||
project_name = match.group(1)
|
||||
p = match.end()
|
||||
extras = []
|
||||
|
||||
match = OBRACKET(line, p)
|
||||
if match:
|
||||
p = match.end()
|
||||
line, p, extras = scan_list(
|
||||
DISTRO, CBRACKET, line, p, (1,), "'extra' name"
|
||||
)
|
||||
|
||||
line, p, specs = scan_list(VERSION, LINE_END, line, p, (1, 2),
|
||||
"version spec")
|
||||
specs = [(op, val) for op, val in specs]
|
||||
yield Requirement(project_name, specs, extras)
|
||||
# Drop comments -- a hash without a space may be in a URL.
|
||||
if ' #' in line:
|
||||
line = line[:line.find(' #')]
|
||||
# If there is a line continuation, drop it, and append the next line.
|
||||
if line.endswith('\\'):
|
||||
line = line[:-2].strip()
|
||||
line += next(lines)
|
||||
yield Requirement(line)
|
||||
|
||||
|
||||
class Requirement:
|
||||
def __init__(self, project_name, specs, extras):
|
||||
class Requirement(packaging.requirements.Requirement):
|
||||
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:
|
||||
super(Requirement, self).__init__(requirement_string)
|
||||
except packaging.requirements.InvalidRequirement as e:
|
||||
raise RequirementParseError(str(e))
|
||||
self.unsafe_name = self.name
|
||||
project_name = safe_name(self.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.specs = [
|
||||
(spec.operator, spec.version) for spec in self.specifier]
|
||||
self.extras = tuple(map(safe_extra, self.extras))
|
||||
self.hashCmp = (
|
||||
self.key,
|
||||
self.specifier,
|
||||
frozenset(self.extras),
|
||||
str(self.marker) if self.marker else None,
|
||||
)
|
||||
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)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (
|
||||
isinstance(other, Requirement) and
|
||||
|
|
|
@ -7,6 +7,6 @@ CacheControl==0.11.6
|
|||
lockfile==0.12.2
|
||||
progress==1.2
|
||||
ipaddress==1.0.16 # Only needed on 2.6 and 2.7
|
||||
packaging==16.6
|
||||
packaging==16.7
|
||||
pyparsing==2.1.0
|
||||
retrying==1.3.3
|
||||
|
|
|
@ -15,8 +15,8 @@ from distutils.util import change_root
|
|||
from email.parser import FeedParser
|
||||
|
||||
from pip._vendor import pkg_resources, six
|
||||
from pip._vendor.distlib.markers import interpret as markers_interpret
|
||||
from pip._vendor.packaging import specifiers
|
||||
from pip._vendor.packaging.markers import Marker
|
||||
from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
|
||||
from pip._vendor.packaging.utils import canonicalize_name
|
||||
from pip._vendor.packaging.version import Version
|
||||
|
@ -814,7 +814,7 @@ class InstallRequirement(object):
|
|||
|
||||
def match_markers(self):
|
||||
if self.markers is not None:
|
||||
return markers_interpret(self.markers)
|
||||
return Marker(self.markers).evaluate()
|
||||
else:
|
||||
return True
|
||||
|
||||
|
|
Loading…
Reference in New Issue