-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.py
executable file
·87 lines (61 loc) · 2.33 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
#! /usr/bin/env nix-shell
#! nix-shell -i python -p python3 -p python3Packages.pyyaml -p python3Packages.packaging nix nix-prefetch-git
import argparse
import json
import subprocess
import sys
import yaml
from packaging.version import Version, parse
from os.path import abspath, dirname
from urllib.request import urlopen
BASE_URL = 'https://storage.googleapis.com/shadow-update/launcher'
FILE = dirname(abspath(__file__)) + '/upstream-info.json'
CHANNELS = ['testing', 'preprod', 'prod']
def fetch_upstream_versions():
"""Loads the given JSON file."""
result = {}
for channel in CHANNELS:
url = f'{BASE_URL}/{channel}/linux/ubuntu_18.04/latest-linux.yml'
with urlopen(url) as response:
body = response.read()
result[channel] = yaml.safe_load(body)
return result
def load_json(path):
"""Loads the given JSON file."""
with open(path, 'r') as f:
return json.load(f)
def commit_upstream_infos(content, commit_message):
with open(FILE, 'w') as f:
f.write(json.dumps(content, indent=2))
f.write('\n')
subprocess.run(['git', 'add', FILE], check=True)
subprocess.run(['git', 'commit', '--file=-'], input=commit_message.encode(), check=True)
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('--commit', action='store_true')
parser.add_argument('--channels', nargs='+', default=CHANNELS, choices=CHANNELS)
return parser.parse_args()
def main() -> int:
args = parse_args()
upstream_channels = fetch_upstream_versions()
current_channels = load_json(FILE)
for channel in CHANNELS:
version_old = current_channels[channel]["version"]
version_new = upstream_channels[channel]["version"]
# Skip channel if the version did not change
if parse(version_old) >= parse(version_new):
continue
commit_message = 'shadow-{}: {} -> {}'.format(
channel,
version_old,
version_new
)
new_content = current_channels
new_content[channel] = upstream_channels[channel]
# Commit the upstream-info.json file if needed
if args.commit:
commit_upstream_infos(new_content, commit_message)
print(commit_message)
if __name__ == '__main__':
sys.exit(main())