summaryrefslogtreecommitdiff
path: root/scripts/list-nightly-builds.py
blob: 078f8cc795361c88c3d6d7d8be0b8f5cf4dc962f (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
79
80
81
82
83
84
#!/usr/bin/env python3
#
# Builds a list of nightly builds from S3
#
# Requires the following environment variables:
# SNAPSHOTS_BUCKET
# AWS_ACCESS_KEY_ID
# AWS_SECRET_ACCESS_KEY

import os
import re
import sys
import json

import boto3

import jinja2

from functools import cmp_to_key

def make_link(bucket, prefix, file):
    f = re.sub(r'\s+', '+', file)
    return "https://s3.amazonaws.com/{0}/{1}/{2}".format(bucket, prefix, file)

def compare(l, r):
    try:
        regex = r'\-(\d+)\-'
        l_date = int(re.search(regex, l).group(1))
        r_date = int(re.search(regex, r).group(1))
        if l_date == r_date:
            return 0
        elif l_date > r_date:
            return 1
        else:
            return -1
    except:
        return(-1)


def list_image_files(bucket, prefix):
    s3 = boto3.client('s3')
    object_listing = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
    data = object_listing['Contents']

    files = []
    for f in data:
        files.append(f['Key'])

    return files

def render_image_list(bucket, prefix, files):
    regex = prefix + r'/(.*?)'
    file_names = list(set(map(lambda s: re.sub(regex, r'\1', s), files)))
    file_names.sort(reverse=True, key=cmp_to_key(compare))

    builds = []

    for name in file_names:
        build = {}
        build['file'] = name
        build['link'] = make_link(bucket, prefix, name)

        builds.append(build)

    tmpl = jinja2.Template("""
      <ul>
      {% for b in builds %}
        <li><a href="{{b.link}}">{{b.file}}</a></li>
      {% endfor %}
     </ul>
    """)
    print(tmpl.render(builds=builds))

if __name__ == '__main__':
    bucket = os.getenv("SNAPSHOTS_BUCKET")

    try:
        prefix = sys.argv[1]
    except:
        print("Please specify directory prefix!", file=sys.stderr)
        sys.exit(1)

    files = list_image_files(bucket, prefix)
    render_image_list(bucket, prefix, files)