Files
endoflife-date-release-data/src/pypi.py
Rajat Jaiswal 05b0977241 NPM automation (#36)
Initial support for NPM Automation. This isn't widely used, since most NPM projects
will use git tags as well, but helpful for a few projects.

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>

* Fix fstrings and remove unused imports

* Fix the merge of product config with default config in npm.py and pypi.py

If product config is first, default config overrides product config.

* Simplify npm.py

Co-authored-by: Nemo <me@captnemo.in>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Marc Wrobel <marc.wrobel@gmail.com>
2022-12-16 17:08:24 +05:30

69 lines
2.1 KiB
Python

from glob import glob
import os
import re
import sys
import json
import frontmatter
import urllib.request
from datetime import datetime
# Same as used in Ruby (update.rb)
DEFAULT_TAG_TEMPLATE = (
"{{major}}{% if minor %}.{{minor}}{% if patch %}.{{patch}}{%endif%}{%endif%}"
)
REGEX = r"^(?:(\d+\.(?:\d+\.)*\d+))$"
def fetch_releases(pypi_id, regex):
releases = {}
if not isinstance(regex, list):
regex = [regex]
url = "https://pypi.org/pypi/%s/json" % pypi_id
with urllib.request.urlopen(url, data=None, timeout=5) as response:
data = json.loads(response.read().decode("utf-8"))
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 = datetime.fromisoformat(R[0]["upload_time"]).strftime("%Y-%m-%d")
releases[version] = d
print("%s: %s" % (version, d))
return releases
def update_releases(product_filter=None):
for product_file in glob("website/products/*.md"):
product_name = os.path.splitext(os.path.basename(product_file))[0]
if product_filter and product_name != product_filter:
continue
with open(product_file, "r") as f:
data = frontmatter.load(f)
if "auto" in data:
for config in data["auto"]:
for key, d_id in config.items():
if key == "pypi":
update_product(product_name, config)
def update_product(product_name, config):
if "pypi" in config:
print("::group::%s" % product_name)
config = {"regex": REGEX} | config
r = fetch_releases(config["pypi"], config["regex"])
with open("releases/%s.json" % product_name, "w") as f:
f.write(json.dumps(r, indent=2))
print("::endgroup::")
if __name__ == "__main__":
if len(sys.argv) > 1:
update_releases(sys.argv[1])
else:
update_releases()