Skip to content

Commit 9d4d56d

Browse files
committed
Accept plain versions or versions ending in ".*"
Versions are now free to have any number of dots Extract fetch_pypi_versions() from read_base_version() See python/typeshed#6095
1 parent 7abf3a5 commit 9d4d56d

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

scripts/get_version.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import argparse
1414
import os.path
15+
import re
1516
from typing import Optional
1617

1718
import requests
@@ -28,14 +29,35 @@
2829
TIMEOUT = 3
2930

3031

32+
def fetch_pypi_versions(distribution: str) -> list[str]:
33+
url = URL_TEMPLATE.format(PREFIX + distribution)
34+
retry_strategy = Retry(
35+
total=RETRIES, status_forcelist=RETRY_ON
36+
)
37+
with requests.Session() as session:
38+
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
39+
resp = session.get(url, timeout=TIMEOUT)
40+
if not resp.ok:
41+
if resp.status_code == 404:
42+
# Looks like this is first time this package is ever uploaded.
43+
return []
44+
raise ValueError("Error while retrieving version")
45+
data = resp.json()
46+
return data["releases"].keys()
47+
48+
3149
def read_base_version(typeshed_dir: str, distribution: str) -> str:
3250
"""Read distribution version from metadata."""
3351
metadata_file = os.path.join(
3452
typeshed_dir, THIRD_PARTY_NAMESPACE, distribution, "METADATA.toml"
3553
)
3654
with open(metadata_file) as f:
3755
data = toml.loads(f.read())
38-
return data["version"]
56+
version = data["version"]
57+
if version.endswith(".*"):
58+
version = version[:-2]
59+
assert re.match(r"\d+(\.\d+)*", version)
60+
return version
3961

4062

4163
def strip_dep_version(dependency: str) -> str:
@@ -67,27 +89,13 @@ def main(typeshed_dir: str, distribution: str, version: Optional[str]) -> int:
6789
6890
Supports basic reties and timeouts (as module constants).
6991
"""
70-
url = URL_TEMPLATE.format(PREFIX + distribution)
71-
retry_strategy = Retry(
72-
total=RETRIES, status_forcelist=RETRY_ON
73-
)
74-
with requests.Session() as session:
75-
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
76-
resp = session.get(url, timeout=TIMEOUT)
77-
if not resp.ok:
78-
if resp.status_code == 404:
79-
# Looks like this is first time this package is ever uploaded.
80-
return -1
81-
raise ValueError("Error while retrieving version")
82-
data = resp.json()
92+
pypi_versions = fetch_pypi_versions()
8393
if not version:
8494
# Use the METADATA.toml version, if not given one.
8595
version = read_base_version(typeshed_dir, distribution)
86-
assert version.count(".") == 1
87-
matching = [v for v in data["releases"].keys() if v.startswith(version)]
96+
matching = [v for v in pypi_versions if v.startswith(f"{version}.")]
8897
if not matching:
8998
return -1
90-
assert all(v.count(".") == 2 for v in matching)
9199
increment = max(int(v.split(".")[-1]) for v in matching)
92100
return increment
93101

0 commit comments

Comments
 (0)