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