| 1 |
#!/usr/bin/env python3 |
| 2 |
import argparse |
| 3 |
import io |
| 4 |
import json |
| 5 |
import os.path |
| 6 |
import re |
| 7 |
import urllib.request |
| 8 |
import zipfile |
| 9 |
|
| 10 |
DEFINITION_RE = re.compile( |
| 11 |
r'https://dev.azure.com/(?P<org>[^/]+)/(?P<project>[^/]+)' |
| 12 |
r'/_build/latest\?definitionId=(?P<definition>\d+)', |
| 13 |
) |
| 14 |
BUILDS = 'https://dev.azure.com/{org}/{project}/_apis/build/builds?api-version=5.0&definitions={definition}&$top=5' # noqa: E501 |
| 15 |
ARTIFACTS = 'https://dev.azure.com/{org}/{project}/_apis/build/builds/{build}/artifacts?api-version=5.0' # noqa: E501 |
| 16 |
|
| 17 |
|
| 18 |
def main() -> int: |
| 19 |
parser = argparse.ArgumentParser() |
| 20 |
parser.add_argument('tag') |
| 21 |
parser.add_argument('--dest', default='dist') |
| 22 |
args = parser.parse_args() |
| 23 |
|
| 24 |
with open('README.rst') as f: |
| 25 |
match = DEFINITION_RE.search(f.read()) |
| 26 |
assert match |
| 27 |
|
| 28 |
builds_resp = urllib.request.urlopen(BUILDS.format(**match.groupdict())) |
| 29 |
builds_json = json.load(builds_resp) |
| 30 |
|
| 31 |
build_id = next( |
| 32 |
iter([ |
| 33 |
build['id'] for build in builds_json['value'] |
| 34 |
if build['status'] == 'completed' |
| 35 |
if build['result'] == 'succeeded' |
| 36 |
if build['sourceBranch'] == f'refs/tags/{args.tag}' |
| 37 |
]), |
| 38 |
) |
| 39 |
|
| 40 |
artifacts_url = ARTIFACTS.format(build=build_id, **match.groupdict()) |
| 41 |
artifacts_resp = urllib.request.urlopen(artifacts_url) |
| 42 |
artifacts_json = json.load(artifacts_resp) |
| 43 |
|
| 44 |
os.makedirs(args.dest, exist_ok=True) |
| 45 |
for artifact in artifacts_json['value']: |
| 46 |
if artifact['name'].startswith('wheel_'): |
| 47 |
print(f'artifact: {artifact["name"]}') |
| 48 |
resp = urllib.request.urlopen(artifact['resource']['downloadUrl']) |
| 49 |
bio = io.BytesIO(resp.read()) |
| 50 |
with zipfile.ZipFile(bio) as zipf: |
| 51 |
for info in zipf.infolist(): |
| 52 |
if info.filename.endswith('.whl'): |
| 53 |
info.filename = os.path.basename(info.filename) |
| 54 |
zipf.extract(info, path=args.dest) |
| 55 |
return 0 |
| 56 |
|
| 57 |
|
| 58 |
if __name__ == '__main__': |
| 59 |
raise SystemExit(main()) |