-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupdate_releases.py
executable file
·76 lines (54 loc) · 1.9 KB
/
update_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
#!/usr/bin/env python
import json
import os
import sys
from pathlib import Path
from shutil import rmtree
import requests
RELEASES_URL = os.getenv('RELEASES_URL', 'https://nucleus.mozilla.org/rna/all-releases.json')
OUTPUT_DIR = Path(__file__).with_name('releases')
LAST_MODIFIED = OUTPUT_DIR.joinpath('.last-modified')
def get_request_headers():
headers = {}
if LAST_MODIFIED.exists():
with LAST_MODIFIED.open() as fh:
headers['If-Modified-Since'] = fh.read()
return headers
def get_release_data(everything):
last_modified = None
if everything:
resp = requests.get(RELEASES_URL, {'all': 'true'}, timeout=60)
else:
headers = get_request_headers()
resp = requests.get(RELEASES_URL, headers=headers, timeout=30)
resp.raise_for_status()
if resp.status_code == 304:
# no updates, nothing modified since last_modified
return None, None
if 'last-modified' in resp.headers:
last_modified = resp.headers['last-modified']
return resp.json(), last_modified
def cleanup():
rmtree(str(OUTPUT_DIR), ignore_errors=True)
OUTPUT_DIR.mkdir()
def write_files(release_data, last_modified=None):
for release in release_data:
with OUTPUT_DIR.joinpath('%s.json' % release['slug']).open('w') as fp:
json.dump(release, fp, indent=2, sort_keys=True)
if last_modified:
with LAST_MODIFIED.open('w') as fh:
fh.write(last_modified)
print('Wrote {} files'.format(len(release_data)))
def main(everything):
try:
data, last_modified = get_release_data(everything)
except requests.RequestException as e:
return 'ERROR: %s' % e
if data:
if everything:
cleanup()
write_files(data, last_modified)
else:
print('Releases are up to date. No update required.')
if __name__ == '__main__':
sys.exit(main(sys.argv[-1] == '--all'))