utils.py 4.1 KB

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