|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This is a simple script to download Windows installers from AppVeyor. |
| 4 | +# Use this script to automate the Windows release process. |
| 5 | +# |
| 6 | +# USAGE |
| 7 | +# |
| 8 | +# $ appveyor-download v1.2.1 # Download installers for v1.2.1 |
| 9 | +# |
| 10 | + |
| 11 | +import json |
| 12 | +import sys |
| 13 | +import os |
| 14 | +import urllib.request |
| 15 | +import datetime |
| 16 | + |
| 17 | +# |
| 18 | +# GLOBAL VARIABLES |
| 19 | + |
| 20 | +GITHUB_URL = 'https://api.github.com/repos/fluent/fluent-bit/statuses/%s' |
| 21 | + |
| 22 | +APPVAYOR_JOBS = 'https://ci.appveyor.com/api/projects/fluent/%s/builds/%s' |
| 23 | +APPVAYOR_LIST = 'https://ci.appveyor.com/api/buildjobs/%s/artifacts' |
| 24 | +APPVAYOR_FILE = 'https://ci.appveyor.com/api/buildjobs/%s/artifacts/%s' |
| 25 | + |
| 26 | +# |
| 27 | +# Util functions |
| 28 | + |
| 29 | +def now(): |
| 30 | + return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| 31 | + |
| 32 | +def usage(): |
| 33 | + print('Usage: appveyor-download VERSION') |
| 34 | + return -1 |
| 35 | + |
| 36 | +def LOG(*args): |
| 37 | + print(now(), *args, file=sys.stderr) |
| 38 | + |
| 39 | +# |
| 40 | +# REST API |
| 41 | + |
| 42 | +def json_api(url): |
| 43 | + LOG('Request %s' % url) |
| 44 | + with urllib.request.urlopen(url) as fp: |
| 45 | + return json.loads(fp.read().decode()) |
| 46 | + |
| 47 | +def build_get(version): |
| 48 | + resp = json_api(GITHUB_URL % version) |
| 49 | + |
| 50 | + for item in resp: |
| 51 | + if item['description'] == 'AppVeyor build succeeded': |
| 52 | + build_url = item['target_url'] |
| 53 | + break |
| 54 | + else: |
| 55 | + raise RuntimeError('No AppVeyor build found: %s' % resp) |
| 56 | + |
| 57 | + tokens = build_url.split('/') |
| 58 | + |
| 59 | + return (tokens[-3], tokens[-1]) |
| 60 | + |
| 61 | +def jobs_get(project_id, build_id): |
| 62 | + resp = json_api(APPVAYOR_JOBS % (project_id, build_id)) |
| 63 | + return list(node['jobId'] for node in resp['build']['jobs']) |
| 64 | + |
| 65 | +def filenames_get(job_id): |
| 66 | + resp = json_api(APPVAYOR_LIST % job_id) |
| 67 | + return list(node['fileName'] for node in resp) |
| 68 | + |
| 69 | +# |
| 70 | +# Main |
| 71 | + |
| 72 | +def main(args): |
| 73 | + if not args: |
| 74 | + return usage() |
| 75 | + |
| 76 | + project_id, build_id = build_get(args[0]) |
| 77 | + |
| 78 | + for job_id in jobs_get(project_id, build_id): |
| 79 | + for filename in filenames_get(job_id): |
| 80 | + |
| 81 | + with urllib.request.urlopen(APPVAYOR_FILE % (job_id, filename)) as fp: |
| 82 | + data = fp.read() |
| 83 | + size = len(data) |
| 84 | + |
| 85 | + outfile = os.path.basename(filename) |
| 86 | + with open(outfile, 'wb') as fw: |
| 87 | + fw.write(data) |
| 88 | + |
| 89 | + LOG('* %s (%s bytes)' % (outfile, size)) |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + sys.exit(main(sys.argv[1:])) |
0 commit comments