~juju-qa/juju-ci-tools/trunk

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
79
80
81
#!/usr/bin/env python
from __future__ import print_function

from argparse import ArgumentParser
from ConfigParser import ConfigParser
import logging
import os
import re
import sys

from boto.s3.connection import S3Connection


JUJU_QA_DATA = 'juju-qa-data'
JUJU_PIP_ARCHIVES = 'juju-pip-archives'
CLOUD_CITY = os.path.join(os.environ['HOME'], 'cloud-city')
jujuqa_config = os.path.join(CLOUD_CITY, 'juju-qa.s3cfg')


log = logging.getLogger("s3-check")
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter(
    fmt='%(asctime)s %(levelname)s %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'))
log.addHandler(handler)
log.setLevel(logging.INFO)


def parse_args(args=None):
    parser = ArgumentParser()
    parser.add_argument(
        '--configs', nargs='*', default=[jujuqa_config],
        help='The job to get files from')
    parser.add_argument(
        'buckets', nargs='*', default=[JUJU_QA_DATA, JUJU_PIP_ARCHIVES],
        help='The job to get files from')

    return parser.parse_args(args)


def get_s3_credentials(s3cfg_path):
    config = ConfigParser()
    with open(s3cfg_path) as fp:
        config.readfp(fp)
    access_key = config.get('default', 'access_key')
    secret_key = config.get('default', 'secret_key')
    return access_key, secret_key


def get_qa_data_bucket(config, bucket):
    credentials = get_s3_credentials(config)
    conn = S3Connection(*credentials)
    return conn.get_bucket(bucket)


def list_file_keys(bucket, prefix, file_regex):
    pattern = re.compile(file_regex)
    keys = bucket.list(prefix)
    filtered = []
    for number, key in enumerate(keys):
        if pattern.match(key.name):
            filtered.append(key.name)
        if number >= 5:
            break
    return filtered


def main():
    args = parse_args()
    for config in args.configs:
        for bucket in args.buckets:
            log.info('trying {} with {}'.format(config, bucket))
            bucket = get_qa_data_bucket(config, bucket)
            log.info(bucket.generate_url(999999999))
            keys = list_file_keys(bucket, '', '.*')
            for key in keys:
                log.info('    Found {}'.format(key))


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