-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_releases.py
146 lines (118 loc) · 4.2 KB
/
check_releases.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import argparse
import hashlib
import requests
import tempfile
import re
import shutil
import sys
import os
# Get all versions of package from pypi
# python script/get_releases.py flux-framework
token = os.environ.get("GITHUB_TOKEN")
headers = {}
if token:
headers["Authorization"] = "token %s" % token
def set_env_and_output(name, value):
"""
helper function to echo a key/value pair to output and env.
Parameters:
name (str) : the name of the environment variable
value (str) : the value to write to file
"""
for env_var in ("GITHUB_ENV", "GITHUB_OUTPUT"):
environment_file_path = os.environ.get(env_var)
print("Writing %s=%s to %s" % (name, value, env_var))
with open(environment_file_path, "a") as environment_file:
environment_file.write("%s=%s\n" % (name, value))
class PackageUpdater:
def __init__(
self, package, version=None, dry_run=False, repo="flux-framework/flux-core"
):
self.package = package
self.dry_run = dry_run
self.version = version
self.repo = repo
def check(self):
"""
Given a package directory and repository name, check for new releases.
"""
releases = self.get_github_releases()
# The latest tag
tag = releases[0]["tag_name"]
if self.version is not None:
tag = self.version
# Get releases from pypi
pypi_releases = self.get_pypi_releases()
# Some versions are prefixed with v
tags = [tag, tag.lstrip("v")]
# If it's a dry run OR we don't have it
if self.dry_run or (
tags[0] not in pypi_releases and tags[1] not in pypi_releases
):
print(f"New version {tag} detected or dry run is set!")
set_env_and_output("version", tag)
set_env_and_output("build", "true")
set_env_and_output("dry-run", self.dry_run)
else:
print(f"Tag {tag} is already released and dry run is false")
def get_pypi_releases(self):
"""
Get releases to pypi
"""
res = requests.get(f"https://pypi.org/pypi/{self.package}/json")
if res.status_code == 404:
return []
if res.status_code != 200:
sys.exit(f"Issue getting pypi releases: {res.txt}")
res = res.json()
# earliest (index 0) to latest (index -1)
return list(res["releases"])
def get_github_releases(self):
"""
Get the lateset release of a repository (under flux-framework)
"""
url = f"https://api.github.com/repos/{self.repo}/releases"
response = requests.get(url, headers=headers, params={"per_page": 100})
response.raise_for_status()
# latest release should be first
return response.json()
def download_archive(self, naked_version, download_path):
"""
Download an archive tarball.
"""
tarball_url = (
f"https://github.com/{self.repo}/archive/refs/tags/{naked_version}.tar.gz"
)
return self.download(tarball_url, download_path)
def get_parser():
parser = argparse.ArgumentParser(
description="Spack Updater for Releases",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("package", help="name of pypi package")
parser.add_argument("--version", help="version of flux to target")
parser.add_argument(
"--repo", default="flux-framework/flux-core", help="repository with releases"
)
parser.add_argument(
"--dry-run",
action="store_true",
default=False,
help="Don't write changes to file",
)
return parser
def main():
parser = get_parser()
# If an error occurs while parsing the arguments, the interpreter will exit with value 2
args, extra = parser.parse_known_args()
# Show args to the user
print(" repo: %s" % args.repo)
print(" package: %s" % args.package)
print(" dry-run: %s" % args.dry_run)
if args.version:
print(" version: %s" % args.version)
updater = PackageUpdater(args.package, args.version, args.dry_run)
updater.check()
if __name__ == "__main__":
main()