utils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 re
  6. from typing import NewType, Union, cast
  7. from .tags import InvalidTag, Tag, UnsortedTagsError, parse_tag
  8. from .version import InvalidVersion, Version, _TrimmedRelease
  9. __all__ = [
  10. "BuildTag",
  11. "InvalidName",
  12. "InvalidSdistFilename",
  13. "InvalidWheelFilename",
  14. "NormalizedName",
  15. "canonicalize_name",
  16. "canonicalize_version",
  17. "is_normalized_name",
  18. "parse_sdist_filename",
  19. "parse_wheel_filename",
  20. ]
  21. def __dir__() -> list[str]:
  22. return __all__
  23. BuildTag = Union[tuple[()], tuple[int, str]]
  24. """
  25. A wheel build tag: an empty tuple, or a ``(build number, build tag suffix)`` pair.
  26. .. versionadded:: 20.9
  27. """
  28. NormalizedName = NewType("NormalizedName", str)
  29. """
  30. A :class:`typing.NewType` of :class:`str`, representing a normalized name.
  31. .. versionadded:: 20.4
  32. """
  33. class InvalidName(ValueError):
  34. """
  35. An invalid distribution name; users should refer to the packaging user guide.
  36. .. versionadded:: 23.2
  37. """
  38. class InvalidWheelFilename(ValueError):
  39. """
  40. An invalid wheel filename was found, users should refer to PEP 427.
  41. .. versionadded:: 20.9
  42. """
  43. class InvalidSdistFilename(ValueError):
  44. """
  45. An invalid sdist filename was found, users should refer to the packaging user guide.
  46. .. versionadded:: 20.9
  47. """
  48. # Core metadata spec for `Name`
  49. _validate_regex = re.compile(
  50. r"[a-z0-9]|[a-z0-9][a-z0-9._-]*[a-z0-9]", re.IGNORECASE | re.ASCII
  51. )
  52. _normalized_regex = re.compile(r"[a-z0-9]+(?:-[a-z0-9]+)*", re.ASCII)
  53. # PEP 427: The build number must start with a digit.
  54. _build_tag_regex = re.compile(r"(\d+)(.*)", re.ASCII)
  55. # PEP 427: Valid characters for an escaped project name in a wheel filename.
  56. # Requires at least one character so an empty project name is rejected.
  57. _wheel_name_regex = re.compile(r"^[\w._]+\Z", re.UNICODE)
  58. def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
  59. """
  60. This function takes a valid Python package or extra name, and returns the
  61. normalized form of it.
  62. The return type is typed as :class:`NormalizedName`. This allows type
  63. checkers to help require that a string has passed through this function
  64. before use.
  65. If **validate** is true, then the function will check if **name** is a valid
  66. distribution name before normalizing.
  67. :param str name: The name to normalize.
  68. :param bool validate: Check whether the name is a valid distribution name.
  69. :raises InvalidName: If **validate** is true and the name is not an
  70. acceptable distribution name.
  71. >>> from packaging.utils import canonicalize_name
  72. >>> canonicalize_name("Django")
  73. 'django'
  74. >>> canonicalize_name("oslo.concurrency")
  75. 'oslo-concurrency'
  76. >>> canonicalize_name("requests")
  77. 'requests'
  78. .. versionadded:: 16.2
  79. .. versionchanged:: 20.4
  80. The return type was changed to :class:`NormalizedName`.
  81. .. versionchanged:: 23.2
  82. Added the *validate* keyword parameter.
  83. """
  84. if validate and not _validate_regex.fullmatch(name):
  85. raise InvalidName(f"name is invalid: {name!r}")
  86. # Ensure all ``.`` and ``_`` are ``-``
  87. # Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503
  88. # Much faster than re, and even faster than str.translate
  89. value = name.lower().replace("_", "-").replace(".", "-")
  90. # Condense repeats (faster than regex)
  91. while "--" in value:
  92. value = value.replace("--", "-")
  93. return cast("NormalizedName", value)
  94. def is_normalized_name(name: str) -> bool:
  95. """
  96. Check if a name is a normalized project name (i.e. a valid name that
  97. :func:`canonicalize_name` would roundtrip to the same value).
  98. The roundtrip only characterizes normalized names for *valid* names. A name
  99. must start and end with an ASCII letter or digit, which
  100. :func:`canonicalize_name` does not enforce: it leaves a leading or trailing
  101. hyphen in place, so such a name roundtrips without being normalized.
  102. :param str name: The name to check.
  103. >>> from packaging.utils import canonicalize_name, is_normalized_name
  104. >>> is_normalized_name("requests")
  105. True
  106. >>> is_normalized_name("Django")
  107. False
  108. >>> canonicalize_name("_not_legal")
  109. '-not-legal'
  110. >>> is_normalized_name("-not-legal") # roundtrips, but not a valid name
  111. False
  112. .. versionadded:: 23.2
  113. """
  114. return _normalized_regex.fullmatch(name) is not None
  115. def canonicalize_version(
  116. version: Version | str, *, strip_trailing_zero: bool = True
  117. ) -> str:
  118. """Return a canonical form of a version as a string.
  119. This function takes a string representing a package version (or a
  120. :class:`~packaging.version.Version` instance), and returns the
  121. normalized form of it. By default, it strips trailing zeros from
  122. the release segment.
  123. >>> from packaging.utils import canonicalize_version
  124. >>> canonicalize_version('1.0.1')
  125. '1.0.1'
  126. Per PEP 625, versions may have multiple canonical forms, differing
  127. only by trailing zeros.
  128. >>> canonicalize_version('1.0.0')
  129. '1'
  130. >>> canonicalize_version('1.0.0', strip_trailing_zero=False)
  131. '1.0.0'
  132. Invalid versions are returned unaltered.
  133. >>> canonicalize_version('foo bar baz')
  134. 'foo bar baz'
  135. >>> canonicalize_version('1.4.0.0.0')
  136. '1.4'
  137. .. versionadded:: 17.1
  138. .. versionchanged:: 21.0
  139. The return type was narrowed to :class:`str`.
  140. .. versionchanged:: 22.0
  141. Added the *strip_trailing_zero* keyword parameter.
  142. """
  143. if isinstance(version, str):
  144. try:
  145. version = Version(version)
  146. except InvalidVersion:
  147. return str(version)
  148. return str(_TrimmedRelease(version) if strip_trailing_zero else version)
  149. def parse_wheel_filename(
  150. filename: str,
  151. *,
  152. validate_order: bool = False,
  153. ) -> tuple[NormalizedName, Version, BuildTag, frozenset[Tag]]:
  154. """
  155. This function takes the filename of a wheel file, and parses it,
  156. returning a tuple of name, version, build number, and tags.
  157. The name part of the tuple is normalized and typed as
  158. :class:`NormalizedName`. The version portion is an instance of
  159. :class:`~packaging.version.Version`. The build number is ``()`` if
  160. there is no build number in the wheel filename, otherwise a
  161. two-item tuple of an integer for the leading digits and
  162. a string for the rest of the build number. The tags portion is a
  163. frozen set of :class:`~packaging.tags.Tag` instances (as the tag
  164. string format allows multiple tags to be combined into a single
  165. string).
  166. If **validate_order** is true, compressed tag set components are
  167. checked to be in sorted order as required by PEP 425.
  168. :param str filename: The name of the wheel file.
  169. :param bool validate_order: Check whether compressed tag set components
  170. are in sorted order.
  171. :raises InvalidWheelFilename: If the filename in question
  172. does not follow the :ref:`wheel specification
  173. <pypug:binary-distribution-format>`.
  174. >>> from packaging.utils import parse_wheel_filename
  175. >>> from packaging.tags import Tag
  176. >>> from packaging.version import Version
  177. >>> name, ver, build, tags = parse_wheel_filename("foo-1.0-py3-none-any.whl")
  178. >>> name
  179. 'foo'
  180. >>> ver == Version('1.0')
  181. True
  182. >>> tags == {Tag("py3", "none", "any")}
  183. True
  184. >>> not build
  185. True
  186. .. versionadded:: 20.9
  187. .. versionchanged:: 23.2
  188. Raises :class:`InvalidWheelFilename` when the version component is invalid.
  189. .. versionadded:: 26.1
  190. The *validate_order* parameter.
  191. .. versionchanged:: 26.3
  192. Raises :class:`InvalidWheelFilename` when an interpreter component is
  193. not an identifier, a tag set component is empty, or the project name is
  194. empty.
  195. """
  196. if not filename.endswith(".whl"):
  197. raise InvalidWheelFilename(
  198. f"Invalid wheel filename (extension must be '.whl'): {filename!r}"
  199. )
  200. filename = filename[:-4]
  201. dashes = filename.count("-")
  202. if dashes not in (4, 5):
  203. raise InvalidWheelFilename(
  204. f"Invalid wheel filename (wrong number of parts): {filename!r}"
  205. )
  206. parts = filename.split("-", dashes - 2)
  207. name_part = parts[0]
  208. # See PEP 427 for the rules on escaping the project name.
  209. if "__" in name_part or _wheel_name_regex.match(name_part) is None:
  210. raise InvalidWheelFilename(f"Invalid project name: {filename!r}")
  211. name = canonicalize_name(name_part)
  212. try:
  213. version = Version(parts[1])
  214. except InvalidVersion as e:
  215. raise InvalidWheelFilename(
  216. f"Invalid wheel filename (invalid version): {filename!r}"
  217. ) from e
  218. if dashes == 5:
  219. build_part = parts[2]
  220. build_match = _build_tag_regex.match(build_part)
  221. if build_match is None:
  222. raise InvalidWheelFilename(
  223. f"Invalid build number: {build_part} in {filename!r}"
  224. )
  225. build = cast("BuildTag", (int(build_match.group(1)), build_match.group(2)))
  226. else:
  227. build = ()
  228. tag_str = parts[-1]
  229. try:
  230. tags = parse_tag(tag_str, validate_order=validate_order)
  231. except UnsortedTagsError:
  232. raise InvalidWheelFilename(
  233. f"Invalid wheel filename (compressed tag set components must be in "
  234. f"sorted order per PEP 425): {filename!r}"
  235. ) from None
  236. except InvalidTag:
  237. raise InvalidWheelFilename(
  238. f"Invalid wheel filename (invalid tag component): {filename!r}"
  239. ) from None
  240. return (name, version, build, tags)
  241. def parse_sdist_filename(filename: str) -> tuple[NormalizedName, Version]:
  242. """
  243. This function takes the filename of a sdist file (as specified
  244. in the `Source distribution format`_ documentation), and parses
  245. it, returning a tuple of the normalized name and version as
  246. represented by an instance of :class:`~packaging.version.Version`.
  247. :param str filename: The name of the sdist file.
  248. :raises InvalidSdistFilename: If the filename does not end
  249. with an sdist extension (``.zip`` or ``.tar.gz``), if it does not
  250. contain a dash separating the name and the version of the distribution,
  251. if the project name is empty, or if the version portion is not a valid
  252. version.
  253. >>> from packaging.utils import parse_sdist_filename
  254. >>> from packaging.version import Version
  255. >>> name, ver = parse_sdist_filename("foo-1.0.tar.gz")
  256. >>> name
  257. 'foo'
  258. >>> ver == Version('1.0')
  259. True
  260. .. versionadded:: 20.9
  261. .. versionchanged:: 21.0
  262. Added support for ``.zip`` source distributions.
  263. .. versionchanged:: 23.2
  264. Raises :class:`InvalidSdistFilename` when the version component is invalid.
  265. .. versionchanged:: 26.3
  266. Raises :class:`InvalidSdistFilename` on an empty project name.
  267. .. _Source distribution format: https://packaging.python.org/specifications/source-distribution-format/#source-distribution-file-name
  268. """
  269. if filename.endswith(".tar.gz"):
  270. file_stem = filename[: -len(".tar.gz")]
  271. elif filename.endswith(".zip"):
  272. file_stem = filename[: -len(".zip")]
  273. else:
  274. raise InvalidSdistFilename(
  275. f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
  276. f" {filename!r}"
  277. )
  278. # We are requiring a PEP 440 version, which cannot contain dashes,
  279. # so we split on the last dash.
  280. name_part, sep, version_part = file_stem.rpartition("-")
  281. if not sep:
  282. raise InvalidSdistFilename(f"Invalid sdist filename: {filename!r}")
  283. if not name_part:
  284. raise InvalidSdistFilename(
  285. f"Invalid sdist filename (empty project name): {filename!r}"
  286. )
  287. name = canonicalize_name(name_part)
  288. try:
  289. version = Version(version_part)
  290. except InvalidVersion as e:
  291. raise InvalidSdistFilename(
  292. f"Invalid sdist filename (invalid version): {filename!r}"
  293. ) from e
  294. return (name, version)