| 1 |
import os |
| 2 |
import urllib2 |
| 3 |
import json |
| 4 |
import sys |
| 5 |
import md5 |
| 6 |
|
| 7 |
wanted = sys.argv[1:] |
| 8 |
if not wanted: |
| 9 |
wanted = ['default'] |
| 10 |
base = "http://buildbot.pypy.org/json/builders/" |
| 11 |
|
| 12 |
cachedir = os.environ.get('PYPY_BUILDS_CACHE') |
| 13 |
if cachedir and not os.path.exists(cachedir): |
| 14 |
os.makedirs(cachedir) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
def get_json(url, cache=cachedir): |
| 19 |
return json.loads(get_data(url, cache)) |
| 20 |
|
| 21 |
|
| 22 |
def get_data(url, cache=cachedir): |
| 23 |
url = str(url) |
| 24 |
if cache: |
| 25 |
digest = md5.md5() |
| 26 |
digest.update(url) |
| 27 |
digest = digest.hexdigest() |
| 28 |
cachepath = os.path.join(cachedir, digest) |
| 29 |
if os.path.exists(cachepath): |
| 30 |
with open(cachepath) as fp: |
| 31 |
return fp.read() |
| 32 |
|
| 33 |
print 'GET', url |
| 34 |
fp = urllib2.urlopen(url) |
| 35 |
try: |
| 36 |
data = fp.read() |
| 37 |
if cache: |
| 38 |
with open(cachepath, 'wb') as cp: |
| 39 |
cp.write(data) |
| 40 |
return data |
| 41 |
finally: |
| 42 |
fp.close() |
| 43 |
|
| 44 |
def parse_log(log): |
| 45 |
items = [] |
| 46 |
for v in log.splitlines(1): |
| 47 |
if not v[0].isspace() and v[1].isspace(): |
| 48 |
items.append(v) |
| 49 |
return sorted(items) |
| 50 |
|
| 51 |
def gather_logdata(build): |
| 52 |
logdata = get_data(str(build['log']) + '?as_text=1') |
| 53 |
logdata = logdata.replace('</span><span class="stdout">', '') |
| 54 |
logdata = logdata.replace('</span></pre>', '') |
| 55 |
del build['log'] |
| 56 |
build['log'] = parse_log(logdata) |
| 57 |
|
| 58 |
|
| 59 |
def branch_mapping(l): |
| 60 |
keep = 3 - len(wanted) |
| 61 |
d = {} |
| 62 |
for x in reversed(l): |
| 63 |
gather_logdata(x) |
| 64 |
if not x['log']: |
| 65 |
continue |
| 66 |
b = x['branch'] |
| 67 |
if b not in d: |
| 68 |
d[b] = [] |
| 69 |
d[b].insert(0, x) |
| 70 |
if len(d[b]) > keep: |
| 71 |
d[b].pop() |
| 72 |
return d |
| 73 |
|
| 74 |
def cleanup_build(d): |
| 75 |
for a in 'times eta steps slave reason sourceStamp blame currentStep text'.split(): |
| 76 |
del d[a] |
| 77 |
|
| 78 |
props = d.pop(u'logs') |
| 79 |
for name, val in props: |
| 80 |
if name == u'pytestLog': |
| 81 |
d['log'] = val |
| 82 |
props = d.pop(u'properties') |
| 83 |
for name, val, _ in props: |
| 84 |
if name == u'branch': |
| 85 |
d['branch'] = val or 'default' |
| 86 |
return d |
| 87 |
|
| 88 |
def collect_builds(d): |
| 89 |
name = str(d['basedir']) |
| 90 |
builds = d['cachedBuilds'] |
| 91 |
l = [] |
| 92 |
for build in builds: |
| 93 |
d = get_json(base + '%s/builds/%s' % (name, build)) |
| 94 |
cleanup_build(d) |
| 95 |
l.append(d) |
| 96 |
|
| 97 |
l = [x for x in l if x['branch'] in wanted and 'log' in x] |
| 98 |
d = branch_mapping(l) |
| 99 |
return [x for lst in d.values() for x in lst] |
| 100 |
|
| 101 |
|
| 102 |
def only_linux32(d): |
| 103 |
return d['own-linux-x86-32'] |
| 104 |
|
| 105 |
|
| 106 |
own_builds = get_json(base, cache=False)['own-linux-x86-32'] |
| 107 |
|
| 108 |
builds = collect_builds(own_builds) |
| 109 |
|
| 110 |
|
| 111 |
builds.sort(key=lambda x: (wanted.index(x['branch']), x['number'])) |
| 112 |
logs = [x.pop('log') for x in builds] |
| 113 |
for b, s in zip(builds, logs): |
| 114 |
b['resultset'] = len(s) |
| 115 |
import pprint |
| 116 |
pprint.pprint(builds) |
| 117 |
|
| 118 |
from difflib import Differ |
| 119 |
|
| 120 |
for x in Differ().compare(*logs): |
| 121 |
if x[0]!=' ': |
| 122 |
sys.stdout.write(x) |