-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathupload-assets
executable file
·99 lines (84 loc) · 4.14 KB
/
upload-assets
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
#!/usr/bin/env python3
import argparse
import os
import github_releases
parser = argparse.ArgumentParser(description="""
Upload a release to GitHub: the .jar, the source archive (so the signatures
will match it), the Java installer, and the Windows installer. If no directory
is specified it will read existing assets.
It assumes the build tag is 'build' followed by the build number
zero-padded to 5 characters.
""")
parser.add_argument('build_number', help='The build number to upload.')
parser.add_argument('release_directory',
help='Path to the FreenetReleased directory containing '
'the assets to upload.', nargs='?', default=None)
args = parser.parse_args()
testing = "-" in args.build_number
if testing: # testing-release, eg 1491-pre1
build_tag = 'build{0:05d}'.format(int(args.build_number.split("-")[0])) + "-" + args.build_number.split("-")[1]
else:
build_tag = 'build{0:05d}'.format(int(args.build_number))
# TODO: Allow resuming: avoid clobbering existing assets when uploading.
# TODO: Nicer error on missing environment variable.
# TODO: Bind owner and repo to GitHubReleases instance? They're in every call.
# TODO: Restructure the body of this into a module?
# TODO: The name 'upload-assets' makes the non-upload behavior surprising.
# What's a better name for this script?
conn = github_releases.GitHubReleases(os.environ['GITHUB_OAUTH'],
'Freenet-Release-Uploader')
# Create a release at the tag, or get its ID if one already exists.
try:
release = conn.create('hyphanet', 'fred', build_tag)
release_id = release['id']
print('Created release with ID {}'.format(release_id))
except github_releases.GitHubError as e:
# TODO: Avoid hardcoding here?
if e.response['errors'][0]['code'] == 'already_exists':
release_id = conn.get('hyphanet', 'fred', build_tag)['id']
print('Found existing release with ID {}'.format(release_id))
else:
raise
files = {
'jar': 'freenet-{0}.jar'.format(build_tag) if not testing else 'freenet-testing-build-{0}.jar'.format(args.build_number),
'source': 'freenet-{0}-source.tar.bz2'.format(build_tag) if not testing else 'freenet-testing-build-{0}-source.tar.bz2'.format(args.build_number),
'java_installer': 'new_installer_offline_{0}.jar'.format(args.build_number),
'windows_installer': 'FreenetInstaller-{0}.exe'.format(args.build_number),
}
urls = {}
existing_assets = conn.list_assets("hyphanet", "fred", release_id)
# Upload if the release directory is specified, otherwise find existing assets.
if args.release_directory is not None:
existing_asset_files = [asset['name'] for asset in existing_assets]
def upload_asset(release_id, name, file, asset_type=None):
path = os.path.join(args.release_directory, file)
if file not in existing_asset_files:
print("Uploading asset {}".format(file))
result = conn.upload_asset("hyphanet", "fred", release_id, path,
asset_type)
if name == 'jar':
result = conn.upload_asset("hyphanet", "fred", release_id, path,
asset_type, filename="freenet.jar")
urls[name + '_url'] = result['browser_download_url']
else:
print("Already uploaded {}".format(file))
asset = next(asset for asset in existing_assets
if asset['name'] == file)
assert asset is not None
urls[name + '_url'] = asset['browser_download_url']
for name, file in files.items():
upload_asset(release_id, name, file)
upload_asset(release_id, name + "_signature", file + ".sig",
"application/pgp-signature")
else:
assets = {}
for asset in existing_assets:
assets[asset['name']] = asset['browser_download_url']
for name, file in files.items():
if file not in assets:
print('Missing asset: {}'.format(file))
exit(1)
print("Found asset {}".format(file))
urls[name + "_url"] = assets[file]
# Each asset and its signature must have a URL.
assert 2 * len(files) == len(urls)