-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
executable file
·183 lines (134 loc) · 6.03 KB
/
update.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# This script is used in two places:
# - On a server running `update.py check` periodically
# - In the GitHub Actions update workflow (`update.py update`)
#
# Check EXPLANATION.md for more information on how the update process works.
#
# In order to send `repository_dispatch` events, $GITHUB_TOKEN needs to be set.
# The token needs to have the `repo` scope.
import json
from argparse import ArgumentParser
from os import environ, path
from re import search
from subprocess import run
from typing import Dict, Union
from urllib.request import Request, urlopen
################################################
GITHUB_REPOSITORY = "InternetUnexplorer/discord-overlay"
################################################
def get_redirect_location(url: str, user_agent: str = "curl/7.76.1") -> str:
"""Follow redirects on `url` and return the final URL."""
return urlopen(Request(url, headers={"User-Agent": user_agent})).geturl()
def get_url(pname: str) -> str:
"""Get the URL for package `pname`."""
url_mid = {"discord": "", "discord-ptb": "/ptb", "discord-canary": "/canary"}[pname]
url = f"https://discord.com/api/download{url_mid}?platform=linux&format=tar.gz"
return get_redirect_location(url)
def get_version(pname: str, url: str) -> str:
"""Extract the package version from the url returned by `get_url`."""
match = search(r"/(\d+\.\d+\.\d+)/(\w+(?:-\w+)?)-(\d+\.\d+\.\d+)\.tar\.gz$", url)
# Assert that the package name matches.
assert match[2] == pname
# As a sanity check, also assert that the versions match.
assert match[1] == match[3]
# Return the version.
return match[1]
def get_sha256(url: str) -> str:
"""Get the sha256 of `url` using `nix-prefetch-url`."""
process = run(
["nix-prefetch-url", "--type", "sha256", url], capture_output=True, check=True
)
return process.stdout.strip().decode("utf-8")
################################################
VersionDict = Dict[str, Union[str, Dict[str, str]]]
def load_versions() -> VersionDict:
"""Load the version information from `versions.json`"""
with open("versions.json", "r") as file:
return json.load(file)
def save_versions(versions: VersionDict) -> None:
"""Save the version information to `versions.json`"""
with open("versions.json", "w") as file:
json.dump(versions, file, sort_keys=True, indent=2)
file.write("\n")
def init_versions() -> None:
"""Initialize the check-side `versions.json` based on the one in the repository"""
url = f"https://github.com/{GITHUB_REPOSITORY}/raw/main/versions.json"
versions = json.loads(urlopen(url).read().decode("utf-8"))
save_versions({pname: data["version"] for pname, data in versions.items()})
################################################
def trigger_update(pname: str, version_old: str, version_new: str) -> None:
"""Send a `repository_dispatch` event to trigger the `update` workflow."""
data = {
# We only ever send a repository dispatch to update a package, so here we misuse the
# `event_type` field to show a helpful description of what's being updated.
"event_type": f"{pname}: {version_old} -> {version_new}",
"client_payload": {"package": pname},
}
request = Request(f"https://api.github.com/repos/{GITHUB_REPOSITORY}/dispatches")
request.add_header("Accept", "application/vnd.github.v3+json")
request.add_header("Authorization", f"token {environ['GITHUB_TOKEN']}")
urlopen(request, data=json.dumps(data).encode("utf-8"))
def check_for_updates() -> None:
"""Check for updates, and call `trigger_update` to update out-of-date packages."""
if not path.isfile("versions.json"):
print("warning: versions.json does not exist, creating it")
init_versions()
versions = load_versions()
print("checking for updates...")
for pname in ["discord", "discord-ptb", "discord-canary"]:
old_version = versions[pname]
new_version = get_version(pname, get_url(pname))
if old_version != new_version:
print(f" {pname}: updating from {old_version} to {new_version}...")
# Send a `repository_dispatch` event to update the package.
trigger_update(pname, old_version, new_version)
# Update `versions.json` with the new version.
# This is done regardless of whether the update succeeded, to prevent successive
# `repository_dispatch` events being sent when an update fails.
versions[pname] = new_version
save_versions(versions)
else:
print(f" {pname}: up-to-date ({old_version})")
################################################
def update_package(pname: str) -> None:
"""Update the values for package `pname` in `versions.json`."""
print(f"updating {pname}...")
# Get the new URL, version, and sha256.
url = get_url(pname)
version = get_version(pname, url)
sha256 = get_sha256(url)
# Load the version information from `versions.json`.
versions = load_versions()
old_version = versions[pname]["version"]
print(f" version: {old_version} -> {version}")
print(f" url: {url}")
print(f" sha256: {sha256}")
print()
# Update `versions.json` with the new values.
versions[pname]["version"] = version
versions[pname]["url"] = url
versions[pname]["sha256"] = sha256
save_versions(versions)
# Commit the new `versions.json`.
run(
[
"git",
"commit",
"versions.json",
"-m",
f"{pname}: {old_version} -> {version}",
],
check=True,
)
################################################
if __name__ == "__main__":
parser = ArgumentParser()
commands = parser.add_subparsers(required=True, dest="command")
commands.add_parser("check")
commands.add_parser("update").add_argument("pname", type=str)
args = parser.parse_args()
if args.command == "check":
check_for_updates()
elif args.command == "update":
update_package(args.pname)