markers.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. import operator
  5. import os
  6. import platform
  7. import sys
  8. from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  9. from ._parser import (
  10. MarkerAtom,
  11. MarkerList,
  12. Op,
  13. Value,
  14. Variable,
  15. parse_marker as _parse_marker,
  16. )
  17. from ._tokenizer import ParserSyntaxError
  18. from .specifiers import InvalidSpecifier, Specifier
  19. from .utils import canonicalize_name
  20. __all__ = [
  21. "InvalidMarker",
  22. "UndefinedComparison",
  23. "UndefinedEnvironmentName",
  24. "Marker",
  25. "default_environment",
  26. ]
  27. Operator = Callable[[str, str], bool]
  28. class InvalidMarker(ValueError):
  29. """
  30. An invalid marker was found, users should refer to PEP 508.
  31. """
  32. class UndefinedComparison(ValueError):
  33. """
  34. An invalid operation was attempted on a value that doesn't support it.
  35. """
  36. class UndefinedEnvironmentName(ValueError):
  37. """
  38. A name was attempted to be used that does not exist inside of the
  39. environment.
  40. """
  41. def _normalize_extra_values(results: Any) -> Any:
  42. """
  43. Normalize extra values.
  44. """
  45. if isinstance(results[0], tuple):
  46. lhs, op, rhs = results[0]
  47. if isinstance(lhs, Variable) and lhs.value == "extra":
  48. normalized_extra = canonicalize_name(rhs.value)
  49. rhs = Value(normalized_extra)
  50. elif isinstance(rhs, Variable) and rhs.value == "extra":
  51. normalized_extra = canonicalize_name(lhs.value)
  52. lhs = Value(normalized_extra)
  53. results[0] = lhs, op, rhs
  54. return results
  55. def _format_marker(
  56. marker: Union[List[str], MarkerAtom, str], first: Optional[bool] = True
  57. ) -> str:
  58. assert isinstance(marker, (list, tuple, str))
  59. # Sometimes we have a structure like [[...]] which is a single item list
  60. # where the single item is itself it's own list. In that case we want skip
  61. # the rest of this function so that we don't get extraneous () on the
  62. # outside.
  63. if (
  64. isinstance(marker, list)
  65. and len(marker) == 1
  66. and isinstance(marker[0], (list, tuple))
  67. ):
  68. return _format_marker(marker[0])
  69. if isinstance(marker, list):
  70. inner = (_format_marker(m, first=False) for m in marker)
  71. if first:
  72. return " ".join(inner)
  73. else:
  74. return "(" + " ".join(inner) + ")"
  75. elif isinstance(marker, tuple):
  76. return " ".join([m.serialize() for m in marker])
  77. else:
  78. return marker
  79. _operators: Dict[str, Operator] = {
  80. "in": lambda lhs, rhs: lhs in rhs,
  81. "not in": lambda lhs, rhs: lhs not in rhs,
  82. "<": operator.lt,
  83. "<=": operator.le,
  84. "==": operator.eq,
  85. "!=": operator.ne,
  86. ">=": operator.ge,
  87. ">": operator.gt,
  88. }
  89. def _eval_op(lhs: str, op: Op, rhs: str) -> bool:
  90. try:
  91. spec = Specifier("".join([op.serialize(), rhs]))
  92. except InvalidSpecifier:
  93. pass
  94. else:
  95. return spec.contains(lhs, prereleases=True)
  96. oper: Optional[Operator] = _operators.get(op.serialize())
  97. if oper is None:
  98. raise UndefinedComparison(f"Undefined {op!r} on {lhs!r} and {rhs!r}.")
  99. return oper(lhs, rhs)
  100. def _normalize(*values: str, key: str) -> Tuple[str, ...]:
  101. # PEP 685 – Comparison of extra names for optional distribution dependencies
  102. # https://peps.python.org/pep-0685/
  103. # > When comparing extra names, tools MUST normalize the names being
  104. # > compared using the semantics outlined in PEP 503 for names
  105. if key == "extra":
  106. return tuple(canonicalize_name(v) for v in values)
  107. # other environment markers don't have such standards
  108. return values
  109. def _evaluate_markers(markers: MarkerList, environment: Dict[str, str]) -> bool:
  110. groups: List[List[bool]] = [[]]
  111. for marker in markers:
  112. assert isinstance(marker, (list, tuple, str))
  113. if isinstance(marker, list):
  114. groups[-1].append(_evaluate_markers(marker, environment))
  115. elif isinstance(marker, tuple):
  116. lhs, op, rhs = marker
  117. if isinstance(lhs, Variable):
  118. environment_key = lhs.value
  119. lhs_value = environment[environment_key]
  120. rhs_value = rhs.value
  121. else:
  122. lhs_value = lhs.value
  123. environment_key = rhs.value
  124. rhs_value = environment[environment_key]
  125. lhs_value, rhs_value = _normalize(lhs_value, rhs_value, key=environment_key)
  126. groups[-1].append(_eval_op(lhs_value, op, rhs_value))
  127. else:
  128. assert marker in ["and", "or"]
  129. if marker == "or":
  130. groups.append([])
  131. return any(all(item) for item in groups)
  132. def format_full_version(info: "sys._version_info") -> str:
  133. version = "{0.major}.{0.minor}.{0.micro}".format(info)
  134. kind = info.releaselevel
  135. if kind != "final":
  136. version += kind[0] + str(info.serial)
  137. return version
  138. def default_environment() -> Dict[str, str]:
  139. iver = format_full_version(sys.implementation.version)
  140. implementation_name = sys.implementation.name
  141. return {
  142. "implementation_name": implementation_name,
  143. "implementation_version": iver,
  144. "os_name": os.name,
  145. "platform_machine": platform.machine(),
  146. "platform_release": platform.release(),
  147. "platform_system": platform.system(),
  148. "platform_version": platform.version(),
  149. "python_full_version": platform.python_version(),
  150. "platform_python_implementation": platform.python_implementation(),
  151. "python_version": ".".join(platform.python_version_tuple()[:2]),
  152. "sys_platform": sys.platform,
  153. }
  154. class Marker:
  155. def __init__(self, marker: str) -> None:
  156. # Note: We create a Marker object without calling this constructor in
  157. # packaging.requirements.Requirement. If any additional logic is
  158. # added here, make sure to mirror/adapt Requirement.
  159. try:
  160. self._markers = _normalize_extra_values(_parse_marker(marker))
  161. # The attribute `_markers` can be described in terms of a recursive type:
  162. # MarkerList = List[Union[Tuple[Node, ...], str, MarkerList]]
  163. #
  164. # For example, the following expression:
  165. # python_version > "3.6" or (python_version == "3.6" and os_name == "unix")
  166. #
  167. # is parsed into:
  168. # [
  169. # (<Variable('python_version')>, <Op('>')>, <Value('3.6')>),
  170. # 'and',
  171. # [
  172. # (<Variable('python_version')>, <Op('==')>, <Value('3.6')>),
  173. # 'or',
  174. # (<Variable('os_name')>, <Op('==')>, <Value('unix')>)
  175. # ]
  176. # ]
  177. except ParserSyntaxError as e:
  178. raise InvalidMarker(str(e)) from e
  179. def __str__(self) -> str:
  180. return _format_marker(self._markers)
  181. def __repr__(self) -> str:
  182. return f"<Marker('{self}')>"
  183. def __hash__(self) -> int:
  184. return hash((self.__class__.__name__, str(self)))
  185. def __eq__(self, other: Any) -> bool:
  186. if not isinstance(other, Marker):
  187. return NotImplemented
  188. return str(self) == str(other)
  189. def evaluate(self, environment: Optional[Dict[str, str]] = None) -> bool:
  190. """Evaluate a marker.
  191. Return the boolean from evaluating the given marker against the
  192. environment. environment is an optional argument to override all or
  193. part of the determined environment.
  194. The environment is determined from the current Python process.
  195. """
  196. current_environment = default_environment()
  197. current_environment["extra"] = ""
  198. if environment is not None:
  199. current_environment.update(environment)
  200. # The API used to allow setting extra to None. We need to handle this
  201. # case for backwards compatibility.
  202. if current_environment["extra"] is None:
  203. current_environment["extra"] = ""
  204. return _evaluate_markers(self._markers, current_environment)