summaryrefslogtreecommitdiff
path: root/appveyor/getwheels.py
blob: 84ac03045bea7822ca050074695094eff757c523 (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
73
74
75
76
77
78
# coding: utf-8
from __future__ import absolute_import, division, print_function

import os
import sys
from textwrap import dedent

import requests

URL = 'https://ci.appveyor.com/api'
TOKEN = os.getenv('APPVEYOR_TOKEN')
ACCOUNT = 'zzzeek'  # AppVeyor username, assuming zzzeek
PROJECT = 'sqlalchemy'

if len(sys.argv) != 2:
    sys.exit('getwheels.py <branch>')

if TOKEN is None:
    sys.exit('APPVEYOR_TOKEN env var not set.')

branch = sys.argv[1]

session = requests.Session()
session.headers.update({'Authorization': 'Bearer ' + TOKEN})

BRANCH_BUILD_URL = '{}/projects/{}/{}/branch/{}'.format(
    URL, ACCOUNT, PROJECT, branch)

response = session.get(BRANCH_BUILD_URL)
response.raise_for_status()

build_data = response.json()['build']

message = dedent('''
    Downloading wheels for latest build on branch {bd[branch]!r}.

    Branch:          {bd[branch]}
    AppVeyor build:  {bd[buildNumber]}
    Commit ID:       {bd[commitId]}
    Commit message:  {bd[message]}

    Build status:    {bd[status]}
'''.format(bd=build_data))

print(message)

if build_data['status'] == 'failed':
    sys.exit('Build failed, aborting download.')
elif build_data['status'] == 'running':
    sys.exit('Build still running, aborting download.')

job_ids = [job['jobId'] for job in build_data['jobs']]


def download_artifact(artifact):
    FILE_URL = '{}/buildjobs/{}/artifacts'.format(
        URL, job_id, artifact['fileName'])

    print('Downloading', artifact['fileName'])

    response = session.get(FILE_URL, stream=True)
    response.raise_for_status()

    with open(artifact['fileName'], 'wb') as fp:
        for chunk in response.iter_content(chunk_size=100 * 1024):
            fp.write(chunk)

try:
    os.mkdir('dist')
except OSError:
    pass

for job_id in job_ids:
    ARTIFACTS_URL = '{}/buildjobs/{}/artifacts'.format(URL, job_id)
    response = session.get(ARTIFACTS_URL)
    for artifact in response.json():
        if artifact['fileName'].endswith('.whl'):
            download_artifact(artifact)