[npm] Refactor script (#235)

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 auto method also now support custom regexes and template.
This commit is contained in:
Marc Wrobel
2023-12-11 21:24:18 +01:00
committed by GitHub
parent 959b1865bf
commit 27721b68d7

View File

@@ -1,46 +1,22 @@
import re
import sys
from common import dates
from common import http
from common import endoflife
METHOD = "npm"
def fetch_releases(npm_id, regex):
releases = {}
if not isinstance(regex, list):
regex = [regex]
url = f"https://registry.npmjs.org/{npm_id}"
response = http.fetch_url(url)
data = response.json()
for version in data["time"]:
matches = False
for r in regex:
if re.match(r, version):
matches = True
release_datetime = data["time"][version]
if matches and release_datetime:
releases[version] = release_datetime.split("T")[0]
print(f"{version}: {releases[version]}")
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://registry.npmjs.org/{config.url}").json()
for version_str in data["time"]:
version_match = config.first_match(version_str)
if version_match:
version = config.render(version_match)
date = dates.parse_datetime(data["time"][version_str])
product.declare_version(version, date)
product.write()
print("::endgroup::")