-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupdate_requirements.py
executable file
·41 lines (32 loc) · 1.28 KB
/
update_requirements.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
#!/usr/bin/python3
import requests
def get_latest_version(package_name):
"""
Fetches the latest version of the package from PyPI.
"""
url = f"https://pypi.org/pypi/{package_name}/json"
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
return response.json()["info"]["version"]
except requests.RequestException as error:
print(f"Error fetching version for {package_name}: {error}")
return None
def update_requirements(packages_to_update, requirements_path="requirements.txt"):
"""
Updates the requirements.txt with the latest versions of the specified packages.
"""
with open(requirements_path, "r", encoding="UTF-8") as file:
lines = file.readlines()
updated_lines = []
for line in lines:
pkg_name = line.split("==")[0].strip()
if pkg_name in packages_to_update and (version := get_latest_version(pkg_name)):
updated_lines.append(f"{pkg_name}=={version}\n")
continue
updated_lines.append(line)
with open(requirements_path, "w", encoding="UTF-8") as file:
file.writelines(updated_lines)
if __name__ == "__main__":
packages_to_update = ["lmterminal", "shellgenius", "vocabmaster"]
update_requirements(packages_to_update)