]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd_popcon2stats
b0adecdfe4c60fae4f1b1fced3e2d4b1eb375197
[neurodebian.git] / tools / nd_popcon2stats
1 #!/usr/bin/python
2 # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3 # vi: set ft=python sts=4 ts=4 sw=4 et:
4 #
5 import fileinput
6 import sys
7 import time
8 from datetime import datetime
9 import re
10 import json
11 import operator
12
13
14 releases = {
15     'etch': 'Debian GNU/Linux 4.0 (etch)',
16     'lenny': 'Debian GNU/Linux 5.0 (lenny)',
17     'squeeze': 'Debian GNU/Linux 6.0 (squeeze)',
18     'wheezy': 'Debian testing (wheezy)',
19     'sid': 'Debian unstable (sid)',
20     'hardy': 'Ubuntu 08.04 LTS "Hardy Heron" (hardy)',
21     'jaunty': 'Ubuntu 09.04 "Jaunty Jackalope" (jaunty)',
22     'karmic': 'Ubuntu 09.10 "Karmic Koala" (karmic)',
23     'lucid': 'Ubuntu 10.04 LTS "Lucid Lynx" (lucid)',
24     'maverick': 'Ubuntu 10.10 "Maverick Meerkat" (maverick)',
25     'natty': 'Ubuntu 11.04 "Natty Narwhal" (natty)',
26     'oneiric': 'Ubuntu 11.10 "Oneiric Ocelot" (oneiric)',
27     'precise': 'Ubuntu 12.04 LTS "Precise Pangolin" (precise)',
28     'quantal': 'Ubuntu 12.10 "Quantal Quetzal" (quantal)',
29     'raring': 'Ubuntu 13.04 "Raring Ringtail" (raring)',
30     'saucy': 'Ubuntu 13.10 "Saucy Salamander" (saucy)',
31 }
32
33 def error(msg):
34     sys.stderr.write('E: %s\n' % msg)
35
36 def info(msg):
37     # print nothing ATM
38     # sys.stderr.write("I: %s\n" % msg)
39     pass
40
41 file_regex = re.compile('.*popcon-(\d{4}-\d{1,2}-\d{1,2})(|.gz)')
42
43 def read_popcon_stats(filename, read_packages=True):
44     info("Reading %s" % filename)
45     entry = dict(submissions = None,
46                  package = {},
47                  release = {},
48                  architecture = {})
49
50     for line in fileinput.FileInput(filename, openhook=fileinput.hook_compressed):
51         key, values = [x.strip().lower() for x in line.split(':', 1)]
52         if key == 'package':          # most probable
53             if not read_packages:
54                 break
55             try:
56                 pkg, vote, old, recent, nofiles = values.split()
57             except ValueError:
58                 raise ValueError("Failed to split %s" % values)
59             entry[key][pkg] = tuple(int(x) for x in (vote, old, recent, nofiles))
60         elif key in ('release', 'architecture'):
61             kvalue, value = values.split()
62             entry[key][kvalue] = int(value)
63         elif key == 'submissions':
64             entry[key] = int(values)
65         else:
66             raise ValueError("Do not know how to handle line" % line)
67     return entry
68
69 if __name__ == '__main__':
70     data = {}
71
72     popcon_versions = {}
73     timestamps = set()
74
75     for f in sys.argv[1:]:
76         file_reg = file_regex.match(f)
77         if not file_reg:
78             error("Failed to recognize filename %s" % f)
79             continue
80
81         date = time.strptime(file_reg.groups()[0], '%Y-%m-%d')
82         entry = read_popcon_stats(f, read_packages=False)
83
84         date_int = int(time.mktime(date)*1000)
85         # Let's coarsen a bit -- to a week which makes sense anyways
86         # since popcon submissions are spread over a week for balanced
87         # load
88         coarsen_days = 7
89         coarsen = coarsen_days*24*3600*1000
90         # coarsen and place marker at the end of the duration
91         # but not later than today
92         date_int = min((date_int//coarsen + 1)*coarsen,
93                        time.time()*1000)
94         for version, count in entry['release'].iteritems():
95             if not version in popcon_versions:
96                 popcon_versions[version] = {}
97             popcon_ = popcon_versions[version]
98             popcon_[date_int] = count + popcon_.get(date_int, 0)
99             timestamps.add(date_int)
100
101     versions = sorted([x for x in popcon_versions.keys() if not 'ubuntu' in x]) + \
102                sorted([x for x in popcon_versions.keys() if     'ubuntu' in x])
103
104     # we need to make sure that for every date we have an entry for
105     # every version, otherwise d3 pukes because of ... d3.v2.js:expand
106     export = [{'key': k,
107                'values': [[date, popcon_versions[k].get(date, 0)/coarsen_days]
108                           for date in sorted(list(timestamps))]}
109               for k in versions]
110     print json.dumps(export)
111