From c3308f04544e42b8596db75e72063bc5a5cff5c8 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 4 Sep 2015 21:43:37 -0400 Subject: A script to download Appveyor artifacts --- ci/download_appveyor.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 ci/download_appveyor.py (limited to 'ci') diff --git a/ci/download_appveyor.py b/ci/download_appveyor.py new file mode 100644 index 00000000..ecb1a2fb --- /dev/null +++ b/ci/download_appveyor.py @@ -0,0 +1,72 @@ +"""Use the Appveyor API to download Windows artifacts.""" + +import os +import os.path +import pprint +import zipfile + +import requests + + +def make_auth_headers(): + with open("ci/appveyor.token") as f: + token = f.read().strip() + + headers = { + 'Authorization': 'Bearer {}'.format(token), + } + return headers + + +def get_project_build(account, project): + url = "https://ci.appveyor.com/api/projects/{account}/{project}".format(account=account, project=project) + response = requests.get(url, headers=make_auth_headers()) + return response.json() + + +def download_latest_artifacts(account, project): + build = get_project_build(account, project) + jobs = build['build']['jobs'] + print "Build {0[build][version]}, {1} jobs: {0[build][message]}".format(build, len(jobs)) + for job in jobs: + name = job['name'].partition(':')[2].split(',')[0].strip() + print " {0}: {1[status]}, {1[artifactsCount]} artifacts".format(name, job) + + url = "https://ci.appveyor.com/api/buildjobs/{jobid}/artifacts".format(jobid=job['jobId']) + response = requests.get(url, headers=make_auth_headers()) + artifacts = response.json() + + for artifact in artifacts: + type = artifact['type'] + filename = artifact['fileName'] + print " {0}".format(filename) + + url = "https://ci.appveyor.com/api/buildjobs/{jobid}/artifacts/{filename}".format(jobid=job['jobId'], filename=filename) + download_url(url, filename, make_auth_headers()) + + if type == "Zip": + unpack_zipfile(filename) + os.remove(filename) + + +def download_url(url, filename, headers): + dirname, _ = os.path.split(filename) + if dirname and not os.path.exists(dirname): + os.makedirs(dirname) + response = requests.get(url, headers=headers, stream=True) + if response.status_code == 200: + with open(filename, 'wb') as f: + for chunk in response.iter_content(16*1024): + f.write(chunk) + + +def unpack_zipfile(filename): + with open(filename, 'rb') as fzip: + z = zipfile.ZipFile(fzip) + for name in z.namelist(): + print " extracting {}".format(name) + z.extract(name) + + +if __name__ == "__main__": + download_latest_artifacts("nedbat", "coveragepy") -- cgit v1.2.1