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.

329 lines
9.3KB

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