markers.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 annotations
  5. import operator
  6. import os
  7. import platform
  8. import sys
  9. from typing import Any, Callable, TypedDict, cast
  10. from ._parser import MarkerAtom, MarkerList, Op, Value, Variable
  11. from ._parser import parse_marker as _parse_marker
  12. from ._tokenizer import ParserSyntaxError
  13. from .specifiers import InvalidSpecifier, Specifier
  14. from .utils import canonicalize_name
  15. __all__ = [
  16. "InvalidMarker",
  17. "Marker",
  18. "UndefinedComparison",
  19. "UndefinedEnvironmentName",
  20. "default_environment",
  21. ]
  22. Operator = Callable[[str, str], bool]
  23. class InvalidMarker(ValueError):
  24. """
  25. An invalid marker was found, users should refer to PEP 508.
  26. """
  27. class UndefinedComparison(ValueError):
  28. """
  29. An invalid operation was attempted on a value that doesn't support it.
  30. """
  31. class UndefinedEnvironmentName(ValueError):
  32. """
  33. A name was attempted to be used that does not exist inside of the
  34. environment.
  35. """
  36. class Environment(TypedDict):
  37. implementation_name: str
  38. """The implementation's identifier, e.g. ``'cpython'``."""
  39. implementation_version: str
  40. """
  41. The implementation's version, e.g. ``'3.13.0a2'`` for CPython 3.13.0a2, or
  42. ``'7.3.13'`` for PyPy3.10 v7.3.13.
  43. """
  44. os_name: str
  45. """
  46. The value of :py:data:`os.name`. The name of the operating system dependent module
  47. imported, e.g. ``'posix'``.
  48. """
  49. platform_machine: str
  50. """
  51. Returns the machine type, e.g. ``'i386'``.
  52. An empty string if the value cannot be determined.
  53. """
  54. platform_release: str
  55. """
  56. The system's release, e.g. ``'2.2.0'`` or ``'NT'``.
  57. An empty string if the value cannot be determined.
  58. """
  59. platform_system: str
  60. """
  61. The system/OS name, e.g. ``'Linux'``, ``'Windows'`` or ``'Java'``.
  62. An empty string if the value cannot be determined.
  63. """
  64. platform_version: str
  65. """
  66. The system's release version, e.g. ``'#3 on degas'``.
  67. An empty string if the value cannot be determined.
  68. """
  69. python_full_version: str
  70. """
  71. The Python version as string ``'major.minor.patchlevel'``.
  72. Note that unlike the Python :py:data:`sys.version`, this value will always include
  73. the patchlevel (it defaults to 0).
  74. """
  75. platform_python_implementation: str
  76. """
  77. A string identifying the Python implementation, e.g. ``'CPython'``.
  78. """
  79. python_version: str
  80. """The Python version as string ``'major.minor'``."""
  81. sys_platform: str
  82. """
  83. This string contains a platform identifier that can be used to append
  84. platform-specific components to :py:data:`sys.path`, for instance.
  85. For Unix systems, except on Linux and AIX, this is the lowercased OS name as
  86. returned by ``uname -s`` with the first part of the version as returned by
  87. ``uname -r`` appended, e.g. ``'sunos5'`` or ``'freebsd8'``, at the time when Python
  88. was built.
  89. """
  90. def _normalize_extra_values(results: Any) -> Any:
  91. """
  92. Normalize extra values.
  93. """
  94. if isinstance(results[0], tuple):
  95. lhs, op, rhs = results[0]
  96. if isinstance(lhs, Variable) and lhs.value == "extra":
  97. normalized_extra = canonicalize_name(rhs.value)
  98. rhs = Value(normalized_extra)
  99. elif isinstance(rhs, Variable) and rhs.value == "extra":
  100. normalized_extra = canonicalize_name(lhs.value)
  101. lhs = Value(normalized_extra)
  102. results[0] = lhs, op, rhs
  103. return results
  104. def _format_marker(
  105. marker: list[str] | MarkerAtom | str, first: bool | None = True
  106. ) -> str:
  107. assert isinstance(marker, (list, tuple, str))
  108. # Sometimes we have a structure like [[...]] which is a single item list
  109. # where the single item is itself it's own list. In that case we want skip
  110. # the rest of this function so that we don't get extraneous () on the
  111. # outside.
  112. if (
  113. isinstance(marker, list)
  114. and len(marker) == 1
  115. and isinstance(marker[0], (list, tuple))
  116. ):
  117. return _format_marker(marker[0])
  118. if isinstance(marker, list):
  119. inner = (_format_marker(m, first=False) for m in marker)
  120. if first:
  121. return " ".join(inner)
  122. else:
  123. return "(" + " ".join(inner) + ")"
  124. elif isinstance(marker, tuple):
  125. return " ".join([m.serialize() for m in marker])
  126. else:
  127. return marker
  128. _operators: dict[str, Operator] = {
  129. "in": lambda lhs, rhs: lhs in rhs,
  130. "not in": lambda lhs, rhs: lhs not in rhs,
  131. "<": operator.lt,
  132. "<=": operator.le,
  133. "==": operator.eq,
  134. "!=": operator.ne,
  135. ">=": operator.ge,
  136. ">": operator.gt,
  137. }
  138. def _eval_op(lhs: str, op: Op, rhs: str) -> bool:
  139. try:
  140. spec = Specifier("".join([op.serialize(), rhs]))
  141. except InvalidSpecifier:
  142. pass
  143. else:
  144. return spec.contains(lhs, prereleases=True)
  145. oper: Operator | None = _operators.get(op.serialize())
  146. if oper is None:
  147. raise UndefinedComparison(f"Undefined {op!r} on {lhs!r} and {rhs!r}.")
  148. return oper(lhs, rhs)
  149. def _normalize(*values: str, key: str) -> tuple[str, ...]:
  150. # PEP 685 – Comparison of extra names for optional distribution dependencies
  151. # https://peps.python.org/pep-0685/
  152. # > When comparing extra names, tools MUST normalize the names being
  153. # > compared using the semantics outlined in PEP 503 for names
  154. if key == "extra":
  155. return tuple(canonicalize_name(v) for v in values)
  156. # other environment markers don't have such standards
  157. return values
  158. def _evaluate_markers(markers: MarkerList, environment: dict[str, str]) -> bool:
  159. groups: list[list[bool]] = [[]]
  160. for marker in markers:
  161. assert isinstance(marker, (list, tuple, str))
  162. if isinstance(marker, list):
  163. groups[-1].append(_evaluate_markers(marker, environment))
  164. elif isinstance(marker, tuple):
  165. lhs, op, rhs = marker
  166. if isinstance(lhs, Variable):
  167. environment_key = lhs.value
  168. lhs_value = environment[environment_key]
  169. rhs_value = rhs.value
  170. else:
  171. lhs_value = lhs.value
  172. environment_key = rhs.value
  173. rhs_value = environment[environment_key]
  174. lhs_value, rhs_value = _normalize(lhs_value, rhs_value, key=environment_key)
  175. groups[-1].append(_eval_op(lhs_value, op, rhs_value))
  176. else:
  177. assert marker in ["and", "or"]
  178. if marker == "or":
  179. groups.append([])
  180. return any(all(item) for item in groups)
  181. def format_full_version(info: sys._version_info) -> str:
  182. version = f"{info.major}.{info.minor}.{info.micro}"
  183. kind = info.releaselevel
  184. if kind != "final":
  185. version += kind[0] + str(info.serial)
  186. return version
  187. def default_environment() -> Environment:
  188. iver = format_full_version(sys.implementation.version)
  189. implementation_name = sys.implementation.name
  190. return {
  191. "implementation_name": implementation_name,
  192. "implementation_version": iver,
  193. "os_name": os.name,
  194. "platform_machine": platform.machine(),
  195. "platform_release": platform.release(),
  196. "platform_system": platform.system(),
  197. "platform_version": platform.version(),
  198. "python_full_version": platform.python_version(),
  199. "platform_python_implementation": platform.python_implementation(),
  200. "python_version": ".".join(platform.python_version_tuple()[:2]),
  201. "sys_platform": sys.platform,
  202. }
  203. class Marker:
  204. def __init__(self, marker: str) -> None:
  205. # Note: We create a Marker object without calling this constructor in
  206. # packaging.requirements.Requirement. If any additional logic is
  207. # added here, make sure to mirror/adapt Requirement.
  208. try:
  209. self._markers = _normalize_extra_values(_parse_marker(marker))
  210. # The attribute `_markers` can be described in terms of a recursive type:
  211. # MarkerList = List[Union[Tuple[Node, ...], str, MarkerList]]
  212. #
  213. # For example, the following expression:
  214. # python_version > "3.6" or (python_version == "3.6" and os_name == "unix")
  215. #
  216. # is parsed into:
  217. # [
  218. # (<Variable('python_version')>, <Op('>')>, <Value('3.6')>),
  219. # 'and',
  220. # [
  221. # (<Variable('python_version')>, <Op('==')>, <Value('3.6')>),
  222. # 'or',
  223. # (<Variable('os_name')>, <Op('==')>, <Value('unix')>)
  224. # ]
  225. # ]
  226. except ParserSyntaxError as e:
  227. raise InvalidMarker(str(e)) from e
  228. def __str__(self) -> str:
  229. return _format_marker(self._markers)
  230. def __repr__(self) -> str:
  231. return f"<Marker('{self}')>"
  232. def __hash__(self) -> int:
  233. return hash((self.__class__.__name__, str(self)))
  234. def __eq__(self, other: Any) -> bool:
  235. if not isinstance(other, Marker):
  236. return NotImplemented
  237. return str(self) == str(other)
  238. def evaluate(self, environment: dict[str, str] | None = None) -> bool:
  239. """Evaluate a marker.
  240. Return the boolean from evaluating the given marker against the
  241. environment. environment is an optional argument to override all or
  242. part of the determined environment.
  243. The environment is determined from the current Python process.
  244. """
  245. current_environment = cast("dict[str, str]", default_environment())
  246. current_environment["extra"] = ""
  247. if environment is not None:
  248. current_environment.update(environment)
  249. # The API used to allow setting extra to None. We need to handle this
  250. # case for backwards compatibility.
  251. if current_environment["extra"] is None:
  252. current_environment["extra"] = ""
  253. return _evaluate_markers(
  254. self._markers, _repair_python_full_version(current_environment)
  255. )
  256. def _repair_python_full_version(env: dict[str, str]) -> dict[str, str]:
  257. """
  258. Work around platform.python_version() returning something that is not PEP 440
  259. compliant for non-tagged Python builds.
  260. """
  261. if env["python_full_version"].endswith("+"):
  262. env["python_full_version"] += "local"
  263. return env