1
#!/usr/bin/env python
2
"""A collection of git info aggregators for graph commands."""
3
4
import time
5
import subprocess
6
from collections import defaultdict
7
8
class TimeHisto(object):
9
10
    def __init__(self):
11
        self.h = defaultdict(lambda: 0)
12
13
    def add_logs(self, directory=None, log_args=['HEAD']):
14
        args=['git']
15
        if directory:
16
            args.append('--git-dir=' + directory)
17
        args.extend(['log', '--pretty=format:%at'])
18
        args.extend(log_args)
19
        sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
20
21
        for l in sub.stdout:
22
            self.h[time.strftime("%w %H", time.localtime(float(l.strip())))] += 1
23
24
    def dump(self):
25
        for h in range(24):
26
            for d in range(7):
27
                sys.stderr.write("%02d %d - %s\n"
28
                                 % (h, d, self.h["%d %02d" % (d, h)]))
29
30
    def to_gchart(self):
31
        from pygooglechart import ScatterChart
32
        chart = ScatterChart(800, 300, x_range=(-1, 24), y_range=(-1, 7))
33
34
        chart.add_data([(h % 24) for h in range(24 * 8)])
35
36
        d=[]
37
        for i in range(8):
38
            d.extend([i] * 24)
39
        chart.add_data(d)
40
41
        day_names = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
42
        days = (0, 6, 5, 4, 3, 2, 1)
43
44
        sizes=[]
45
        for d in days:
46
            sizes.extend([self.h["%d %02d" % (d, h)] for h in range(24)])
47
        sizes.extend([0] * 24)
48
        chart.add_data(sizes)
49
50
        chart.set_axis_labels('x', [''] + [str(h) for h  in range(24)] + [''])
51
        chart.set_axis_labels('y', [''] + [day_names[n] for n in days] + [''])
52
53
        chart.add_marker(1, 1.0, 'o', '333333', 25)
54
        return chart.get_url() + '&chds=-1,24,-1,7,0,20'
55
56
        for l in sub.stdout:
57
            self.h[time.strftime("%w %H", time.localtime(float(l.strip())))] += 1
58
59
    def dump(self):
60
        for h in range(24):
61
            for d in range(7):
62
                sys.stderr.write("%02d %d - %s\n"
63
                                 % (h, d, self.h["%d %02d" % (d, h)]))
64
65
    def to_gchart(self):
66
        from pygooglechart import ScatterChart
67
        chart = ScatterChart(800, 300, x_range=(-1, 24), y_range=(-1, 7))
68
69
        chart.add_data([(h % 24) for h in range(24 * 8)])
70
71
        d=[]
72
        for i in range(8):
73
            d.extend([i] * 24)
74
        chart.add_data(d)
75
76
        day_names = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
77
        days = (0, 6, 5, 4, 3, 2, 1)
78
79
        sizes=[]
80
        for d in days:
81
            sizes.extend([self.h["%d %02d" % (d, h)] for h in range(24)])
82
        sizes.extend([0] * 24)
83
        chart.add_data(sizes)
84
85
        chart.set_axis_labels('x', [''] + [str(h) for h  in range(24)] + [''])
86
        chart.set_axis_labels('y', [''] + [day_names[n] for n in days] + [''])
87
88
        chart.add_marker(1, 1.0, 'o', '333333', 25)
89
        return chart.get_url() + '&chds=-1,24,-1,7,0,20'
90
91
class Contributors(object):
92
93
    width = 400
94
    height = 300
95
    max_entries = 6
96
97
    def __init__(self, log_args=['HEAD']):
98
        self.data=[]
99
        args = ['git', 'shortlog', '-sn'] + log_args
100
        sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
101
102
        for l in sub.stdout:
103
            commits, name = [x.strip() for x in l.split("\t")]
104
            commits = int(commits)
105
            self.data.append((name, commits))
106
107
    def dump(self):
108
        sys.stderr.write(repr(self.data) + "\n")
109
110
    def mk_chart(self):
111
        from pygooglechart import PieChart2D
112
        chart = PieChart2D(self.width, self.height)
113
114
        data = self.data[:self.max_entries]
115
        if len(self.data) > len(data):
116
            remainder = sum(d[1] for d in self.data[self.max_entries:])
117
            data.append(('Other', remainder))
118
119
        chart.add_data([d[1] for d in data])
120
        chart.set_pie_labels([d[0].split()[0] for d in data])
121
        return chart
122
123
    def to_gchart(self):
124
        return self.mk_chart().get_url()
125
126
def open_chart(chartish):
127
    subprocess.check_call(['open', chartish.to_gchart()])