markers.py 12 KB

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