aboutsummaryrefslogtreecommitdiff
path: root/.ci/s3-download-release.py
blob: 223f7f17078f7190a6d98183682d519b75518ee0 (about) (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
#!/usr/bin/env python3


import argparse
import os
import os.path
import sys
import urllib.request

import tinys3


def main():
    parser = argparse.ArgumentParser(description='S3 File Uploader')
    parser.add_argument(
        '--s3-bucket',
        help=('S3 bucket name (defaults to $S3_UPLOAD_BUCKET)'),
        default=os.environ.get('S3_UPLOAD_BUCKET'))
    parser.add_argument(
        '--s3-region',
        help=('S3 region (defaults to $S3_UPLOAD_REGION)'),
        default=os.environ.get('S3_UPLOAD_REGION'))
    parser.add_argument(
        '--s3-username',
        help=('S3 username (defaults to $S3_UPLOAD_USERNAME)'),
        default=os.environ.get('S3_UPLOAD_USERNAME'))
    parser.add_argument(
        '--s3-key',
        help=('S3 access key (defaults to $S3_UPLOAD_ACCESSKEY)'),
        default=os.environ.get('S3_UPLOAD_ACCESSKEY'))
    parser.add_argument(
        '--s3-secret',
        help=('S3 secret (defaults to $S3_UPLOAD_SECRET)'),
        default=os.environ.get('S3_UPLOAD_SECRET'))
    parser.add_argument(
        '--destdir',
        help='Destination directory.')
    parser.add_argument(
        'package', metavar='PACKAGE',
        help='Package name and version to download.')

    args = parser.parse_args()

    if args.s3_region:
        endpoint = 's3-{}.amazonaws.com'.format(args.s3_region.lower())
    else:
        endpoint = 's3.amazonaws.com'

    conn = tinys3.Connection(
        access_key=args.s3_key,
        secret_key=args.s3_secret,
        default_bucket=args.s3_bucket,
        tls=True,
        endpoint=endpoint,
    )

    files = []

    for entry in conn.list(args.package):
        files.append(entry['key'])

    destdir = args.destdir or os.getpwd()

    for file in files:
        print('Downloading {}...'.format(file))
        url = 'https://{}/{}/{}'.format(endpoint, args.s3_bucket, file)
        target = os.path.join(destdir, file)
        urllib.request.urlretrieve(url, target)

    return 0


if __name__ == '__main__':
    sys.exit(main())