Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/packaging/utils.py: 45%
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
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
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.
5from __future__ import annotations
7import re
8from typing import NewType, Tuple, Union, cast
10from .tags import Tag, parse_tag
11from .version import InvalidVersion, Version, _TrimmedRelease
13BuildTag = Union[Tuple[()], Tuple[int, str]]
14NormalizedName = NewType("NormalizedName", str)
16__all__ = [
17 "BuildTag",
18 "InvalidName",
19 "InvalidSdistFilename",
20 "InvalidWheelFilename",
21 "NormalizedName",
22 "canonicalize_name",
23 "canonicalize_version",
24 "is_normalized_name",
25 "parse_sdist_filename",
26 "parse_wheel_filename",
27]
30def __dir__() -> list[str]:
31 return __all__
34class InvalidName(ValueError):
35 """
36 An invalid distribution name; users should refer to the packaging user guide.
37 """
40class InvalidWheelFilename(ValueError):
41 """
42 An invalid wheel filename was found, users should refer to PEP 427.
43 """
46class InvalidSdistFilename(ValueError):
47 """
48 An invalid sdist filename was found, users should refer to the packaging user guide.
49 """
52# Core metadata spec for `Name`
53_validate_regex = re.compile(r"[A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9]", re.IGNORECASE)
54_normalized_regex = re.compile(r"[a-z0-9]|[a-z0-9]([a-z0-9-](?!--))*[a-z0-9]")
55# PEP 427: The build number must start with a digit.
56_build_tag_regex = re.compile(r"(\d+)(.*)")
59def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
60 if validate and not _validate_regex.fullmatch(name):
61 raise InvalidName(f"name is invalid: {name!r}")
62 # Ensure all ``.`` and ``_`` are ``-``
63 # Emulates ``re.sub(r"[-_.]+", "-", name).lower()`` from PEP 503
64 # Much faster than re, and even faster than str.translate
65 value = name.lower().replace("_", "-").replace(".", "-")
66 # Condense repeats (faster than regex)
67 while "--" in value:
68 value = value.replace("--", "-")
69 return cast("NormalizedName", value)
72def is_normalized_name(name: str) -> bool:
73 return _normalized_regex.fullmatch(name) is not None
76def canonicalize_version(
77 version: Version | str, *, strip_trailing_zero: bool = True
78) -> str:
79 """
80 Return a canonical form of a version as a string.
82 >>> canonicalize_version('1.0.1')
83 '1.0.1'
85 Per PEP 625, versions may have multiple canonical forms, differing
86 only by trailing zeros.
88 >>> canonicalize_version('1.0.0')
89 '1'
90 >>> canonicalize_version('1.0.0', strip_trailing_zero=False)
91 '1.0.0'
93 Invalid versions are returned unaltered.
95 >>> canonicalize_version('foo bar baz')
96 'foo bar baz'
97 """
98 if isinstance(version, str):
99 try:
100 version = Version(version)
101 except InvalidVersion:
102 return str(version)
103 return str(_TrimmedRelease(version) if strip_trailing_zero else version)
106def parse_wheel_filename(
107 filename: str,
108) -> tuple[NormalizedName, Version, BuildTag, frozenset[Tag]]:
109 if not filename.endswith(".whl"):
110 raise InvalidWheelFilename(
111 f"Invalid wheel filename (extension must be '.whl'): {filename!r}"
112 )
114 filename = filename[:-4]
115 dashes = filename.count("-")
116 if dashes not in (4, 5):
117 raise InvalidWheelFilename(
118 f"Invalid wheel filename (wrong number of parts): {filename!r}"
119 )
121 parts = filename.split("-", dashes - 2)
122 name_part = parts[0]
123 # See PEP 427 for the rules on escaping the project name.
124 if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
125 raise InvalidWheelFilename(f"Invalid project name: {filename!r}")
126 name = canonicalize_name(name_part)
128 try:
129 version = Version(parts[1])
130 except InvalidVersion as e:
131 raise InvalidWheelFilename(
132 f"Invalid wheel filename (invalid version): {filename!r}"
133 ) from e
135 if dashes == 5:
136 build_part = parts[2]
137 build_match = _build_tag_regex.match(build_part)
138 if build_match is None:
139 raise InvalidWheelFilename(
140 f"Invalid build number: {build_part} in {filename!r}"
141 )
142 build = cast("BuildTag", (int(build_match.group(1)), build_match.group(2)))
143 else:
144 build = ()
145 tags = parse_tag(parts[-1])
146 return (name, version, build, tags)
149def parse_sdist_filename(filename: str) -> tuple[NormalizedName, Version]:
150 if filename.endswith(".tar.gz"):
151 file_stem = filename[: -len(".tar.gz")]
152 elif filename.endswith(".zip"):
153 file_stem = filename[: -len(".zip")]
154 else:
155 raise InvalidSdistFilename(
156 f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
157 f" {filename!r}"
158 )
160 # We are requiring a PEP 440 version, which cannot contain dashes,
161 # so we split on the last dash.
162 name_part, sep, version_part = file_stem.rpartition("-")
163 if not sep:
164 raise InvalidSdistFilename(f"Invalid sdist filename: {filename!r}")
166 name = canonicalize_name(name_part)
168 try:
169 version = Version(version_part)
170 except InvalidVersion as e:
171 raise InvalidSdistFilename(
172 f"Invalid sdist filename (invalid version): {filename!r}"
173 ) from e
175 return (name, version)