summaryrefslogtreecommitdiff
path: root/ci/download_appveyor.py
blob: ecb1a2fb5486205ef056e0a9a44ac676717d247f (plain)
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
"""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")