diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-09-04 21:43:37 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-09-04 21:43:37 -0400 |
commit | c3308f04544e42b8596db75e72063bc5a5cff5c8 (patch) | |
tree | 9a5b934c7af4f701b8988286eaa855f2ff656efc /ci | |
parent | d768e9a608b82f7a44f9bd27e80e3599a3fccc1f (diff) | |
download | python-coveragepy-git-c3308f04544e42b8596db75e72063bc5a5cff5c8.tar.gz |
A script to download Appveyor artifacts
Diffstat (limited to 'ci')
-rw-r--r-- | ci/download_appveyor.py | 72 |
1 files changed, 72 insertions, 0 deletions
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") |