1#! /usr/bin/env python
2
3# Copyright 2018 Google LLC.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7from subprocess import check_output, CalledProcessError
8import os
9import re
10import sys
11import tempfile
12
13HEADER = '''<!DOCTYPE html>
14<html lang="en">
15<head>
16<meta charset="utf-8">
17<title>SkQP Pre-built APKs</title>
18<meta name="viewport" content="width=device-width, initial-scale=1.0">
19<style>
20body {
21font-family:sans-serif;
22max-width:50em;
23margin:8px auto;
24padding:0 8px;
25}
26table { max-width:100%; border-collapse: collapse; }
27td { padding:12px 8px; vertical-align:top; }
28tr:nth-child(even) {background: #F2F2F2; color:#000;}
29tr:nth-child(odd)  {background: #FFFFFF; color:#000;}
30</style>
31</head>
32<body>
33<h1>SkQP Pre-built APKs</h1>
34'''
35FOOTER = '</body>\n</html>\n'
36
37BUCKET = 'skia-skqp'
38
39NAME_FMT = 'skqp-universal-%s.apk'
40
41def get_existing_files():
42    cmd = ['gsutil', 'ls', 'gs://' + BUCKET]
43    try:
44        output = check_output(cmd)
45    except (OSError, CalledProcessError):
46        sys.stderr.write('command: "%s" failed.\n' % ' '.join(cmd))
47        sys.exit(1)
48    result = set()
49    regex = re.compile('gs://%s/%s' % (BUCKET, NAME_FMT % '([0-9a-f]+)'))
50    for line in output.split('\n'):
51        m = regex.match(line.strip())
52        if m is not None:
53            result.add(m.group(1))
54    return result
55
56def find(v, extant):
57    l = min(16, len(v))
58    while l > 8:
59        if v[:l] in extant:
60            return v[:l]
61        l -= 1
62    return None
63
64def nowrap(s):
65    return (s.replace(' ', u'\u00A0'.encode('utf-8'))
66             .replace('-', u'\u2011'.encode('utf-8')))
67
68def table(o, from_commit, to_commit):
69    env_copy = os.environ.copy()
70    env_copy['TZ'] = ''
71    extant = get_existing_files()
72    o.write('<h2>%s %s</h2>\n' % (to_commit, ' '.join(from_commit)))
73    o.write('<table>\n<tr><th>APK</th><th>Date</th><th>Commit</th></tr>\n')
74    git_cmd = ['git', 'log', '--format=%H;%cd;%<(100,trunc)%s',
75               '--date=format-local:%Y-%m-%d %H:%M:%S %Z'
76               ] + from_commit + [to_commit]
77    commits = check_output(git_cmd, env=env_copy)
78    for line in commits.split('\n'):
79        line = line.strip()
80        if not line:
81            continue
82        commit, date, subj = line.split(';', 2)
83        short = find(commit, extant)
84        if short is not None:
85            apk_name =  NAME_FMT % short
86            url = 'https://storage.googleapis.com/%s/%s' % (BUCKET, apk_name)
87        else:
88            apk_name, url =  '', ''
89        commit_url = 'https://skia.googlesource.com/skia/+/' + commit
90        o.write('<tr>\n<td><a href="%s">%s</a></td>\n'
91                '<td>%s</td>\n<td><a href="%s">%s</a></td>\n</tr>\n' %
92                (url, nowrap(apk_name), nowrap(date), commit_url, subj))
93    o.write('</table>\n')
94
95def main():
96    assert '/' in [os.sep, os.altsep] and '..' == os.pardir
97    os.chdir(os.path.join(os.path.dirname(__file__), '../..'))
98    d = tempfile.mkdtemp()
99    path = os.path.join(d, 'apklist.html')
100    with open(path, 'w') as o:
101        o.write(HEADER)
102        table(o, ['^origin/master', '^3e34285f2a0'], 'origin/skqp/dev')
103        table(o, ['^origin/master'], 'origin/skqp/release')
104        o.write(FOOTER)
105    print path
106    gscmd = 'gsutil -h "Content-Type:text/html" cp "%s" gs://skia-skqp/apklist'
107    print gscmd % path
108
109if __name__ == '__main__':
110    main()
111
112