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.

485 lines
15KB

  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 logging
  5. import platform
  6. import sys
  7. import sysconfig
  8. from importlib.machinery import EXTENSION_SUFFIXES
  9. from typing import (
  10. Dict,
  11. FrozenSet,
  12. Iterable,
  13. Iterator,
  14. List,
  15. Optional,
  16. Sequence,
  17. Tuple,
  18. Union,
  19. cast,
  20. )
  21. from . import _manylinux, _musllinux
  22. logger = logging.getLogger(__name__)
  23. PythonVersion = Sequence[int]
  24. MacVersion = Tuple[int, int]
  25. INTERPRETER_SHORT_NAMES: Dict[str, str] = {
  26. "python": "py", # Generic.
  27. "cpython": "cp",
  28. "pypy": "pp",
  29. "ironpython": "ip",
  30. "jython": "jy",
  31. }
  32. _32_BIT_INTERPRETER = sys.maxsize <= 2 ** 32
  33. class Tag:
  34. """
  35. A representation of the tag triple for a wheel.
  36. Instances are considered immutable and thus are hashable. Equality checking
  37. is also supported.
  38. """
  39. __slots__ = ["_interpreter", "_abi", "_platform", "_hash"]
  40. def __init__(self, interpreter: str, abi: str, platform: str) -> None:
  41. self._interpreter = interpreter.lower()
  42. self._abi = abi.lower()
  43. self._platform = platform.lower()
  44. # The __hash__ of every single element in a Set[Tag] will be evaluated each time
  45. # that a set calls its `.disjoint()` method, which may be called hundreds of
  46. # times when scanning a page of links for packages with tags matching that
  47. # Set[Tag]. Pre-computing the value here produces significant speedups for
  48. # downstream consumers.
  49. self._hash = hash((self._interpreter, self._abi, self._platform))
  50. @property
  51. def interpreter(self) -> str:
  52. return self._interpreter
  53. @property
  54. def abi(self) -> str:
  55. return self._abi
  56. @property
  57. def platform(self) -> str:
  58. return self._platform
  59. def __eq__(self, other: object) -> bool:
  60. if not isinstance(other, Tag):
  61. return NotImplemented
  62. return (
  63. (self._hash == other._hash) # Short-circuit ASAP for perf reasons.
  64. and (self._platform == other._platform)
  65. and (self._abi == other._abi)
  66. and (self._interpreter == other._interpreter)
  67. )
  68. def __hash__(self) -> int:
  69. return self._hash
  70. def __str__(self) -> str:
  71. return f"{self._interpreter}-{self._abi}-{self._platform}"
  72. def __repr__(self) -> str:
  73. return "<{self} @ {self_id}>".format(self=self, self_id=id(self))
  74. def parse_tag(tag: str) -> FrozenSet[Tag]:
  75. """
  76. Parses the provided tag (e.g. `py3-none-any`) into a frozenset of Tag instances.
  77. Returning a set is required due to the possibility that the tag is a
  78. compressed tag set.
  79. """
  80. tags = set()
  81. interpreters, abis, platforms = tag.split("-")
  82. for interpreter in interpreters.split("."):
  83. for abi in abis.split("."):
  84. for platform_ in platforms.split("."):
  85. tags.add(Tag(interpreter, abi, platform_))
  86. return frozenset(tags)
  87. def _get_config_var(name: str, warn: bool = False) -> Union[int, str, None]:
  88. value = sysconfig.get_config_var(name)
  89. if value is None and warn:
  90. logger.debug(
  91. "Config variable '%s' is unset, Python ABI tag may be incorrect", name
  92. )
  93. return value
  94. def _normalize_string(string: str) -> str:
  95. return string.replace(".", "_").replace("-", "_")
  96. def _abi3_applies(python_version: PythonVersion) -> bool:
  97. """
  98. Determine if the Python version supports abi3.
  99. PEP 384 was first implemented in Python 3.2.
  100. """
  101. return len(python_version) > 1 and tuple(python_version) >= (3, 2)
  102. def _cpython_abis(py_version: PythonVersion, warn: bool = False) -> List[str]:
  103. py_version = tuple(py_version) # To allow for version comparison.
  104. abis = []
  105. version = _version_nodot(py_version[:2])
  106. debug = pymalloc = ucs4 = ""
  107. with_debug = _get_config_var("Py_DEBUG", warn)
  108. has_refcount = hasattr(sys, "gettotalrefcount")
  109. # Windows doesn't set Py_DEBUG, so checking for support of debug-compiled
  110. # extension modules is the best option.
  111. # https://github.com/pypa/pip/issues/3383#issuecomment-173267692
  112. has_ext = "_d.pyd" in EXTENSION_SUFFIXES
  113. if with_debug or (with_debug is None and (has_refcount or has_ext)):
  114. debug = "d"
  115. if py_version < (3, 8):
  116. with_pymalloc = _get_config_var("WITH_PYMALLOC", warn)
  117. if with_pymalloc or with_pymalloc is None:
  118. pymalloc = "m"
  119. if py_version < (3, 3):
  120. unicode_size = _get_config_var("Py_UNICODE_SIZE", warn)
  121. if unicode_size == 4 or (
  122. unicode_size is None and sys.maxunicode == 0x10FFFF
  123. ):
  124. ucs4 = "u"
  125. elif debug:
  126. # Debug builds can also load "normal" extension modules.
  127. # We can also assume no UCS-4 or pymalloc requirement.
  128. abis.append(f"cp{version}")
  129. abis.insert(
  130. 0,
  131. "cp{version}{debug}{pymalloc}{ucs4}".format(
  132. version=version, debug=debug, pymalloc=pymalloc, ucs4=ucs4
  133. ),
  134. )
  135. return abis
  136. def cpython_tags(
  137. python_version: Optional[PythonVersion] = None,
  138. abis: Optional[Iterable[str]] = None,
  139. platforms: Optional[Iterable[str]] = None,
  140. *,
  141. warn: bool = False,
  142. ) -> Iterator[Tag]:
  143. """
  144. Yields the tags for a CPython interpreter.
  145. The tags consist of:
  146. - cp<python_version>-<abi>-<platform>
  147. - cp<python_version>-abi3-<platform>
  148. - cp<python_version>-none-<platform>
  149. - cp<less than python_version>-abi3-<platform> # Older Python versions down to 3.2.
  150. If python_version only specifies a major version then user-provided ABIs and
  151. the 'none' ABItag will be used.
  152. If 'abi3' or 'none' are specified in 'abis' then they will be yielded at
  153. their normal position and not at the beginning.
  154. """
  155. if not python_version:
  156. python_version = sys.version_info[:2]
  157. interpreter = "cp{}".format(_version_nodot(python_version[:2]))
  158. if abis is None:
  159. if len(python_version) > 1:
  160. abis = _cpython_abis(python_version, warn)
  161. else:
  162. abis = []
  163. abis = list(abis)
  164. # 'abi3' and 'none' are explicitly handled later.
  165. for explicit_abi in ("abi3", "none"):
  166. try:
  167. abis.remove(explicit_abi)
  168. except ValueError:
  169. pass
  170. platforms = list(platforms or _platform_tags())
  171. for abi in abis:
  172. for platform_ in platforms:
  173. yield Tag(interpreter, abi, platform_)
  174. if _abi3_applies(python_version):
  175. yield from (Tag(interpreter, "abi3", platform_) for platform_ in platforms)
  176. yield from (Tag(interpreter, "none", platform_) for platform_ in platforms)
  177. if _abi3_applies(python_version):
  178. for minor_version in range(python_version[1] - 1, 1, -1):
  179. for platform_ in platforms:
  180. interpreter = "cp{version}".format(
  181. version=_version_nodot((python_version[0], minor_version))
  182. )
  183. yield Tag(interpreter, "abi3", platform_)
  184. def _generic_abi() -> Iterator[str]:
  185. abi = sysconfig.get_config_var("SOABI")
  186. if abi:
  187. yield _normalize_string(abi)
  188. def generic_tags(
  189. interpreter: Optional[str] = None,
  190. abis: Optional[Iterable[str]] = None,
  191. platforms: Optional[Iterable[str]] = None,
  192. *,
  193. warn: bool = False,
  194. ) -> Iterator[Tag]:
  195. """
  196. Yields the tags for a generic interpreter.
  197. The tags consist of:
  198. - <interpreter>-<abi>-<platform>
  199. The "none" ABI will be added if it was not explicitly provided.
  200. """
  201. if not interpreter:
  202. interp_name = interpreter_name()
  203. interp_version = interpreter_version(warn=warn)
  204. interpreter = "".join([interp_name, interp_version])
  205. if abis is None:
  206. abis = _generic_abi()
  207. platforms = list(platforms or _platform_tags())
  208. abis = list(abis)
  209. if "none" not in abis:
  210. abis.append("none")
  211. for abi in abis:
  212. for platform_ in platforms:
  213. yield Tag(interpreter, abi, platform_)
  214. def _py_interpreter_range(py_version: PythonVersion) -> Iterator[str]:
  215. """
  216. Yields Python versions in descending order.
  217. After the latest version, the major-only version will be yielded, and then
  218. all previous versions of that major version.
  219. """
  220. if len(py_version) > 1:
  221. yield "py{version}".format(version=_version_nodot(py_version[:2]))
  222. yield "py{major}".format(major=py_version[0])
  223. if len(py_version) > 1:
  224. for minor in range(py_version[1] - 1, -1, -1):
  225. yield "py{version}".format(version=_version_nodot((py_version[0], minor)))
  226. def compatible_tags(
  227. python_version: Optional[PythonVersion] = None,
  228. interpreter: Optional[str] = None,
  229. platforms: Optional[Iterable[str]] = None,
  230. ) -> Iterator[Tag]:
  231. """
  232. Yields the sequence of tags that are compatible with a specific version of Python.
  233. The tags consist of:
  234. - py*-none-<platform>
  235. - <interpreter>-none-any # ... if `interpreter` is provided.
  236. - py*-none-any
  237. """
  238. if not python_version:
  239. python_version = sys.version_info[:2]
  240. platforms = list(platforms or _platform_tags())
  241. for version in _py_interpreter_range(python_version):
  242. for platform_ in platforms:
  243. yield Tag(version, "none", platform_)
  244. if interpreter:
  245. yield Tag(interpreter, "none", "any")
  246. for version in _py_interpreter_range(python_version):
  247. yield Tag(version, "none", "any")
  248. def _mac_arch(arch: str, is_32bit: bool = _32_BIT_INTERPRETER) -> str:
  249. if not is_32bit:
  250. return arch
  251. if arch.startswith("ppc"):
  252. return "ppc"
  253. return "i386"
  254. def _mac_binary_formats(version: MacVersion, cpu_arch: str) -> List[str]:
  255. formats = [cpu_arch]
  256. if cpu_arch == "x86_64":
  257. if version < (10, 4):
  258. return []
  259. formats.extend(["intel", "fat64", "fat32"])
  260. elif cpu_arch == "i386":
  261. if version < (10, 4):
  262. return []
  263. formats.extend(["intel", "fat32", "fat"])
  264. elif cpu_arch == "ppc64":
  265. # TODO: Need to care about 32-bit PPC for ppc64 through 10.2?
  266. if version > (10, 5) or version < (10, 4):
  267. return []
  268. formats.append("fat64")
  269. elif cpu_arch == "ppc":
  270. if version > (10, 6):
  271. return []
  272. formats.extend(["fat32", "fat"])
  273. if cpu_arch in {"arm64", "x86_64"}:
  274. formats.append("universal2")
  275. if cpu_arch in {"x86_64", "i386", "ppc64", "ppc", "intel"}:
  276. formats.append("universal")
  277. return formats
  278. def mac_platforms(
  279. version: Optional[MacVersion] = None, arch: Optional[str] = None
  280. ) -> Iterator[str]:
  281. """
  282. Yields the platform tags for a macOS system.
  283. The `version` parameter is a two-item tuple specifying the macOS version to
  284. generate platform tags for. The `arch` parameter is the CPU architecture to
  285. generate platform tags for. Both parameters default to the appropriate value
  286. for the current system.
  287. """
  288. version_str, _, cpu_arch = platform.mac_ver()
  289. if version is None:
  290. version = cast("MacVersion", tuple(map(int, version_str.split(".")[:2])))
  291. else:
  292. version = version
  293. if arch is None:
  294. arch = _mac_arch(cpu_arch)
  295. else:
  296. arch = arch
  297. if (10, 0) <= version and version < (11, 0):
  298. # Prior to Mac OS 11, each yearly release of Mac OS bumped the
  299. # "minor" version number. The major version was always 10.
  300. for minor_version in range(version[1], -1, -1):
  301. compat_version = 10, minor_version
  302. binary_formats = _mac_binary_formats(compat_version, arch)
  303. for binary_format in binary_formats:
  304. yield "macosx_{major}_{minor}_{binary_format}".format(
  305. major=10, minor=minor_version, binary_format=binary_format
  306. )
  307. if version >= (11, 0):
  308. # Starting with Mac OS 11, each yearly release bumps the major version
  309. # number. The minor versions are now the midyear updates.
  310. for major_version in range(version[0], 10, -1):
  311. compat_version = major_version, 0
  312. binary_formats = _mac_binary_formats(compat_version, arch)
  313. for binary_format in binary_formats:
  314. yield "macosx_{major}_{minor}_{binary_format}".format(
  315. major=major_version, minor=0, binary_format=binary_format
  316. )
  317. if version >= (11, 0):
  318. # Mac OS 11 on x86_64 is compatible with binaries from previous releases.
  319. # Arm64 support was introduced in 11.0, so no Arm binaries from previous
  320. # releases exist.
  321. #
  322. # However, the "universal2" binary format can have a
  323. # macOS version earlier than 11.0 when the x86_64 part of the binary supports
  324. # that version of macOS.
  325. if arch == "x86_64":
  326. for minor_version in range(16, 3, -1):
  327. compat_version = 10, minor_version
  328. binary_formats = _mac_binary_formats(compat_version, arch)
  329. for binary_format in binary_formats:
  330. yield "macosx_{major}_{minor}_{binary_format}".format(
  331. major=compat_version[0],
  332. minor=compat_version[1],
  333. binary_format=binary_format,
  334. )
  335. else:
  336. for minor_version in range(16, 3, -1):
  337. compat_version = 10, minor_version
  338. binary_format = "universal2"
  339. yield "macosx_{major}_{minor}_{binary_format}".format(
  340. major=compat_version[0],
  341. minor=compat_version[1],
  342. binary_format=binary_format,
  343. )
  344. def _linux_platforms(is_32bit: bool = _32_BIT_INTERPRETER) -> Iterator[str]:
  345. linux = _normalize_string(sysconfig.get_platform())
  346. if is_32bit:
  347. if linux == "linux_x86_64":
  348. linux = "linux_i686"
  349. elif linux == "linux_aarch64":
  350. linux = "linux_armv7l"
  351. _, arch = linux.split("_", 1)
  352. yield from _manylinux.platform_tags(linux, arch)
  353. yield from _musllinux.platform_tags(arch)
  354. yield linux
  355. def _generic_platforms() -> Iterator[str]:
  356. yield _normalize_string(sysconfig.get_platform())
  357. def _platform_tags() -> Iterator[str]:
  358. """
  359. Provides the platform tags for this installation.
  360. """
  361. if platform.system() == "Darwin":
  362. return mac_platforms()
  363. elif platform.system() == "Linux":
  364. return _linux_platforms()
  365. else:
  366. return _generic_platforms()
  367. def interpreter_name() -> str:
  368. """
  369. Returns the name of the running interpreter.
  370. """
  371. name = sys.implementation.name
  372. return INTERPRETER_SHORT_NAMES.get(name) or name
  373. def interpreter_version(*, warn: bool = False) -> str:
  374. """
  375. Returns the version of the running interpreter.
  376. """
  377. version = _get_config_var("py_version_nodot", warn=warn)
  378. if version:
  379. version = str(version)
  380. else:
  381. version = _version_nodot(sys.version_info[:2])
  382. return version
  383. def _version_nodot(version: PythonVersion) -> str:
  384. return "".join(map(str, version))
  385. def sys_tags(*, warn: bool = False) -> Iterator[Tag]:
  386. """
  387. Returns the sequence of tag triples for the running interpreter.
  388. The order of the sequence corresponds to priority order for the
  389. interpreter, from most to least important.
  390. """
  391. interp_name = interpreter_name()
  392. if interp_name == "cp":
  393. yield from cpython_tags(warn=warn)
  394. else:
  395. yield from generic_tags()
  396. yield from compatible_tags()