1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # graph-svn-dav.py by Brian W. Fitzpatrick <fitz@red-bean.com> |
---|
4 | # |
---|
5 | # This was originally a quick hack to make a pretty picture of svn DAV servers. |
---|
6 | # |
---|
7 | # I've dropped it in Subversion's repository at the request of Karl Fogel. |
---|
8 | # |
---|
9 | # Be warned this this script has many dependencies that don't ship with Python. |
---|
10 | |
---|
11 | import sys |
---|
12 | import os |
---|
13 | import fileinput |
---|
14 | import datetime |
---|
15 | import time |
---|
16 | import datetime |
---|
17 | from matplotlib import dates |
---|
18 | import matplotlib |
---|
19 | matplotlib.use('Agg') |
---|
20 | from matplotlib import pylab |
---|
21 | import Image |
---|
22 | |
---|
23 | OUTPUT_FILE = '../../www/images/svn-dav-securityspace-survey.png' |
---|
24 | OUTPUT_IMAGE_WIDTH = 800 |
---|
25 | |
---|
26 | STATS = [ |
---|
27 | ('1/1/2003', 70), |
---|
28 | ('2/1/2003', 158), |
---|
29 | ('3/1/2003', 222), |
---|
30 | ('4/1/2003', 250), |
---|
31 | ('5/1/2003', 308), |
---|
32 | ('6/1/2003', 369), |
---|
33 | ('7/1/2003', 448), |
---|
34 | ('8/1/2003', 522), |
---|
35 | ('9/1/2003', 665), |
---|
36 | ('10/1/2003', 782), |
---|
37 | ('11/1/2003', 969), |
---|
38 | ('12/1/2003', 1009), |
---|
39 | ('1/1/2004', 1162), |
---|
40 | ('2/1/2004', 1307), |
---|
41 | ('3/1/2004', 1424), |
---|
42 | ('4/1/2004', 1792), |
---|
43 | ('5/1/2004', 2113), |
---|
44 | ('6/1/2004', 2502), |
---|
45 | ('7/1/2004', 2941), |
---|
46 | ('8/1/2004', 3863), |
---|
47 | ('9/1/2004', 4174), |
---|
48 | ('10/1/2004', 4187), |
---|
49 | ('11/1/2004', 4783), |
---|
50 | ('12/1/2004', 4995), |
---|
51 | ('1/1/2005', 5565), |
---|
52 | ('2/1/2005', 6505), |
---|
53 | ('3/1/2005', 7897), |
---|
54 | ('4/1/2005', 8751), |
---|
55 | ('5/1/2005', 9793), |
---|
56 | ('6/1/2005', 11534), |
---|
57 | ('7/1/2005', 12808), |
---|
58 | ('8/1/2005', 13545), |
---|
59 | ('9/1/2005', 15233), |
---|
60 | ('10/1/2005', 17588), |
---|
61 | ('11/1/2005', 18893), |
---|
62 | ('12/1/2005', 20278), |
---|
63 | ('1/1/2006', 21084), |
---|
64 | ('2/1/2006', 23861), |
---|
65 | ('3/1/2006', 26540), |
---|
66 | ('4/1/2006', 29396), |
---|
67 | ('5/1/2006', 33001), |
---|
68 | ('6/1/2006', 35082), |
---|
69 | ('7/1/2006', 38939), |
---|
70 | ('8/1/2006', 40672), |
---|
71 | ('9/1/2006', 46525), |
---|
72 | ('10/1/2006', 54247), |
---|
73 | ('11/1/2006', 63145), |
---|
74 | ('12/1/2006', 68988), |
---|
75 | ('1/1/2007', 77027), |
---|
76 | ('2/1/2007', 84813), |
---|
77 | ('3/1/2007', 95679), |
---|
78 | ('4/1/2007', 103852), |
---|
79 | ('5/1/2007', 117267), |
---|
80 | ('6/1/2007', 133665), |
---|
81 | ('7/1/2007', 137575), |
---|
82 | ('8/1/2007', 155426), |
---|
83 | ('9/1/2007', 159055), |
---|
84 | ('10/1/2007', 169939), |
---|
85 | ('11/1/2007', 180831), |
---|
86 | ('12/1/2007', 187093), |
---|
87 | ('1/1/2008', 199432), |
---|
88 | ('2/1/2008', 221547), |
---|
89 | ('3/1/2008', 240794), |
---|
90 | ('4/1/2008', 255520), |
---|
91 | ('5/1/2008', 269478), |
---|
92 | ('6/1/2008', 286614), |
---|
93 | ('7/1/2008', 294579), |
---|
94 | ('8/1/2008', 307923), |
---|
95 | ('9/1/2008', 254757), |
---|
96 | ('10/1/2008', 268081), |
---|
97 | ('11/1/2008', 299071), |
---|
98 | ('12/1/2008', 330884), |
---|
99 | ('1/1/2009', 369719), |
---|
100 | ('2/1/2009', 378434), |
---|
101 | ] |
---|
102 | |
---|
103 | |
---|
104 | def get_date(raw_date): |
---|
105 | month, day, year = map(int, raw_date.split('/')) |
---|
106 | return datetime.datetime(year, month, day) |
---|
107 | |
---|
108 | |
---|
109 | def get_ordinal_date(date): |
---|
110 | # This is the only way I can get matplotlib to do the dates right. |
---|
111 | return int(dates.date2num(get_date(date))) |
---|
112 | |
---|
113 | |
---|
114 | def load_stats(): |
---|
115 | dates = [get_ordinal_date(date) for date, value in STATS] |
---|
116 | counts = [x[1] for x in STATS] |
---|
117 | |
---|
118 | return dates, counts |
---|
119 | |
---|
120 | |
---|
121 | def draw_graph(dates, counts): |
---|
122 | ########################################################### |
---|
123 | # Drawing takes place here. |
---|
124 | pylab.figure(1) |
---|
125 | |
---|
126 | ax = pylab.subplot(111) |
---|
127 | pylab.plot_date(dates, counts, |
---|
128 | color='r', linestyle='-', marker='o', markersize=3) |
---|
129 | |
---|
130 | ax.xaxis.set_major_formatter( pylab.DateFormatter('%Y') ) |
---|
131 | ax.xaxis.set_major_locator( pylab.YearLocator() ) |
---|
132 | ax.xaxis.set_minor_locator( pylab.MonthLocator() ) |
---|
133 | ax.set_xlim( (dates[0] - 92, dates[len(dates) - 1] + 92) ) |
---|
134 | |
---|
135 | ax.yaxis.set_major_formatter( pylab.FormatStrFormatter('%d') ) |
---|
136 | |
---|
137 | pylab.ylabel('Total # of Public DAV Servers') |
---|
138 | |
---|
139 | lastdate = datetime.datetime.fromordinal(dates[len(dates) - 1]).strftime("%B %Y") |
---|
140 | pylab.xlabel("Data as of " + lastdate) |
---|
141 | pylab.title('Security Space Survey of\nPublic Subversion DAV Servers') |
---|
142 | # End drawing |
---|
143 | ########################################################### |
---|
144 | png = open(OUTPUT_FILE, 'w') |
---|
145 | pylab.savefig(png) |
---|
146 | png.close() |
---|
147 | os.rename(OUTPUT_FILE, OUTPUT_FILE + ".tmp.png") |
---|
148 | try: |
---|
149 | im = Image.open(OUTPUT_FILE + ".tmp.png", 'r') |
---|
150 | (width, height) = im.size |
---|
151 | print("Original size: %d x %d pixels" % (width, height)) |
---|
152 | scale = float(OUTPUT_IMAGE_WIDTH) / float(width) |
---|
153 | width = OUTPUT_IMAGE_WIDTH |
---|
154 | height = int(float(height) * scale) |
---|
155 | print("Final size: %d x %d pixels" % (width, height)) |
---|
156 | im = im.resize((width, height), Image.ANTIALIAS) |
---|
157 | im.save(OUTPUT_FILE, im.format) |
---|
158 | os.unlink(OUTPUT_FILE + ".tmp.png") |
---|
159 | except Exception, e: |
---|
160 | sys.stderr.write("Error attempting to resize the graphic: %s\n" % (str(e))) |
---|
161 | os.rename(OUTPUT_FILE + ".tmp.png", OUTPUT_FILE) |
---|
162 | raise |
---|
163 | pylab.close() |
---|
164 | |
---|
165 | |
---|
166 | if __name__ == '__main__': |
---|
167 | dates, counts = load_stats() |
---|
168 | draw_graph(dates, counts) |
---|
169 | print("Don't forget to update ../../www/svn-dav-securityspace-survey.html!") |
---|