-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_pypi.py
59 lines (46 loc) · 1.79 KB
/
check_pypi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import argparse
import json
import time
from urllib.request import urlopen
PYPI_RELEASES_URL = "https://pypi.org/pypi/dbt-platform-helper/json"
OK = 0
FAIL = 1
def opts():
parser = argparse.ArgumentParser(
description="Tool to check PyPI for the presence of the platform-tools package"
)
parser.add_argument("--retry-interval", help="Delay before retrying", type=int, default=6)
parser.add_argument("--max-attempts", help="Maximum number of attempts", type=int, default=1)
parser.add_argument("--version", help="Display the project version", action="store_true")
return parser.parse_args()
def check_for_version_in_pypi_releases(options, version, get_releases_fn):
print("Version:", version)
if options.version:
return OK
for i in range(options.max_attempts):
print(f"Attempt {i + 1} of {options.max_attempts}: ", end="")
releases = get_releases_fn()
if version in releases:
print(f"Version {version} has been found in PyPI.")
return OK
if i + 1 < options.max_attempts:
print(f"Package not yet found in PyPI. Retrying in {options.retry_interval}s.")
time.sleep(options.retry_interval)
print(f"Version {version} could not be found in PyPI.")
return FAIL
def get_releases():
pypi_releases = urlopen(PYPI_RELEASES_URL)
data = json.loads(pypi_releases.read())
return data["releases"].keys()
def get_current_version(project_file):
with open(project_file, "rb") as fh:
import tomllib
pyproject = tomllib.load(fh)
version = pyproject["tool"]["poetry"]["version"]
return version
if __name__ == "__main__":
exit(
check_for_version_in_pypi_releases(
opts(), get_current_version("pyproject.toml"), get_releases
)
)