-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrack_releases.py
More file actions
83 lines (80 loc) · 3.63 KB
/
track_releases.py
File metadata and controls
83 lines (80 loc) · 3.63 KB
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
import json
import os
import urllib
import yaml
import pathlib
import feedparser
from packaging.version import Version
GH_TOKEN = os.environ.get("GH_TOKEN", None)
# Go through the yaml files
simtools_dir = pathlib.Path("simtools")
yaml_files = simtools_dir.glob("**/*.yaml")
for yaml_file in yaml_files:
with open(yaml_file, "r") as file:
data = yaml.safe_load(file)
name = data["name"]
release_info = data.get("release", {})
if (
release_info.get("source", None) == "pypi"
): # Only source supported at the moment
current_version = Version(release_info.get("version", "0"))
package_name = release_info.get("package_name", name)
etag = release_info.get("etag", None)
print(f"Checking updates for '{name}', latest known version: {current_version}")
pypi_feed = feedparser.parse(
f"https://pypi.org/rss/project/{package_name}/releases.xml", etag=etag
)
if pypi_feed.status == 304:
print(" PyPI feed has not changed")
continue
# Go through entries until finding a version that is not a dev release or pre-release
for entry in pypi_feed.entries:
entry_version = Version(entry.title)
if entry_version.is_prerelease or entry_version.is_devrelease:
continue
if entry_version > current_version:
print(f" New version found: {entry_version}")
release_info["version"] = str(entry_version)
release_info["published"] = (
f"{entry.published_parsed.tm_year}-{entry.published_parsed.tm_mon:02d}-{entry.published_parsed.tm_mday:02d}"
)
release_info["etag"] = pypi_feed.etag
with open(yaml_file, "w") as file:
yaml.safe_dump(data, file, sort_keys=False)
break
else:
print(f" No new version found for '{name}'")
elif release_info.get("source", None) == "github":
current_version = Version(release_info.get("version", "0"))
repo_name = release_info.get("repository", name)
etag = release_info.get("etag", None)
print(f"Checking updates for '{name}', latest known version: {current_version}")
headers = {"Accept": "application/vnd.github+json"}
if GH_TOKEN:
headers["authorization"] = f"Bearer {GH_TOKEN}"
request = urllib.request.Request(
f"https://api.github.com/repos/{repo_name}/releases/latest",
headers=headers,
)
response = urllib.request.urlopen(request)
if response.status == 304:
print(" GitHub releases feed has not changed")
continue
releases = json.loads(response.read().decode("utf-8"))
if "tag_name" in releases:
entry_version = Version(releases["tag_name"])
if entry_version.is_prerelease or entry_version.is_devrelease:
print(f" Skipping pre-release version: {entry_version}")
continue
if entry_version > current_version:
print(f" New version found: {entry_version}")
release_info["version"] = str(entry_version)
release_date = releases.get("created_at", "")[:10] # only date
release_info["published"] = release_date
release_info["etag"] = response.headers.get("ETag")
with open(yaml_file, "w") as file:
yaml.safe_dump(data, file, sort_keys=False)
else:
print(f" No new version found for '{name}'")
else:
print(f"Skipping '{name}' as it is not on PyPI or GitHub")