Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/packaging/utils.py: 46%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

69 statements  

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 

5from __future__ import annotations 

6 

7import re 

8from typing import NewType, Tuple, Union, cast 

9 

10from .tags import Tag, parse_tag 

11from .version import InvalidVersion, Version, _TrimmedRelease 

12 

13BuildTag = Union[Tuple[()], Tuple[int, str]] 

14NormalizedName = NewType("NormalizedName", str) 

15 

16 

17class InvalidName(ValueError): 

18 """ 

19 An invalid distribution name; users should refer to the packaging user guide. 

20 """ 

21 

22 

23class InvalidWheelFilename(ValueError): 

24 """ 

25 An invalid wheel filename was found, users should refer to PEP 427. 

26 """ 

27 

28 

29class InvalidSdistFilename(ValueError): 

30 """ 

31 An invalid sdist filename was found, users should refer to the packaging user guide. 

32 """ 

33 

34 

35# Core metadata spec for `Name` 

36_validate_regex = re.compile( 

37 r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])\Z", re.IGNORECASE 

38) 

39_canonicalize_regex = re.compile(r"[-_.]+") 

40_normalized_regex = re.compile(r"^([a-z0-9]|[a-z0-9]([a-z0-9-](?!--))*[a-z0-9])$") 

41# PEP 427: The build number must start with a digit. 

42_build_tag_regex = re.compile(r"(\d+)(.*)") 

43 

44 

45def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName: 

46 if validate and not _validate_regex.match(name): 

47 raise InvalidName(f"name is invalid: {name!r}") 

48 # This is taken from PEP 503. 

49 value = _canonicalize_regex.sub("-", name).lower() 

50 return cast("NormalizedName", value) 

51 

52 

53def is_normalized_name(name: str) -> bool: 

54 return _normalized_regex.match(name) is not None 

55 

56 

57def canonicalize_version( 

58 version: Version | str, *, strip_trailing_zero: bool = True 

59) -> str: 

60 """ 

61 Return a canonical form of a version as a string. 

62 

63 >>> canonicalize_version('1.0.1') 

64 '1.0.1' 

65 

66 Per PEP 625, versions may have multiple canonical forms, differing 

67 only by trailing zeros. 

68 

69 >>> canonicalize_version('1.0.0') 

70 '1' 

71 >>> canonicalize_version('1.0.0', strip_trailing_zero=False) 

72 '1.0.0' 

73 

74 Invalid versions are returned unaltered. 

75 

76 >>> canonicalize_version('foo bar baz') 

77 'foo bar baz' 

78 """ 

79 if isinstance(version, str): 

80 try: 

81 version = Version(version) 

82 except InvalidVersion: 

83 return str(version) 

84 return str(_TrimmedRelease(version) if strip_trailing_zero else version) 

85 

86 

87def parse_wheel_filename( 

88 filename: str, 

89) -> tuple[NormalizedName, Version, BuildTag, frozenset[Tag]]: 

90 if not filename.endswith(".whl"): 

91 raise InvalidWheelFilename( 

92 f"Invalid wheel filename (extension must be '.whl'): {filename!r}" 

93 ) 

94 

95 filename = filename[:-4] 

96 dashes = filename.count("-") 

97 if dashes not in (4, 5): 

98 raise InvalidWheelFilename( 

99 f"Invalid wheel filename (wrong number of parts): {filename!r}" 

100 ) 

101 

102 parts = filename.split("-", dashes - 2) 

103 name_part = parts[0] 

104 # See PEP 427 for the rules on escaping the project name. 

105 if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None: 

106 raise InvalidWheelFilename(f"Invalid project name: {filename!r}") 

107 name = canonicalize_name(name_part) 

108 

109 try: 

110 version = Version(parts[1]) 

111 except InvalidVersion as e: 

112 raise InvalidWheelFilename( 

113 f"Invalid wheel filename (invalid version): {filename!r}" 

114 ) from e 

115 

116 if dashes == 5: 

117 build_part = parts[2] 

118 build_match = _build_tag_regex.match(build_part) 

119 if build_match is None: 

120 raise InvalidWheelFilename( 

121 f"Invalid build number: {build_part} in {filename!r}" 

122 ) 

123 build = cast("BuildTag", (int(build_match.group(1)), build_match.group(2))) 

124 else: 

125 build = () 

126 tags = parse_tag(parts[-1]) 

127 return (name, version, build, tags) 

128 

129 

130def parse_sdist_filename(filename: str) -> tuple[NormalizedName, Version]: 

131 if filename.endswith(".tar.gz"): 

132 file_stem = filename[: -len(".tar.gz")] 

133 elif filename.endswith(".zip"): 

134 file_stem = filename[: -len(".zip")] 

135 else: 

136 raise InvalidSdistFilename( 

137 f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):" 

138 f" {filename!r}" 

139 ) 

140 

141 # We are requiring a PEP 440 version, which cannot contain dashes, 

142 # so we split on the last dash. 

143 name_part, sep, version_part = file_stem.rpartition("-") 

144 if not sep: 

145 raise InvalidSdistFilename(f"Invalid sdist filename: {filename!r}") 

146 

147 name = canonicalize_name(name_part) 

148 

149 try: 

150 version = Version(version_part) 

151 except InvalidVersion as e: 

152 raise InvalidSdistFilename( 

153 f"Invalid sdist filename (invalid version): {filename!r}" 

154 ) from e 

155 

156 return (name, version)