forked from COMP1511UNSW/dcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_github_release.py
81 lines (72 loc) · 2.55 KB
/
create_github_release.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
#!/usr/bin/env python3
# https://stackoverflow.com/questions/38153418/can-someone-give-a-python-requests-example-of-uploading-a-release-asset-in-githu/52354681#52354681
import json, os, re, subprocess, sys
from urllib.parse import urlencode
from urllib.request import Request, urlopen
REPO = 'COMP1511UNSW/dcc'
URL_TEMPLATE = 'https://{}.github.com/repos/' + REPO + '/releases'
def make_release(token, tag):
# print('token', token)
# print('tag', tag)
# print('url', URL_TEMPLATE.format('api'),)
_json = json.loads(urlopen(Request(
URL_TEMPLATE.format('api'),
json.dumps({
'tag_name': tag,
'name': tag,
'prerelease': False,
}).encode(),
headers={
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + token,
},
)).read().decode())
return _json['id']
def upload_file(token, pathname, release_id):
with open(pathname, 'br') as myfile:
content = myfile.read()
# print('pathname', pathname)
# print('token', token)
# print('url', URL_TEMPLATE.format('uploads') + '/' + str(release_id) + '/assets?' \
# + urlencode({'name': os.path.split(pathname)[1]}))
json.loads(urlopen(Request(
URL_TEMPLATE.format('uploads') + '/' + str(release_id) + '/assets?' \
+ urlencode({'name': os.path.split(pathname)[1]}),
content,
headers={
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + token,
'Content-Type': 'application/zip',
},
)).read().decode())
def run(command):
print(' '.join(command))
subprocess.check_call(command)
def update_readme(tag):
with open('README.md') as f:
contents = f.read()
contents = re.sub(r'dcc/releases/download/[^/]+', 'dcc/releases/download/' + tag, contents)
contents = re.sub(r'dcc_[\w\.]+_all.deb', f'dcc_{tag}_all.deb', contents)
with open('README.md', 'w') as f:
f.write(contents)
def main():
if len(sys.argv) != 3:
print("Usage:", sys.argv[0], '<release-tag> <release-description>', file=sys.stderr)
sys.exit(1)
tag = sys.argv[1] # e.g 1.0'
description = sys.argv[2] # description
token = os.environ.get('GITHUB_TOKEN', '')
with open(os.path.join(os.environ.get('HOME', ''), '.github_token')) as f:
token = f.read().strip()
update_readme(tag)
run(['git', 'commit', 'README.md', '-m', 'release ' + tag])
run(['git', 'tag', '-a', tag, '-m', description])
run(['git', 'push'])
run(['git', 'push', 'origin', tag])
run(['rm', '-f', 'dcc'])
run(['make', 'deb'])
release_id = make_release(token, tag)
for pathname in ['dcc', f'packaging/debian/dcc_{tag}_all.deb']:
upload_file(token, pathname, release_id)
if __name__ == '__main__':
main()