utils.py 9.6 KB

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