Add sortable.js for sortable tables in output.
[gitstats.git] / gitstats
blob258e12364e4425ff8b69b2a31edeb0feb9e3bcae
1 #!/usr/bin/env python
2 # Copyright (c) 2007-2008 Heikki Hokkanen <hoxu@users.sf.net>
3 # GPLv2 / GPLv3
4 import subprocess
5 import datetime
6 import glob
7 import os
8 import pickle
9 import re
10 import shutil
11 import sys
12 import time
13 import zlib
15 GNUPLOT_COMMON = 'set terminal png transparent\nset size 0.5,0.5\n'
17 exectime_internal = 0.0
18 exectime_external = 0.0
19 time_start = time.time()
21 # By default, gnuplot is searched from path, but can be overridden with the
22 # environment variable "GNUPLOT"
23 gnuplot_cmd = 'gnuplot'
24 if 'GNUPLOT' in os.environ:
25 gnuplot_cmd = os.environ['GNUPLOT']
27 def getpipeoutput(cmds, quiet = False):
28 global exectime_external
29 start = time.time()
30 if not quiet:
31 print '>> ' + ' | '.join(cmds),
32 sys.stdout.flush()
33 p0 = subprocess.Popen(cmds[0], stdout = subprocess.PIPE, shell = True)
34 p = p0
35 for x in cmds[1:]:
36 p = subprocess.Popen(x, stdin = p0.stdout, stdout = subprocess.PIPE, shell = True)
37 p0 = p
38 output = p.communicate()[0]
39 end = time.time()
40 if not quiet:
41 print '\r[%.5f] >> %s' % (end - start, ' | '.join(cmds))
42 exectime_external += (end - start)
43 return output.rstrip('\n')
45 def getkeyssortedbyvalues(dict):
46 return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
48 # dict['author'] = { 'commits': 512 } - ...key(dict, 'commits')
49 def getkeyssortedbyvaluekey(d, key):
50 return map(lambda el : el[1], sorted(map(lambda el : (d[el][key], el), d.keys())))
52 class DataCollector:
53 """Manages data collection from a revision control repository."""
54 def __init__(self):
55 self.stamp_created = time.time()
56 self.cache = {}
59 # This should be the main function to extract data from the repository.
60 def collect(self, dir):
61 self.dir = dir
62 self.projectname = os.path.basename(os.path.abspath(dir))
65 # Load cacheable data
66 def loadCache(self, dir):
67 cachefile = os.path.join(dir, '.git', 'gitstats.cache')
68 if not os.path.exists(cachefile):
69 return
70 print 'Loading cache...'
71 f = open(cachefile)
72 try:
73 self.cache = pickle.loads(zlib.decompress(f.read()))
74 except:
75 # temporary hack to upgrade non-compressed caches
76 f.seek(0)
77 self.cache = pickle.load(f)
78 f.close()
81 # Produce any additional statistics from the extracted data.
82 def refine(self):
83 pass
86 # : get a dictionary of author
87 def getAuthorInfo(self, author):
88 return None
90 def getActivityByDayOfWeek(self):
91 return {}
93 def getActivityByHourOfDay(self):
94 return {}
97 # Get a list of authors
98 def getAuthors(self):
99 return []
101 def getFirstCommitDate(self):
102 return datetime.datetime.now()
104 def getLastCommitDate(self):
105 return datetime.datetime.now()
107 def getStampCreated(self):
108 return self.stamp_created
110 def getTags(self):
111 return []
113 def getTotalAuthors(self):
114 return -1
116 def getTotalCommits(self):
117 return -1
119 def getTotalFiles(self):
120 return -1
122 def getTotalLOC(self):
123 return -1
126 # Save cacheable data
127 def saveCache(self, dir):
128 print 'Saving cache...'
129 f = open(os.path.join(dir, '.git', 'gitstats.cache'), 'w')
130 #pickle.dump(self.cache, f)
131 data = zlib.compress(pickle.dumps(self.cache))
132 f.write(data)
133 f.close()
135 class GitDataCollector(DataCollector):
136 def collect(self, dir):
137 DataCollector.collect(self, dir)
139 try:
140 self.total_authors = int(getpipeoutput(['git-log', 'git-shortlog -s', 'wc -l']))
141 except:
142 self.total_authors = 0
143 #self.total_lines = int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
145 self.activity_by_hour_of_day = {} # hour -> commits
146 self.activity_by_day_of_week = {} # day -> commits
147 self.activity_by_month_of_year = {} # month [1-12] -> commits
148 self.activity_by_hour_of_week = {} # weekday -> hour -> commits
150 self.authors = {} # name -> {commits, first_commit_stamp, last_commit_stamp}
152 # author of the month
153 self.author_of_month = {} # month -> author -> commits
154 self.author_of_year = {} # year -> author -> commits
155 self.commits_by_month = {} # month -> commits
156 self.commits_by_year = {} # year -> commits
157 self.first_commit_stamp = 0
158 self.last_commit_stamp = 0
160 # tags
161 self.tags = {}
162 lines = getpipeoutput(['git-show-ref --tags']).split('\n')
163 for line in lines:
164 if len(line) == 0:
165 continue
166 (hash, tag) = line.split(' ')
168 tag = tag.replace('refs/tags/', '')
169 output = getpipeoutput(['git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash])
170 if len(output) > 0:
171 parts = output.split(' ')
172 stamp = 0
173 try:
174 stamp = int(parts[0])
175 except ValueError:
176 stamp = 0
177 self.tags[tag] = { 'stamp': stamp, 'hash' : hash, 'date' : datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d') }
179 # Collect revision statistics
180 # Outputs "<stamp> <author>"
181 lines = getpipeoutput(['git-rev-list --pretty=format:"%at %an" HEAD', 'grep -v ^commit']).split('\n')
182 for line in lines:
183 # linux-2.6 says "<unknown>" for one line O_o
184 parts = line.split(' ')
185 author = ''
186 try:
187 stamp = int(parts[0])
188 except ValueError:
189 stamp = 0
190 if len(parts) > 1:
191 author = ' '.join(parts[1:])
192 date = datetime.datetime.fromtimestamp(float(stamp))
194 # First and last commit stamp
195 if self.last_commit_stamp == 0:
196 self.last_commit_stamp = stamp
197 self.first_commit_stamp = stamp
199 # activity
200 # hour
201 hour = date.hour
202 if hour in self.activity_by_hour_of_day:
203 self.activity_by_hour_of_day[hour] += 1
204 else:
205 self.activity_by_hour_of_day[hour] = 1
207 # day of week
208 day = date.weekday()
209 if day in self.activity_by_day_of_week:
210 self.activity_by_day_of_week[day] += 1
211 else:
212 self.activity_by_day_of_week[day] = 1
214 # hour of week
215 if day not in self.activity_by_hour_of_week:
216 self.activity_by_hour_of_week[day] = {}
217 if hour not in self.activity_by_hour_of_week[day]:
218 self.activity_by_hour_of_week[day][hour] = 1
219 else:
220 self.activity_by_hour_of_week[day][hour] += 1
222 # month of year
223 month = date.month
224 if month in self.activity_by_month_of_year:
225 self.activity_by_month_of_year[month] += 1
226 else:
227 self.activity_by_month_of_year[month] = 1
229 # author stats
230 if author not in self.authors:
231 self.authors[author] = {}
232 # commits
233 if 'last_commit_stamp' not in self.authors[author]:
234 self.authors[author]['last_commit_stamp'] = stamp
235 self.authors[author]['first_commit_stamp'] = stamp
236 if 'commits' in self.authors[author]:
237 self.authors[author]['commits'] += 1
238 else:
239 self.authors[author]['commits'] = 1
241 # author of the month/year
242 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
243 if yymm in self.author_of_month:
244 if author in self.author_of_month[yymm]:
245 self.author_of_month[yymm][author] += 1
246 else:
247 self.author_of_month[yymm][author] = 1
248 else:
249 self.author_of_month[yymm] = {}
250 self.author_of_month[yymm][author] = 1
251 if yymm in self.commits_by_month:
252 self.commits_by_month[yymm] += 1
253 else:
254 self.commits_by_month[yymm] = 1
256 yy = datetime.datetime.fromtimestamp(stamp).year
257 if yy in self.author_of_year:
258 if author in self.author_of_year[yy]:
259 self.author_of_year[yy][author] += 1
260 else:
261 self.author_of_year[yy][author] = 1
262 else:
263 self.author_of_year[yy] = {}
264 self.author_of_year[yy][author] = 1
265 if yy in self.commits_by_year:
266 self.commits_by_year[yy] += 1
267 else:
268 self.commits_by_year[yy] = 1
270 # TODO Optimize this, it's the worst bottleneck
271 # outputs "<stamp> <files>" for each revision
272 self.files_by_stamp = {} # stamp -> files
273 revlines = getpipeoutput(['git-rev-list --pretty=format:"%at %T" HEAD', 'grep -v ^commit']).strip().split('\n')
274 lines = []
275 for revline in revlines:
276 time, rev = revline.split(' ')
277 #linecount = int(getpipeoutput(['git-ls-tree -r --name-only "%s"' % rev, 'wc -l']).split('\n')[0])
278 linecount = self.getFilesInCommit(rev)
279 lines.append('%d %d' % (int(time), linecount))
281 self.total_commits = len(lines)
282 for line in lines:
283 parts = line.split(' ')
284 if len(parts) != 2:
285 continue
286 (stamp, files) = parts[0:2]
287 try:
288 self.files_by_stamp[int(stamp)] = int(files)
289 except ValueError:
290 print 'Warning: failed to parse line "%s"' % line
292 # extensions
293 self.extensions = {} # extension -> files, lines
294 lines = getpipeoutput(['git-ls-files']).split('\n')
295 self.total_files = len(lines)
296 for line in lines:
297 base = os.path.basename(line)
298 if base.find('.') == -1:
299 ext = ''
300 else:
301 ext = base[(base.rfind('.') + 1):]
303 if ext not in self.extensions:
304 self.extensions[ext] = {'files': 0, 'lines': 0}
306 self.extensions[ext]['files'] += 1
307 try:
308 # Escaping could probably be improved here
309 self.extensions[ext]['lines'] += int(getpipeoutput(['wc -l "%s"' % line]).split()[0])
310 except:
311 print 'Warning: Could not count lines for file "%s"' % line
313 # line statistics
314 # outputs:
315 # N files changed, N insertions (+), N deletions(-)
316 # <stamp> <author>
317 self.changes_by_date = {} # stamp -> { files, ins, del }
318 lines = getpipeoutput(['git-log --shortstat --pretty=format:"%at %an"']).split('\n')
319 lines.reverse()
320 files = 0; inserted = 0; deleted = 0; total_lines = 0
321 for line in lines:
322 if len(line) == 0:
323 continue
325 # <stamp> <author>
326 if line.find('files changed,') == -1:
327 pos = line.find(' ')
328 if pos != -1:
329 try:
330 (stamp, author) = (int(line[:pos]), line[pos+1:])
331 self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted, 'lines': total_lines }
332 except ValueError:
333 print 'Warning: unexpected line "%s"' % line
334 else:
335 print 'Warning: unexpected line "%s"' % line
336 else:
337 numbers = re.findall('\d+', line)
338 if len(numbers) == 3:
339 (files, inserted, deleted) = map(lambda el : int(el), numbers)
340 total_lines += inserted
341 total_lines -= deleted
342 else:
343 print 'Warning: failed to handle line "%s"' % line
344 (files, inserted, deleted) = (0, 0, 0)
345 #self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
346 self.total_lines = total_lines
348 def refine(self):
349 # authors
350 # name -> {place_by_commits, commits_frac, date_first, date_last, timedelta}
351 authors_by_commits = getkeyssortedbyvaluekey(self.authors, 'commits')
352 authors_by_commits.reverse() # most first
353 for i, name in enumerate(authors_by_commits):
354 self.authors[name]['place_by_commits'] = i + 1
356 for name in self.authors.keys():
357 a = self.authors[name]
358 a['commits_frac'] = (100 * float(a['commits'])) / self.getTotalCommits()
359 date_first = datetime.datetime.fromtimestamp(a['first_commit_stamp'])
360 date_last = datetime.datetime.fromtimestamp(a['last_commit_stamp'])
361 delta = date_last - date_first
362 a['date_first'] = date_first.strftime('%Y-%m-%d')
363 a['date_last'] = date_last.strftime('%Y-%m-%d')
364 a['timedelta'] = delta
366 def getActivityByDayOfWeek(self):
367 return self.activity_by_day_of_week
369 def getActivityByHourOfDay(self):
370 return self.activity_by_hour_of_day
372 def getAuthorInfo(self, author):
373 return self.authors[author]
375 def getAuthors(self):
376 return self.authors.keys()
378 def getFilesInCommit(self, rev):
379 try:
380 res = self.cache['files_in_tree'][rev]
381 except:
382 res = int(getpipeoutput(['git-ls-tree -r --name-only "%s"' % rev, 'wc -l']).split('\n')[0])
383 if 'files_in_tree' not in self.cache:
384 self.cache['files_in_tree'] = {}
385 self.cache['files_in_tree'][rev] = res
387 return res
389 def getFirstCommitDate(self):
390 return datetime.datetime.fromtimestamp(self.first_commit_stamp)
392 def getLastCommitDate(self):
393 return datetime.datetime.fromtimestamp(self.last_commit_stamp)
395 def getTags(self):
396 lines = getpipeoutput(['git-show-ref --tags', 'cut -d/ -f3'])
397 return lines.split('\n')
399 def getTagDate(self, tag):
400 return self.revToDate('tags/' + tag)
402 def getTotalAuthors(self):
403 return self.total_authors
405 def getTotalCommits(self):
406 return self.total_commits
408 def getTotalFiles(self):
409 return self.total_files
411 def getTotalLOC(self):
412 return self.total_lines
414 def revToDate(self, rev):
415 stamp = int(getpipeoutput(['git-log --pretty=format:%%at "%s" -n 1' % rev]))
416 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
418 class ReportCreator:
419 """Creates the actual report based on given data."""
420 def __init__(self):
421 pass
423 def create(self, data, path):
424 self.data = data
425 self.path = path
427 def html_linkify(text):
428 return text.lower().replace(' ', '_')
430 def html_header(level, text):
431 name = html_linkify(text)
432 return '\n<h%d><a href="#%s" name="%s">%s</a></h%d>\n\n' % (level, name, name, text, level)
434 class HTMLReportCreator(ReportCreator):
435 def create(self, data, path):
436 ReportCreator.create(self, data, path)
437 self.title = data.projectname
439 # copy the CSS if it does not exist
440 if not os.path.exists(path + '/gitstats.css'):
441 basedir = os.path.dirname(os.path.abspath(__file__))
442 shutil.copyfile(basedir + '/gitstats.css', path + '/gitstats.css')
444 f = open(path + "/index.html", 'w')
445 format = '%Y-%m-%d %H:%m:%S'
446 self.printHeader(f)
448 f.write('<h1>GitStats - %s</h1>' % data.projectname)
450 self.printNav(f)
452 f.write('<dl>');
453 f.write('<dt>Project name</dt><dd>%s</dd>' % (data.projectname))
454 f.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime.datetime.now().strftime(format), time.time() - data.getStampCreated()));
455 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
456 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
457 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
458 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
459 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
460 f.write('</dl>');
462 f.write('</body>\n</html>');
463 f.close()
466 # Activity
467 f = open(path + '/activity.html', 'w')
468 self.printHeader(f)
469 f.write('<h1>Activity</h1>')
470 self.printNav(f)
472 #f.write('<h2>Last 30 days</h2>')
474 #f.write('<h2>Last 12 months</h2>')
476 # Hour of Day
477 f.write(html_header(2, 'Hour of Day'))
478 hour_of_day = data.getActivityByHourOfDay()
479 f.write('<table><tr><th>Hour</th>')
480 for i in range(1, 25):
481 f.write('<th>%d</th>' % i)
482 f.write('</tr>\n<tr><th>Commits</th>')
483 fp = open(path + '/hour_of_day.dat', 'w')
484 for i in range(0, 24):
485 if i in hour_of_day:
486 f.write('<td>%d</td>' % hour_of_day[i])
487 fp.write('%d %d\n' % (i, hour_of_day[i]))
488 else:
489 f.write('<td>0</td>')
490 fp.write('%d 0\n' % i)
491 fp.close()
492 f.write('</tr>\n<tr><th>%</th>')
493 totalcommits = data.getTotalCommits()
494 for i in range(0, 24):
495 if i in hour_of_day:
496 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
497 else:
498 f.write('<td>0.00</td>')
499 f.write('</tr></table>')
500 f.write('<img src="hour_of_day.png" alt="Hour of Day" />')
501 fg = open(path + '/hour_of_day.dat', 'w')
502 for i in range(0, 24):
503 if i in hour_of_day:
504 fg.write('%d %d\n' % (i + 1, hour_of_day[i]))
505 else:
506 fg.write('%d 0\n' % (i + 1))
507 fg.close()
509 # Day of Week
510 f.write(html_header(2, 'Day of Week'))
511 day_of_week = data.getActivityByDayOfWeek()
512 f.write('<div class="vtable"><table>')
513 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
514 fp = open(path + '/day_of_week.dat', 'w')
515 for d in range(0, 7):
516 commits = 0
517 if d in day_of_week:
518 commits = day_of_week[d]
519 fp.write('%d %d\n' % (d + 1, commits))
520 f.write('<tr>')
521 f.write('<th>%d</th>' % (d + 1))
522 if d in day_of_week:
523 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
524 else:
525 f.write('<td>0</td>')
526 f.write('</tr>')
527 f.write('</table></div>')
528 f.write('<img src="day_of_week.png" alt="Day of Week" />')
529 fp.close()
531 # Hour of Week
532 f.write(html_header(2, 'Hour of Week'))
533 f.write('<table>')
535 f.write('<tr><th>Weekday</th>')
536 for hour in range(0, 24):
537 f.write('<th>%d</th>' % (hour + 1))
538 f.write('</tr>')
540 for weekday in range(0, 7):
541 f.write('<tr><th>%d</th>' % (weekday + 1))
542 for hour in range(0, 24):
543 try:
544 commits = data.activity_by_hour_of_week[weekday][hour]
545 except KeyError:
546 commits = 0
547 if commits != 0:
548 f.write('<td>%d</td>' % commits)
549 else:
550 f.write('<td></td>')
551 f.write('</tr>')
553 f.write('</table>')
555 # Month of Year
556 f.write(html_header(2, 'Month of Year'))
557 f.write('<div class="vtable"><table>')
558 f.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
559 fp = open (path + '/month_of_year.dat', 'w')
560 for mm in range(1, 13):
561 commits = 0
562 if mm in data.activity_by_month_of_year:
563 commits = data.activity_by_month_of_year[mm]
564 f.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm, commits, (100.0 * commits) / data.getTotalCommits()))
565 fp.write('%d %d\n' % (mm, commits))
566 fp.close()
567 f.write('</table></div>')
568 f.write('<img src="month_of_year.png" alt="Month of Year" />')
570 # Commits by year/month
571 f.write(html_header(2, 'Commits by year/month'))
572 f.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
573 for yymm in reversed(sorted(data.commits_by_month.keys())):
574 f.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm, data.commits_by_month[yymm]))
575 f.write('</table></div>')
576 f.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
577 fg = open(path + '/commits_by_year_month.dat', 'w')
578 for yymm in sorted(data.commits_by_month.keys()):
579 fg.write('%s %s\n' % (yymm, data.commits_by_month[yymm]))
580 fg.close()
582 # Commits by year
583 f.write(html_header(2, 'Commits by Year'))
584 f.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
585 for yy in reversed(sorted(data.commits_by_year.keys())):
586 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td></tr>' % (yy, data.commits_by_year[yy], (100.0 * data.commits_by_year[yy]) / data.getTotalCommits()))
587 f.write('</table></div>')
588 f.write('<img src="commits_by_year.png" alt="Commits by Year" />')
589 fg = open(path + '/commits_by_year.dat', 'w')
590 for yy in sorted(data.commits_by_year.keys()):
591 fg.write('%d %d\n' % (yy, data.commits_by_year[yy]))
592 fg.close()
594 f.write('</body></html>')
595 f.close()
598 # Authors
599 f = open(path + '/authors.html', 'w')
600 self.printHeader(f)
602 f.write('<h1>Authors</h1>')
603 self.printNav(f)
605 # Authors :: List of authors
606 f.write(html_header(2, 'List of Authors'))
608 f.write('<table class="authors">')
609 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th><th>Age</th><th># by commits</th></tr>')
610 for author in sorted(data.getAuthors()):
611 info = data.getAuthorInfo(author)
612 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td></tr>' % (author, info['commits'], info['commits_frac'], info['date_first'], info['date_last'], info['timedelta'], info['place_by_commits']))
613 f.write('</table>')
615 # Authors :: Author of Month
616 f.write(html_header(2, 'Author of Month'))
617 f.write('<table>')
618 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th><th>Next top 5</th></tr>')
619 for yymm in reversed(sorted(data.author_of_month.keys())):
620 authordict = data.author_of_month[yymm]
621 authors = getkeyssortedbyvalues(authordict)
622 authors.reverse()
623 commits = data.author_of_month[yymm][authors[0]]
624 next = ', '.join(authors[1:5])
625 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yymm, authors[0], commits, (100 * commits) / data.commits_by_month[yymm], data.commits_by_month[yymm], next))
627 f.write('</table>')
629 f.write(html_header(2, 'Author of Year'))
630 f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th><th>Next top 5</th></tr>')
631 for yy in reversed(sorted(data.author_of_year.keys())):
632 authordict = data.author_of_year[yy]
633 authors = getkeyssortedbyvalues(authordict)
634 authors.reverse()
635 commits = data.author_of_year[yy][authors[0]]
636 next = ', '.join(authors[1:5])
637 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td><td>%s</td></tr>' % (yy, authors[0], commits, (100 * commits) / data.commits_by_year[yy], data.commits_by_year[yy], next))
638 f.write('</table>')
640 f.write('</body></html>')
641 f.close()
644 # Files
645 f = open(path + '/files.html', 'w')
646 self.printHeader(f)
647 f.write('<h1>Files</h1>')
648 self.printNav(f)
650 f.write('<dl>\n')
651 f.write('<dt>Total files</dt><dd>%d</dd>' % data.getTotalFiles())
652 f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
653 f.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data.getTotalLOC()) / data.getTotalFiles()))
654 f.write('</dl>\n')
656 # Files :: File count by date
657 f.write(html_header(2, 'File count by date'))
659 fg = open(path + '/files_by_date.dat', 'w')
660 for stamp in sorted(data.files_by_stamp.keys()):
661 fg.write('%s %d\n' % (datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d'), data.files_by_stamp[stamp]))
662 fg.close()
664 f.write('<img src="files_by_date.png" alt="Files by Date" />')
666 #f.write('<h2>Average file size by date</h2>')
668 # Files :: Extensions
669 f.write(html_header(2, 'Extensions'))
670 f.write('<table><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
671 for ext in sorted(data.extensions.keys()):
672 files = data.extensions[ext]['files']
673 lines = data.extensions[ext]['lines']
674 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%d (%.2f%%)</td><td>%d</td></tr>' % (ext, files, (100.0 * files) / data.getTotalFiles(), lines, (100.0 * lines) / data.getTotalLOC(), lines / files))
675 f.write('</table>')
677 f.write('</body></html>')
678 f.close()
681 # Lines
682 f = open(path + '/lines.html', 'w')
683 self.printHeader(f)
684 f.write('<h1>Lines</h1>')
685 self.printNav(f)
687 f.write('<dl>\n')
688 f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
689 f.write('</dl>\n')
691 f.write(html_header(2, 'Lines of Code'))
692 f.write('<img src="lines_of_code.png" />')
694 fg = open(path + '/lines_of_code.dat', 'w')
695 for stamp in sorted(data.changes_by_date.keys()):
696 fg.write('%d %d\n' % (stamp, data.changes_by_date[stamp]['lines']))
697 fg.close()
699 f.write('</body></html>')
700 f.close()
703 # tags.html
704 f = open(path + '/tags.html', 'w')
705 self.printHeader(f)
706 f.write('<h1>Tags</h1>')
707 self.printNav(f)
709 f.write('<dl>')
710 f.write('<dt>Total tags</dt><dd>%d</dd>' % len(data.tags))
711 if len(data.tags) > 0:
712 f.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data.getTotalCommits() / len(data.tags)))
713 f.write('</dl>')
715 f.write('<table>')
716 f.write('<tr><th>Name</th><th>Date</th></tr>')
717 # sort the tags by date desc
718 tags_sorted_by_date_desc = map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), data.tags.items()))))
719 for tag in tags_sorted_by_date_desc:
720 f.write('<tr><td>%s</td><td>%s</td></tr>' % (tag, data.tags[tag]['date']))
721 f.write('</table>')
723 f.write('</body></html>')
724 f.close()
726 self.createGraphs(path)
728 def createGraphs(self, path):
729 print 'Generating graphs...'
731 # hour of day
732 f = open(path + '/hour_of_day.plot', 'w')
733 f.write(GNUPLOT_COMMON)
734 f.write(
736 set output 'hour_of_day.png'
737 unset key
738 set xrange [0.5:24.5]
739 set xtics 4
740 set ylabel "Commits"
741 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
742 """)
743 f.close()
745 # day of week
746 f = open(path + '/day_of_week.plot', 'w')
747 f.write(GNUPLOT_COMMON)
748 f.write(
750 set output 'day_of_week.png'
751 unset key
752 set xrange [0.5:7.5]
753 set xtics 1
754 set ylabel "Commits"
755 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
756 """)
757 f.close()
759 # Month of Year
760 f = open(path + '/month_of_year.plot', 'w')
761 f.write(GNUPLOT_COMMON)
762 f.write(
764 set output 'month_of_year.png'
765 unset key
766 set xrange [0.5:12.5]
767 set xtics 1
768 set ylabel "Commits"
769 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
770 """)
771 f.close()
773 # commits_by_year_month
774 f = open(path + '/commits_by_year_month.plot', 'w')
775 f.write(GNUPLOT_COMMON)
776 f.write(
778 set output 'commits_by_year_month.png'
779 unset key
780 set xdata time
781 set timefmt "%Y-%m"
782 set format x "%Y-%m"
783 set xtics rotate by 90 15768000
784 set bmargin 5
785 set ylabel "Commits"
786 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
787 """)
788 f.close()
790 # commits_by_year
791 f = open(path + '/commits_by_year.plot', 'w')
792 f.write(GNUPLOT_COMMON)
793 f.write(
795 set output 'commits_by_year.png'
796 unset key
797 set xtics 1
798 set ylabel "Commits"
799 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
800 """)
801 f.close()
803 # Files by date
804 f = open(path + '/files_by_date.plot', 'w')
805 f.write(GNUPLOT_COMMON)
806 f.write(
808 set output 'files_by_date.png'
809 unset key
810 set xdata time
811 set timefmt "%Y-%m-%d"
812 set format x "%Y-%m-%d"
813 set ylabel "Files"
814 set xtics rotate by 90
815 set bmargin 6
816 plot 'files_by_date.dat' using 1:2 smooth csplines
817 """)
818 f.close()
820 # Lines of Code
821 f = open(path + '/lines_of_code.plot', 'w')
822 f.write(GNUPLOT_COMMON)
823 f.write(
825 set output 'lines_of_code.png'
826 unset key
827 set xdata time
828 set timefmt "%s"
829 set format x "%Y-%m-%d"
830 set ylabel "Lines"
831 set xtics rotate by 90
832 set bmargin 6
833 plot 'lines_of_code.dat' using 1:2 w lines
834 """)
835 f.close()
837 os.chdir(path)
838 files = glob.glob(path + '/*.plot')
839 for f in files:
840 out = getpipeoutput([gnuplot_cmd + ' "%s"' % f])
841 if len(out) > 0:
842 print out
844 def printHeader(self, f, title = ''):
845 f.write(
846 """<?xml version="1.0" encoding="UTF-8"?>
847 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
848 <html xmlns="http://www.w3.org/1999/xhtml">
849 <head>
850 <title>GitStats - %s</title>
851 <link rel="stylesheet" href="gitstats.css" type="text/css" />
852 <meta name="generator" content="GitStats" />
853 </head>
854 <body>
855 """ % self.title)
857 def printNav(self, f):
858 f.write("""
859 <div class="nav">
860 <ul>
861 <li><a href="index.html">General</a></li>
862 <li><a href="activity.html">Activity</a></li>
863 <li><a href="authors.html">Authors</a></li>
864 <li><a href="files.html">Files</a></li>
865 <li><a href="lines.html">Lines</a></li>
866 <li><a href="tags.html">Tags</a></li>
867 </ul>
868 </div>
869 """)
872 usage = """
873 Usage: gitstats [options] <gitpath> <outputpath>
875 Options:
878 if len(sys.argv) < 3:
879 print usage
880 sys.exit(0)
882 gitpath = sys.argv[1]
883 outputpath = os.path.abspath(sys.argv[2])
884 rundir = os.getcwd()
886 try:
887 os.makedirs(outputpath)
888 except OSError:
889 pass
890 if not os.path.isdir(outputpath):
891 print 'FATAL: Output path is not a directory or does not exist'
892 sys.exit(1)
894 print 'Git path: %s' % gitpath
895 print 'Output path: %s' % outputpath
897 os.chdir(gitpath)
899 print 'Collecting data...'
900 data = GitDataCollector()
901 data.loadCache(gitpath)
902 data.collect(gitpath)
903 print 'Refining data...'
904 data.saveCache(gitpath)
905 data.refine()
907 os.chdir(rundir)
909 print 'Generating report...'
910 report = HTMLReportCreator()
911 report.create(data, outputpath)
913 time_end = time.time()
914 exectime_internal = time_end - time_start
915 print 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal, exectime_external, (100.0 * exectime_external) / exectime_internal)