-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_bun.py
137 lines (109 loc) · 4.14 KB
/
check_bun.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Script to fetch the latest versions of the Bun package based on specified
version types.
Usage:
python check_bun.py [version_type]
Arguments:
version_type: Comma-separated list of version types to fetch. Valid
options: "latest", "canary".
"""
import argparse
import json
from typing import List, Dict, Optional, Tuple
import requests
from bs4 import BeautifulSoup
from packaging import version
def get_bun_latest_versions(version_types: List[str]) -> Dict[str, Optional[str]]:
"""
Fetches the latest Bun versions from the Bun NPM page based on specified version types.
Args:
version_types (List[str]): List of version types to fetch.
Returns:
Dict[str, Optional[str]]: Dictionary with version types as keys and latest version strings as values.
"""
url = "https://www.npmjs.com/package/bun?activeTab=versions"
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
except requests.RequestException as e:
raise RuntimeError("Failed to fetch Bun versions") from e
soup = BeautifulSoup(response.content, "html.parser")
versions = soup.find_all("a", class_="_132722c7 f5 black-60 lh-copy code")
version_list = [ver.get_text().strip() for ver in versions]
latest_versions = {"latest": None, "canary": None}
for ver_str in version_list:
parsed_version, is_canary = parse_version(ver_str)
update_latest_versions(
latest_versions, ver_str, parsed_version, is_canary, version_types
)
return {k: v for k, v in latest_versions.items() if k in version_types}
def parse_version(version_str: str) -> Tuple[version.Version, bool]:
"""
Parses a version string and identifies if it's a canary version.
Args:
version_str (str): The version string to parse.
Returns:
version.Version: The parsed version.
bool: True if it's a canary version, False otherwise.
"""
version_parts = version_str.split("-", 1)
parsed_version = version.parse(version_parts[0])
is_canary = "canary" in version_str
return parsed_version, is_canary
def update_latest_versions(
latest_versions: Dict[str, Optional[str]],
version_str: str,
parsed_version: version.Version,
is_canary: bool,
version_types: List[str],
):
"""
Updates the dictionary of latest versions if a newer version is found.
Args:
latest_versions (Dict[str, Optional[str]]): Dictionary to store latest versions.
version_str (str): The version string.
parsed_version (version.Version): Parsed version object.
is_canary (bool): Flag indicating if the version is a canary version.
version_types (List[str]): List of version types to consider.
"""
type_key = "canary" if is_canary else "latest"
if type_key in version_types:
current_version = latest_versions[type_key]
if current_version is None or parsed_version > version.parse(
current_version.split("-", 1)[0]
):
latest_versions[type_key] = version_str
def main():
"""
Fetches the latest Bun versions and compares them with the current versions.
If there are any updated versions, it prints them.
Args:
version_type (str): Comma-separated list of version types to fetch: latest, canary
Returns:
None
"""
parser = argparse.ArgumentParser(description="Fetch the latest Bun versions.")
parser.add_argument(
"version_type",
type=str,
default="latest,canary",
help="Comma-separated list of version types to fetch: latest, canary",
)
args = parser.parse_args()
version_types = args.version_type.split(",")
try:
latest_versions = get_bun_latest_versions(version_types)
except RuntimeError as e:
print(str(e))
return
with open("versions.json", encoding="utf-8") as f:
current_versions = json.load(f)["bun"]
updated_versions = [
latest_versions[vt]
for vt in version_types
if latest_versions[vt] != current_versions.get(vt)
]
if updated_versions:
print(",".join(updated_versions))
if __name__ == "__main__":
main()