|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Downloads Azure Pipelines artifacts to a destination directory. |
| 4 | + |
| 5 | +import argparse |
| 6 | +import io |
| 7 | +import pathlib |
| 8 | +import zipfile |
| 9 | + |
| 10 | +import requests |
| 11 | + |
| 12 | + |
| 13 | +URL = 'https://dev.azure.com/gregoryszorc/python-zstandard/_apis/build/builds/{build}/artifacts?api-version=4.1' |
| 14 | + |
| 15 | +FETCH_NAME_PREFIXES = ( |
| 16 | + 'MacOSWheel', |
| 17 | + 'ManyLinuxWheel', |
| 18 | + 'SourceDistribution', |
| 19 | + 'WindowsAnaconda', |
| 20 | + 'Windowsx64', |
| 21 | + 'Windowsx86', |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def download_artifacts(build: str, dest: pathlib.Path): |
| 26 | + if not dest.exists(): |
| 27 | + dest.mkdir(parents=True) |
| 28 | + |
| 29 | + session = requests.session() |
| 30 | + |
| 31 | + index = session.get(URL.format(build=build)).json() |
| 32 | + |
| 33 | + for entry in index['value']: |
| 34 | + if not entry['name'].startswith(FETCH_NAME_PREFIXES): |
| 35 | + print('skipping %s' % entry['name']) |
| 36 | + continue |
| 37 | + |
| 38 | + print('downloading %s' % entry['resource']['downloadUrl']) |
| 39 | + zipdata = io.BytesIO(session.get(entry['resource']['downloadUrl']).content) |
| 40 | + |
| 41 | + with zipfile.ZipFile(zipdata, 'r') as zf: |
| 42 | + for name in zf.namelist(): |
| 43 | + name_path = pathlib.Path(name) |
| 44 | + dest_path = dest / name_path.name |
| 45 | + print('writing %s' % dest_path) |
| 46 | + |
| 47 | + with dest_path.open('wb') as fh: |
| 48 | + fh.write(zf.read(name)) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + parser = argparse.ArgumentParser() |
| 53 | + parser.add_argument('build', help='which build to download. e.g. "42"') |
| 54 | + parser.add_argument('dest', help='destination directory') |
| 55 | + |
| 56 | + args = parser.parse_args() |
| 57 | + |
| 58 | + download_artifacts(args.build, pathlib.Path(args.dest)) |
| 59 | + |
0 commit comments