You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

markers.py 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import operator
  6. import os
  7. import platform
  8. import sys
  9. from pip._vendor.pyparsing import ParseException, ParseResults, stringStart, stringEnd
  10. from pip._vendor.pyparsing import ZeroOrMore, Group, Forward, QuotedString
  11. from pip._vendor.pyparsing import Literal as L # noqa
  12. from ._compat import string_types
  13. from .specifiers import Specifier, InvalidSpecifier
  14. __all__ = [
  15. "InvalidMarker",
  16. "UndefinedComparison",
  17. "UndefinedEnvironmentName",
  18. "Marker",
  19. "default_environment",
  20. ]
  21. class InvalidMarker(ValueError):
  22. """
  23. An invalid marker was found, users should refer to PEP 508.
  24. """
  25. class UndefinedComparison(ValueError):
  26. """
  27. An invalid operation was attempted on a value that doesn't support it.
  28. """
  29. class UndefinedEnvironmentName(ValueError):
  30. """
  31. A name was attempted to be used that does not exist inside of the
  32. environment.
  33. """
  34. class Node(object):
  35. def __init__(self, value):
  36. self.value = value
  37. def __str__(self):
  38. return str(self.value)
  39. def __repr__(self):
  40. return "<{0}({1!r})>".format(self.__class__.__name__, str(self))
  41. def serialize(self):
  42. raise NotImplementedError
  43. class Variable(Node):
  44. def serialize(self):
  45. return str(self)
  46. class Value(Node):
  47. def serialize(self):
  48. return '"{0}"'.format(self)
  49. class Op(Node):
  50. def serialize(self):
  51. return str(self)
  52. VARIABLE = (
  53. L("implementation_version")
  54. | L("platform_python_implementation")
  55. | L("implementation_name")
  56. | L("python_full_version")
  57. | L("platform_release")
  58. | L("platform_version")
  59. | L("platform_machine")
  60. | L("platform_system")
  61. | L("python_version")
  62. | L("sys_platform")
  63. | L("os_name")
  64. | L("os.name")
  65. | L("sys.platform") # PEP-345
  66. | L("platform.version") # PEP-345
  67. | L("platform.machine") # PEP-345
  68. | L("platform.python_implementation") # PEP-345
  69. | L("python_implementation") # PEP-345
  70. | L("extra") # undocumented setuptools legacy
  71. )
  72. ALIASES = {
  73. "os.name": "os_name",
  74. "sys.platform": "sys_platform",
  75. "platform.version": "platform_version",
  76. "platform.machine": "platform_machine",
  77. "platform.python_implementation": "platform_python_implementation",
  78. "python_implementation": "platform_python_implementation",
  79. }
  80. VARIABLE.setParseAction(lambda s, l, t: Variable(ALIASES.get(t[0], t[0])))
  81. VERSION_CMP = (
  82. L("===") | L("==") | L(">=") | L("<=") | L("!=") | L("~=") | L(">") | L("<")
  83. )
  84. MARKER_OP = VERSION_CMP | L("not in") | L("in")
  85. MARKER_OP.setParseAction(lambda s, l, t: Op(t[0]))
  86. MARKER_VALUE = QuotedString("'") | QuotedString('"')
  87. MARKER_VALUE.setParseAction(lambda s, l, t: Value(t[0]))
  88. BOOLOP = L("and") | L("or")
  89. MARKER_VAR = VARIABLE | MARKER_VALUE
  90. MARKER_ITEM = Group(MARKER_VAR + MARKER_OP + MARKER_VAR)
  91. MARKER_ITEM.setParseAction(lambda s, l, t: tuple(t[0]))
  92. LPAREN = L("(").suppress()
  93. RPAREN = L(")").suppress()
  94. MARKER_EXPR = Forward()
  95. MARKER_ATOM = MARKER_ITEM | Group(LPAREN + MARKER_EXPR + RPAREN)
  96. MARKER_EXPR << MARKER_ATOM + ZeroOrMore(BOOLOP + MARKER_EXPR)
  97. MARKER = stringStart + MARKER_EXPR + stringEnd
  98. def _coerce_parse_result(results):
  99. if isinstance(results, ParseResults):
  100. return [_coerce_parse_result(i) for i in results]
  101. else:
  102. return results
  103. def _format_marker(marker, first=True):
  104. assert isinstance(marker, (list, tuple, string_types))
  105. # Sometimes we have a structure like [[...]] which is a single item list
  106. # where the single item is itself it's own list. In that case we want skip
  107. # the rest of this function so that we don't get extraneous () on the
  108. # outside.
  109. if (
  110. isinstance(marker, list)
  111. and len(marker) == 1
  112. and isinstance(marker[0], (list, tuple))
  113. ):
  114. return _format_marker(marker[0])
  115. if isinstance(marker, list):
  116. inner = (_format_marker(m, first=False) for m in marker)
  117. if first:
  118. return " ".join(inner)
  119. else:
  120. return "(" + " ".join(inner) + ")"
  121. elif isinstance(marker, tuple):
  122. return " ".join([m.serialize() for m in marker])
  123. else:
  124. return marker
  125. _operators = {
  126. "in": lambda lhs, rhs: lhs in rhs,
  127. "not in": lambda lhs, rhs: lhs not in rhs,
  128. "<": operator.lt,
  129. "<=": operator.le,
  130. "==": operator.eq,
  131. "!=": operator.ne,
  132. ">=": operator.ge,
  133. ">": operator.gt,
  134. }
  135. def _eval_op(lhs, op, rhs):
  136. try:
  137. spec = Specifier("".join([op.serialize(), rhs]))
  138. except InvalidSpecifier:
  139. pass
  140. else:
  141. return spec.contains(lhs)
  142. oper = _operators.get(op.serialize())
  143. if oper is None:
  144. raise UndefinedComparison(
  145. "Undefined {0!r} on {1!r} and {2!r}.".format(op, lhs, rhs)
  146. )
  147. return oper(lhs, rhs)
  148. _undefined = object()
  149. def _get_env(environment, name):
  150. value = environment.get(name, _undefined)
  151. if value is _undefined:
  152. raise UndefinedEnvironmentName(
  153. "{0!r} does not exist in evaluation environment.".format(name)
  154. )
  155. return value
  156. def _evaluate_markers(markers, environment):
  157. groups = [[]]
  158. for marker in markers:
  159. assert isinstance(marker, (list, tuple, string_types))
  160. if isinstance(marker, list):
  161. groups[-1].append(_evaluate_markers(marker, environment))
  162. elif isinstance(marker, tuple):
  163. lhs, op, rhs = marker
  164. if isinstance(lhs, Variable):
  165. lhs_value = _get_env(environment, lhs.value)
  166. rhs_value = rhs.value
  167. else:
  168. lhs_value = lhs.value
  169. rhs_value = _get_env(environment, rhs.value)
  170. groups[-1].append(_eval_op(lhs_value, op, rhs_value))
  171. else:
  172. assert marker in ["and", "or"]
  173. if marker == "or":
  174. groups.append([])
  175. return any(all(item) for item in groups)
  176. def format_full_version(info):
  177. version = "{0.major}.{0.minor}.{0.micro}".format(info)
  178. kind = info.releaselevel
  179. if kind != "final":
  180. version += kind[0] + str(info.serial)
  181. return version
  182. def default_environment():
  183. if hasattr(sys, "implementation"):
  184. iver = format_full_version(sys.implementation.version)
  185. implementation_name = sys.implementation.name
  186. else:
  187. iver = "0"
  188. implementation_name = ""
  189. return {
  190. "implementation_name": implementation_name,
  191. "implementation_version": iver,
  192. "os_name": os.name,
  193. "platform_machine": platform.machine(),
  194. "platform_release": platform.release(),
  195. "platform_system": platform.system(),
  196. "platform_version": platform.version(),
  197. "python_full_version": platform.python_version(),
  198. "platform_python_implementation": platform.python_implementation(),
  199. "python_version": platform.python_version()[:3],
  200. "sys_platform": sys.platform,
  201. }
  202. class Marker(object):
  203. def __init__(self, marker):
  204. try:
  205. self._markers = _coerce_parse_result(MARKER.parseString(marker))
  206. except ParseException as e:
  207. err_str = "Invalid marker: {0!r}, parse error at {1!r}".format(
  208. marker, marker[e.loc : e.loc + 8]
  209. )
  210. raise InvalidMarker(err_str)
  211. def __str__(self):
  212. return _format_marker(self._markers)
  213. def __repr__(self):
  214. return "<Marker({0!r})>".format(str(self))
  215. def evaluate(self, environment=None):
  216. """Evaluate a marker.
  217. Return the boolean from evaluating the given marker against the
  218. environment. environment is an optional argument to override all or
  219. part of the determined environment.
  220. The environment is determined from the current Python process.
  221. """
  222. current_environment = default_environment()
  223. if environment is not None:
  224. current_environment.update(environment)
  225. return _evaluate_markers(self._markers, current_environment)