utils.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 re
  5. from typing import FrozenSet, NewType, Tuple, Union, cast
  6. from .tags import Tag, parse_tag
  7. from .version import InvalidVersion, Version
  8. BuildTag = Union[Tuple[()], Tuple[int, str]]
  9. NormalizedName = NewType("NormalizedName", str)
  10. class InvalidName(ValueError):
  11. """
  12. An invalid distribution name; users should refer to the packaging user guide.
  13. """
  14. class InvalidWheelFilename(ValueError):
  15. """
  16. An invalid wheel filename was found, users should refer to PEP 427.
  17. """
  18. class InvalidSdistFilename(ValueError):
  19. """
  20. An invalid sdist filename was found, users should refer to the packaging user guide.
  21. """
  22. # Core metadata spec for `Name`
  23. _validate_regex = re.compile(
  24. r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE
  25. )
  26. _canonicalize_regex = re.compile(r"[-_.]+")
  27. _normalized_regex = re.compile(r"^([a-z0-9]|[a-z0-9]([a-z0-9-](?!--))*[a-z0-9])$")
  28. # PEP 427: The build number must start with a digit.
  29. _build_tag_regex = re.compile(r"(\d+)(.*)")
  30. def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
  31. if validate and not _validate_regex.match(name):
  32. raise InvalidName(f"name is invalid: {name!r}")
  33. # This is taken from PEP 503.
  34. value = _canonicalize_regex.sub("-", name).lower()
  35. return cast(NormalizedName, value)
  36. def is_normalized_name(name: str) -> bool:
  37. return _normalized_regex.match(name) is not None
  38. def canonicalize_version(
  39. version: Union[Version, str], *, strip_trailing_zero: bool = True
  40. ) -> str:
  41. """
  42. This is very similar to Version.__str__, but has one subtle difference
  43. with the way it handles the release segment.
  44. """
  45. if isinstance(version, str):
  46. try:
  47. parsed = Version(version)
  48. except InvalidVersion:
  49. # Legacy versions cannot be normalized
  50. return version
  51. else:
  52. parsed = version
  53. parts = []
  54. # Epoch
  55. if parsed.epoch != 0:
  56. parts.append(f"{parsed.epoch}!")
  57. # Release segment
  58. release_segment = ".".join(str(x) for x in parsed.release)
  59. if strip_trailing_zero:
  60. # NB: This strips trailing '.0's to normalize
  61. release_segment = re.sub(r"(\.0)+$", "", release_segment)
  62. parts.append(release_segment)
  63. # Pre-release
  64. if parsed.pre is not None:
  65. parts.append("".join(str(x) for x in parsed.pre))
  66. # Post-release
  67. if parsed.post is not None:
  68. parts.append(f".post{parsed.post}")
  69. # Development release
  70. if parsed.dev is not None:
  71. parts.append(f".dev{parsed.dev}")
  72. # Local version segment
  73. if parsed.local is not None:
  74. parts.append(f"+{parsed.local}")
  75. return "".join(parts)
  76. def parse_wheel_filename(
  77. filename: str,
  78. ) -> Tuple[NormalizedName, Version, BuildTag, FrozenSet[Tag]]:
  79. if not filename.endswith(".whl"):
  80. raise InvalidWheelFilename(
  81. f"Invalid wheel filename (extension must be '.whl'): {filename}"
  82. )
  83. filename = filename[:-4]
  84. dashes = filename.count("-")
  85. if dashes not in (4, 5):
  86. raise InvalidWheelFilename(
  87. f"Invalid wheel filename (wrong number of parts): {filename}"
  88. )
  89. parts = filename.split("-", dashes - 2)
  90. name_part = parts[0]
  91. # See PEP 427 for the rules on escaping the project name.
  92. if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
  93. raise InvalidWheelFilename(f"Invalid project name: {filename}")
  94. name = canonicalize_name(name_part)
  95. try:
  96. version = Version(parts[1])
  97. except InvalidVersion as e:
  98. raise InvalidWheelFilename(
  99. f"Invalid wheel filename (invalid version): {filename}"
  100. ) from e
  101. if dashes == 5:
  102. build_part = parts[2]
  103. build_match = _build_tag_regex.match(build_part)
  104. if build_match is None:
  105. raise InvalidWheelFilename(
  106. f"Invalid build number: {build_part} in '{filename}'"
  107. )
  108. build = cast(BuildTag, (int(build_match.group(1)), build_match.group(2)))
  109. else:
  110. build = ()
  111. tags = parse_tag(parts[-1])
  112. return (name, version, build, tags)
  113. def parse_sdist_filename(filename: str) -> Tuple[NormalizedName, Version]:
  114. if filename.endswith(".tar.gz"):
  115. file_stem = filename[: -len(".tar.gz")]
  116. elif filename.endswith(".zip"):
  117. file_stem = filename[: -len(".zip")]
  118. else:
  119. raise InvalidSdistFilename(
  120. f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
  121. f" {filename}"
  122. )
  123. # We are requiring a PEP 440 version, which cannot contain dashes,
  124. # so we split on the last dash.
  125. name_part, sep, version_part = file_stem.rpartition("-")
  126. if not sep:
  127. raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")
  128. name = canonicalize_name(name_part)
  129. try:
  130. version = Version(version_part)
  131. except InvalidVersion as e:
  132. raise InvalidSdistFilename(
  133. f"Invalid sdist filename (invalid version): {filename}"
  134. ) from e
  135. return (name, version)