[pypi] Refactor script (#240)
Make the script more readable, mostly by: - using the Product and AutoConfig classes, - removing the use of functions when unnecessary, - a little bit of renaming and documentation. This script also now support custom template.
This commit is contained in:
55
src/pypi.py
55
src/pypi.py
@@ -1,4 +1,3 @@
|
||||
import re
|
||||
import sys
|
||||
from common import http
|
||||
from common import dates
|
||||
@@ -6,42 +5,22 @@ from common import endoflife
|
||||
|
||||
METHOD = "pypi"
|
||||
|
||||
|
||||
def fetch_releases(pypi_id, regex):
|
||||
releases = {}
|
||||
|
||||
if not isinstance(regex, list):
|
||||
regex = [regex]
|
||||
|
||||
url = f"https://pypi.org/pypi/{pypi_id}/json"
|
||||
response = http.fetch_url(url)
|
||||
data = response.json()
|
||||
for version in data["releases"]:
|
||||
R = data["releases"][version]
|
||||
matches = False
|
||||
for r in regex:
|
||||
if re.match(r, version):
|
||||
matches = True
|
||||
if matches and R:
|
||||
d = dates.parse_datetime(R[0]["upload_time"], to_utc=False).strftime("%Y-%m-%d")
|
||||
releases[version] = d
|
||||
print(f"{version}: {d}")
|
||||
|
||||
return releases
|
||||
|
||||
|
||||
def update_product(product_name, configs):
|
||||
versions = {}
|
||||
|
||||
for config in configs:
|
||||
config = {"regex": endoflife.DEFAULT_VERSION_REGEX} | config
|
||||
versions = versions | fetch_releases(config[METHOD], config["regex"])
|
||||
|
||||
endoflife.write_releases(product_name, versions)
|
||||
|
||||
|
||||
p_filter = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
for product, configs in endoflife.list_products(METHOD, p_filter).items():
|
||||
print(f"::group::{product}")
|
||||
update_product(product, configs)
|
||||
for product_name, configs in endoflife.list_products(METHOD, p_filter).items():
|
||||
print(f"::group::{product_name}")
|
||||
product = endoflife.Product(product_name, load_product_data=True)
|
||||
|
||||
for config in product.get_auto_configs(METHOD):
|
||||
data = http.fetch_url(f"https://pypi.org/pypi/{config.url}/json").json()
|
||||
|
||||
for version_str in data["releases"]:
|
||||
version_match = config.first_match(version_str)
|
||||
version_data = data["releases"][version_str]
|
||||
|
||||
if version_match and version_data:
|
||||
version = config.render(version_match)
|
||||
date = dates.parse_datetime(version_data[0]["upload_time_iso_8601"])
|
||||
product.declare_version(version, date)
|
||||
|
||||
product.write()
|
||||
print("::endgroup::")
|
||||
|
||||
Reference in New Issue
Block a user