|
10 | 10 | distribution information.
|
11 | 11 | """
|
12 | 12 |
|
| 13 | +from __future__ import annotations |
| 14 | + |
13 | 15 | import argparse
|
14 | 16 | import os.path
|
15 |
| -from typing import Optional, cast |
| 17 | +from collections.abc import Iterable |
| 18 | +from typing import Any, Optional, cast |
16 | 19 |
|
17 | 20 | import requests
|
18 | 21 | import toml
|
|
28 | 31 | TIMEOUT = 3
|
29 | 32 |
|
30 | 33 |
|
| 34 | +def fetch_pypi_versions(distribution: str) -> Iterable[str]: |
| 35 | + url = URL_TEMPLATE.format(PREFIX + distribution) |
| 36 | + retry_strategy = Retry(total=RETRIES, status_forcelist=RETRY_ON) |
| 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 | + releases: dict[str, Any] = resp.json()["releases"] |
| 46 | + return releases.keys() |
| 47 | + |
| 48 | + |
31 | 49 | def read_base_version(typeshed_dir: str, distribution: str) -> str:
|
32 | 50 | """Read distribution version from metadata."""
|
33 | 51 | metadata_file = os.path.join(
|
@@ -67,25 +85,13 @@ def main(typeshed_dir: str, distribution: str, version: Optional[str]) -> int:
|
67 | 85 |
|
68 | 86 | Supports basic reties and timeouts (as module constants).
|
69 | 87 | """
|
70 |
| - url = URL_TEMPLATE.format(PREFIX + distribution) |
71 |
| - retry_strategy = Retry(total=RETRIES, status_forcelist=RETRY_ON) |
72 |
| - with requests.Session() as session: |
73 |
| - session.mount("https://", HTTPAdapter(max_retries=retry_strategy)) |
74 |
| - resp = session.get(url, timeout=TIMEOUT) |
75 |
| - if not resp.ok: |
76 |
| - if resp.status_code == 404: |
77 |
| - # Looks like this is first time this package is ever uploaded. |
78 |
| - return -1 |
79 |
| - raise ValueError("Error while retrieving version") |
80 |
| - data = resp.json() |
| 88 | + pypi_versions = fetch_pypi_versions(distribution) |
81 | 89 | if not version:
|
82 | 90 | # Use the METADATA.toml version, if not given one.
|
83 | 91 | version = read_base_version(typeshed_dir, distribution)
|
84 |
| - assert version.count(".") == 1 |
85 |
| - matching = [v for v in data["releases"].keys() if v.startswith(version)] |
| 92 | + matching = [v for v in pypi_versions if v.startswith(f"{version}.")] |
86 | 93 | if not matching:
|
87 | 94 | return -1
|
88 |
| - assert all(v.count(".") == 2 for v in matching) |
89 | 95 | increment = max(int(v.split(".")[-1]) for v in matching)
|
90 | 96 | return increment
|
91 | 97 |
|
|
0 commit comments