Optimize total lines of code.
[gitstats.git] / gitstats
bloba575ae37517fb1adf8f7220bb5717aa5ac0ba2d7
1 #!/usr/bin/python
2 # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
3 # GPLv2
4 import commands
5 import datetime
6 import glob
7 import os
8 import re
9 import shutil
10 import sys
11 import time
13 GNUPLOT_COMMON = 'set terminal png transparent\nset size 0.5,0.5\n'
15 def getoutput(cmd, quiet = False):
16 if not quiet:
17 print '>> %s' % cmd
18 output = commands.getoutput(cmd)
19 return output
21 def getkeyssortedbyvalues(dict):
22 return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
24 # TODO getdictkeyssortedbyvaluekey(dict, key) - eg. dict['author'] = { 'commits' : 512 } - ...key(dict, 'commits')
26 class DataCollector:
27 """Manages data collection from a revision control repository."""
28 def __init__(self):
29 self.stamp_created = time.time()
30 pass
33 # This should be the main function to extract data from the repository.
34 def collect(self, dir):
35 self.dir = dir
38 # : get a dictionary of author
39 def getAuthorInfo(self, author):
40 return None
42 def getActivityByDayOfWeek(self):
43 return {}
45 def getActivityByHourOfDay(self):
46 return {}
49 # Get a list of authors
50 def getAuthors(self):
51 return []
53 def getFirstCommitDate(self):
54 return datetime.datetime.now()
56 def getLastCommitDate(self):
57 return datetime.datetime.now()
59 def getStampCreated(self):
60 return self.stamp_created
62 def getTags(self):
63 return []
65 def getTotalAuthors(self):
66 return -1
68 def getTotalCommits(self):
69 return -1
71 def getTotalFiles(self):
72 return -1
74 def getTotalLOC(self):
75 return -1
77 class GitDataCollector(DataCollector):
78 def collect(self, dir):
79 DataCollector.collect(self, dir)
81 self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
82 self.total_commits = int(getoutput('git-rev-list HEAD |wc -l'))
83 self.total_files = int(getoutput('git-ls-files |wc -l'))
84 #self.total_lines = int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
86 self.activity_by_hour_of_day = {} # hour -> commits
87 self.activity_by_day_of_week = {} # day -> commits
88 self.activity_by_month_of_year = {} # month [1-12] -> commits
89 self.activity_by_hour_of_week = {} # weekday -> hour -> commits
91 self.authors = {} # name -> {commits, first_commit_stamp, last_commit_stamp}
93 # author of the month
94 self.author_of_month = {} # month -> author -> commits
95 self.author_of_year = {} # year -> author -> commits
96 self.commits_by_month = {} # month -> commits
97 self.commits_by_year = {} # year -> commits
98 self.first_commit_stamp = 0
99 self.last_commit_stamp = 0
101 # tags
102 self.tags = {}
103 lines = getoutput('git-show-ref --tags').split('\n')
104 for line in lines:
105 if len(line) == 0:
106 continue
107 (hash, tag) = line.split(' ')
108 tag = tag.replace('refs/tags/', '')
109 output = getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
110 if len(output) > 0:
111 parts = output.split(' ')
112 stamp = 0
113 try:
114 stamp = int(parts[0])
115 except ValueError:
116 stamp = 0
117 self.tags[tag] = { 'stamp': stamp, 'hash' : hash, 'date' : datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d') }
118 pass
120 # Collect revision statistics
121 # Outputs "<stamp> <author>"
122 lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
123 for line in lines:
124 # linux-2.6 says "<unknown>" for one line O_o
125 parts = line.split(' ')
126 author = ''
127 try:
128 stamp = int(parts[0])
129 except ValueError:
130 stamp = 0
131 if len(parts) > 1:
132 author = ' '.join(parts[1:])
133 date = datetime.datetime.fromtimestamp(float(stamp))
135 # First and last commit stamp
136 if self.last_commit_stamp == 0:
137 self.last_commit_stamp = stamp
138 self.first_commit_stamp = stamp
140 # activity
141 # hour
142 hour = date.hour
143 if hour in self.activity_by_hour_of_day:
144 self.activity_by_hour_of_day[hour] += 1
145 else:
146 self.activity_by_hour_of_day[hour] = 1
148 # day of week
149 day = date.weekday()
150 if day in self.activity_by_day_of_week:
151 self.activity_by_day_of_week[day] += 1
152 else:
153 self.activity_by_day_of_week[day] = 1
155 # hour of week
156 if day not in self.activity_by_hour_of_week:
157 self.activity_by_hour_of_week[day] = {}
158 if hour not in self.activity_by_hour_of_week[day]:
159 self.activity_by_hour_of_week[day][hour] = 1
160 else:
161 self.activity_by_hour_of_week[day][hour] += 1
163 # month of year
164 month = date.month
165 if month in self.activity_by_month_of_year:
166 self.activity_by_month_of_year[month] += 1
167 else:
168 self.activity_by_month_of_year[month] = 1
170 # author stats
171 if author not in self.authors:
172 self.authors[author] = {}
173 # TODO commits
174 if 'last_commit_stamp' not in self.authors[author]:
175 self.authors[author]['last_commit_stamp'] = stamp
176 self.authors[author]['first_commit_stamp'] = stamp
177 if 'commits' in self.authors[author]:
178 self.authors[author]['commits'] += 1
179 else:
180 self.authors[author]['commits'] = 1
182 # author of the month/year
183 yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
184 if yymm in self.author_of_month:
185 if author in self.author_of_month[yymm]:
186 self.author_of_month[yymm][author] += 1
187 else:
188 self.author_of_month[yymm][author] = 1
189 else:
190 self.author_of_month[yymm] = {}
191 self.author_of_month[yymm][author] = 1
192 if yymm in self.commits_by_month:
193 self.commits_by_month[yymm] += 1
194 else:
195 self.commits_by_month[yymm] = 1
197 yy = datetime.datetime.fromtimestamp(stamp).year
198 if yy in self.author_of_year:
199 if author in self.author_of_year[yy]:
200 self.author_of_year[yy][author] += 1
201 else:
202 self.author_of_year[yy][author] = 1
203 else:
204 self.author_of_year[yy] = {}
205 self.author_of_year[yy][author] = 1
206 if yy in self.commits_by_year:
207 self.commits_by_year[yy] += 1
208 else:
209 self.commits_by_year[yy] = 1
211 # outputs "<stamp> <files>" for each revision
212 self.files_by_stamp = {} # stamp -> files
213 lines = getoutput('git-rev-list --pretty=format:"%at %H" HEAD |grep -v ^commit |while read line; do set $line; echo "$1 $(git-ls-tree -r "$2" |wc -l)"; done').split('\n')
214 for line in lines:
215 parts = line.split(' ')
216 if len(parts) != 2:
217 continue
218 (stamp, files) = parts[0:2]
219 self.files_by_stamp[int(stamp)] = int(files)
221 # extensions
222 self.extensions = {} # extension -> files, lines
223 lines = getoutput('git-ls-files').split('\n')
224 for line in lines:
225 base = os.path.basename(line)
226 if base.find('.') == -1:
227 ext = ''
228 else:
229 ext = base[(base.rfind('.') + 1):]
231 if ext not in self.extensions:
232 self.extensions[ext] = {'files': 0, 'lines': 0}
234 self.extensions[ext]['files'] += 1
235 try:
236 # FIXME filenames with spaces or special characters are broken
237 self.extensions[ext]['lines'] += int(getoutput('wc -l < %s' % line, quiet = True))
238 except:
239 print 'Warning: Could not count lines for file "%s"' % line
241 # line statistics
242 # outputs:
243 # N files changed, N insertions (+), N deletions(-)
244 # <stamp> <author>
245 self.changes_by_date = {} # stamp -> { files, ins, del }
246 lines = getoutput('git-log --shortstat --pretty=format:"%at %an" |tac').split('\n')
247 files = 0; inserted = 0; deleted = 0; total_lines = 0
248 for line in lines:
249 if len(line) == 0:
250 continue
252 # <stamp> <author>
253 if line.find(',') == -1:
254 pos = line.find(' ')
255 (stamp, author) = (int(line[:pos]), line[pos+1:])
256 self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted, 'lines': total_lines }
257 else:
258 numbers = re.findall('\d+', line)
259 if len(numbers) == 3:
260 (files, inserted, deleted) = map(lambda el : int(el), numbers)
261 total_lines += inserted
262 total_lines -= deleted
263 else:
264 print 'Warning: failed to handle line "%s"' % line
265 (files, inserted, deleted) = (0, 0, 0)
266 #self.changes_by_date[stamp] = { 'files': files, 'ins': inserted, 'del': deleted }
267 self.total_lines = total_lines
269 def getActivityByDayOfWeek(self):
270 return self.activity_by_day_of_week
272 def getActivityByHourOfDay(self):
273 return self.activity_by_hour_of_day
275 def getAuthorInfo(self, author):
276 a = self.authors[author]
278 commits = a['commits']
279 commits_frac = (100 * float(commits)) / self.getTotalCommits()
280 date_first = datetime.datetime.fromtimestamp(a['first_commit_stamp'])
281 date_last = datetime.datetime.fromtimestamp(a['last_commit_stamp'])
282 delta = date_last - date_first
284 res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first.strftime('%Y-%m-%d'), 'date_last': date_last.strftime('%Y-%m-%d'), 'timedelta' : delta }
285 return res
287 def getAuthors(self):
288 return self.authors.keys()
290 def getFirstCommitDate(self):
291 return datetime.datetime.fromtimestamp(self.first_commit_stamp)
293 def getLastCommitDate(self):
294 return datetime.datetime.fromtimestamp(self.last_commit_stamp)
296 def getTags(self):
297 lines = getoutput('git-show-ref --tags |cut -d/ -f3')
298 return lines.split('\n')
300 def getTagDate(self, tag):
301 return self.revToDate('tags/' + tag)
303 def getTotalAuthors(self):
304 return self.total_authors
306 def getTotalCommits(self):
307 return self.total_commits
309 def getTotalFiles(self):
310 return self.total_files
312 def getTotalLOC(self):
313 return self.total_lines
315 def revToDate(self, rev):
316 stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
317 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
319 class ReportCreator:
320 """Creates the actual report based on given data."""
321 def __init__(self):
322 pass
324 def create(self, data, path):
325 self.data = data
326 self.path = path
328 def html_linkify(text):
329 return text.lower().replace(' ', '_')
331 def html_header(level, text):
332 name = html_linkify(text)
333 return '\n<h%d><a href="#%s" name="%s">%s</a></h%d>\n\n' % (level, name, name, text, level)
335 class HTMLReportCreator(ReportCreator):
336 def create(self, data, path):
337 ReportCreator.create(self, data, path)
339 # TODO copy the CSS if it does not exist
340 if not os.path.exists(path + '/gitstats.css'):
341 #shutil.copyfile('')
342 pass
344 f = open(path + "/index.html", 'w')
345 format = '%Y-%m-%d %H:%m:%S'
346 self.printHeader(f)
348 f.write('<h1>GitStats</h1>')
350 self.printNav(f)
352 f.write('<dl>');
353 f.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime.datetime.now().strftime(format), time.time() - data.getStampCreated()));
354 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
355 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
356 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
357 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
358 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
359 f.write('</dl>');
361 f.write('</body>\n</html>');
362 f.close()
365 # Activity
366 f = open(path + '/activity.html', 'w')
367 self.printHeader(f)
368 f.write('<h1>Activity</h1>')
369 self.printNav(f)
371 #f.write('<h2>Last 30 days</h2>')
373 #f.write('<h2>Last 12 months</h2>')
375 # Hour of Day
376 f.write(html_header(2, 'Hour of Day'))
377 hour_of_day = data.getActivityByHourOfDay()
378 f.write('<table><tr><th>Hour</th>')
379 for i in range(1, 25):
380 f.write('<th>%d</th>' % i)
381 f.write('</tr>\n<tr><th>Commits</th>')
382 fp = open(path + '/hour_of_day.dat', 'w')
383 for i in range(0, 24):
384 if i in hour_of_day:
385 f.write('<td>%d</td>' % hour_of_day[i])
386 fp.write('%d %d\n' % (i, hour_of_day[i]))
387 else:
388 f.write('<td>0</td>')
389 fp.write('%d 0\n' % i)
390 fp.close()
391 f.write('</tr>\n<tr><th>%</th>')
392 totalcommits = data.getTotalCommits()
393 for i in range(0, 24):
394 if i in hour_of_day:
395 f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
396 else:
397 f.write('<td>0.00</td>')
398 f.write('</tr></table>')
399 f.write('<img src="hour_of_day.png" alt="Hour of Day" />')
400 fg = open(path + '/hour_of_day.dat', 'w')
401 for i in range(0, 24):
402 if i in hour_of_day:
403 fg.write('%d %d\n' % (i + 1, hour_of_day[i]))
404 else:
405 fg.write('%d 0\n' % (i + 1))
406 fg.close()
408 # Day of Week
409 f.write(html_header(2, 'Day of Week'))
410 day_of_week = data.getActivityByDayOfWeek()
411 f.write('<div class="vtable"><table>')
412 f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
413 fp = open(path + '/day_of_week.dat', 'w')
414 for d in range(0, 7):
415 commits = 0
416 if d in day_of_week:
417 commits = day_of_week[d]
418 fp.write('%d %d\n' % (d + 1, commits))
419 f.write('<tr>')
420 f.write('<th>%d</th>' % (d + 1))
421 if d in day_of_week:
422 f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
423 else:
424 f.write('<td>0</td>')
425 f.write('</tr>')
426 f.write('</table></div>')
427 f.write('<img src="day_of_week.png" alt="Day of Week" />')
428 fp.close()
430 # Hour of Week
431 f.write(html_header(2, 'Hour of Week'))
432 f.write('<table>')
434 f.write('<tr><th>Weekday</th>')
435 for hour in range(0, 24):
436 f.write('<th>%d</th>' % (hour + 1))
437 f.write('</tr>')
439 for weekday in range(0, 7):
440 f.write('<tr><th>%d</th>' % (weekday + 1))
441 for hour in range(0, 24):
442 try:
443 commits = data.activity_by_hour_of_week[weekday][hour]
444 except KeyError:
445 commits = 0
446 if commits != 0:
447 f.write('<td>%d</td>' % commits)
448 else:
449 f.write('<td></td>')
450 f.write('</tr>')
452 f.write('</table>')
454 # Month of Year
455 f.write(html_header(2, 'Month of Year'))
456 f.write('<div class="vtable"><table>')
457 f.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
458 fp = open (path + '/month_of_year.dat', 'w')
459 for mm in range(1, 13):
460 commits = 0
461 if mm in data.activity_by_month_of_year:
462 commits = data.activity_by_month_of_year[mm]
463 f.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm, commits, (100.0 * commits) / data.getTotalCommits()))
464 fp.write('%d %d\n' % (mm, commits))
465 fp.close()
466 f.write('</table></div>')
467 f.write('<img src="month_of_year.png" alt="Month of Year" />')
469 # Commits by year/month
470 f.write(html_header(2, 'Commits by year/month'))
471 f.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
472 for yymm in reversed(sorted(data.commits_by_month.keys())):
473 f.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm, data.commits_by_month[yymm]))
474 f.write('</table></div>')
475 f.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
476 fg = open(path + '/commits_by_year_month.dat', 'w')
477 for yymm in sorted(data.commits_by_month.keys()):
478 fg.write('%s %s\n' % (yymm, data.commits_by_month[yymm]))
479 fg.close()
481 # Commits by year
482 f.write(html_header(2, 'Commits by Year'))
483 f.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
484 for yy in reversed(sorted(data.commits_by_year.keys())):
485 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()))
486 f.write('</table></div>')
487 f.write('<img src="commits_by_year.png" alt="Commits by Year" />')
488 fg = open(path + '/commits_by_year.dat', 'w')
489 for yy in sorted(data.commits_by_year.keys()):
490 fg.write('%d %d\n' % (yy, data.commits_by_year[yy]))
491 fg.close()
493 f.write('</body></html>')
494 f.close()
497 # Authors
498 f = open(path + '/authors.html', 'w')
499 self.printHeader(f)
501 f.write('<h1>Authors</h1>')
502 self.printNav(f)
504 # Authors :: List of authors
505 f.write(html_header(2, 'List of Authors'))
507 f.write('<table class="authors">')
508 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th><th>Age</th></tr>')
509 for author in sorted(data.getAuthors()):
510 info = data.getAuthorInfo(author)
511 f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (author, info['commits'], info['commits_frac'], info['date_first'], info['date_last'], info['timedelta']))
512 f.write('</table>')
514 # Authors :: Author of Month
515 f.write(html_header(2, 'Author of Month'))
516 f.write('<table>')
517 f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
518 for yymm in reversed(sorted(data.author_of_month.keys())):
519 authordict = data.author_of_month[yymm]
520 authors = getkeyssortedbyvalues(authordict)
521 authors.reverse()
522 commits = data.author_of_month[yymm][authors[0]]
523 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td></tr>' % (yymm, authors[0], commits, (100 * commits) / data.commits_by_month[yymm], data.commits_by_month[yymm]))
525 f.write('</table>')
527 f.write(html_header(2, 'Author of Year'))
528 f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
529 for yy in reversed(sorted(data.author_of_year.keys())):
530 authordict = data.author_of_year[yy]
531 authors = getkeyssortedbyvalues(authordict)
532 authors.reverse()
533 commits = data.author_of_year[yy][authors[0]]
534 f.write('<tr><td>%s</td><td>%s</td><td>%d (%.2f%% of %d)</td></tr>' % (yy, authors[0], commits, (100 * commits) / data.commits_by_year[yy], data.commits_by_year[yy]))
535 f.write('</table>')
537 f.write('</body></html>')
538 f.close()
541 # Files
542 f = open(path + '/files.html', 'w')
543 self.printHeader(f)
544 f.write('<h1>Files</h1>')
545 self.printNav(f)
547 f.write('<dl>\n')
548 f.write('<dt>Total files</dt><dd>%d</dd>' % data.getTotalFiles())
549 f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
550 f.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data.getTotalLOC()) / data.getTotalFiles()))
551 f.write('</dl>\n')
553 # Files :: File count by date
554 f.write(html_header(2, 'File count by date'))
556 fg = open(path + '/files_by_date.dat', 'w')
557 for stamp in sorted(data.files_by_stamp.keys()):
558 fg.write('%s %d\n' % (datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d'), data.files_by_stamp[stamp]))
559 fg.close()
561 f.write('<img src="files_by_date.png" alt="Files by Date" />')
563 #f.write('<h2>Average file size by date</h2>')
565 # Files :: Extensions
566 f.write(html_header(2, 'Extensions'))
567 f.write('<table><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
568 for ext in sorted(data.extensions.keys()):
569 files = data.extensions[ext]['files']
570 lines = data.extensions[ext]['lines']
571 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))
572 f.write('</table>')
574 f.write('</body></html>')
575 f.close()
578 # Lines
579 f = open(path + '/lines.html', 'w')
580 self.printHeader(f)
581 f.write('<h1>Lines</h1>')
582 self.printNav(f)
584 f.write('<dl>\n')
585 f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
586 f.write('</dl>\n')
588 f.write(html_header(2, 'Lines of Code'))
589 f.write('<img src="lines_of_code.png" />')
591 fg = open(path + '/lines_of_code.dat', 'w')
592 for stamp in sorted(data.changes_by_date.keys()):
593 fg.write('%d %d\n' % (stamp, data.changes_by_date[stamp]['lines']))
594 fg.close()
596 f.write('</body></html>')
597 f.close()
600 # tags.html
601 f = open(path + '/tags.html', 'w')
602 self.printHeader(f)
603 f.write('<h1>Tags</h1>')
604 self.printNav(f)
606 f.write('<dl>')
607 f.write('<dt>Total tags</dt><dd>%d</dd>' % len(data.tags))
608 if len(data.tags) > 0:
609 f.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data.getTotalCommits() / len(data.tags)))
610 f.write('</dl>')
612 f.write('<table>')
613 f.write('<tr><th>Name</th><th>Date</th></tr>')
614 # sort the tags by date desc
615 tags_sorted_by_date_desc = map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), data.tags.items()))))
616 for tag in tags_sorted_by_date_desc:
617 f.write('<tr><td>%s</td><td>%s</td></tr>' % (tag, data.tags[tag]['date']))
618 f.write('</table>')
620 f.write('</body></html>')
621 f.close()
623 self.createGraphs(path)
624 pass
626 def createGraphs(self, path):
627 print 'Generating graphs...'
629 # hour of day
630 f = open(path + '/hour_of_day.plot', 'w')
631 f.write(GNUPLOT_COMMON)
632 f.write(
634 set output 'hour_of_day.png'
635 unset key
636 set xrange [0.5:24.5]
637 set xtics 4
638 set ylabel "Commits"
639 plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
640 """)
641 f.close()
643 # day of week
644 f = open(path + '/day_of_week.plot', 'w')
645 f.write(GNUPLOT_COMMON)
646 f.write(
648 set output 'day_of_week.png'
649 unset key
650 set xrange [0.5:7.5]
651 set xtics 1
652 set ylabel "Commits"
653 plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
654 """)
655 f.close()
657 # Month of Year
658 f = open(path + '/month_of_year.plot', 'w')
659 f.write(GNUPLOT_COMMON)
660 f.write(
662 set output 'month_of_year.png'
663 unset key
664 set xrange [0.5:12.5]
665 set xtics 1
666 set ylabel "Commits"
667 plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
668 """)
669 f.close()
671 # commits_by_year_month
672 f = open(path + '/commits_by_year_month.plot', 'w')
673 f.write(GNUPLOT_COMMON)
674 f.write(
676 set output 'commits_by_year_month.png'
677 unset key
678 set xdata time
679 set timefmt "%Y-%m"
680 set format x "%Y-%m"
681 set xtics rotate by 90 15768000
682 set ylabel "Commits"
683 plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
684 """)
685 f.close()
687 # commits_by_year
688 f = open(path + '/commits_by_year.plot', 'w')
689 f.write(GNUPLOT_COMMON)
690 f.write(
692 set output 'commits_by_year.png'
693 unset key
694 set xtics 1
695 set ylabel "Commits"
696 plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
697 """)
698 f.close()
700 # Files by date
701 f = open(path + '/files_by_date.plot', 'w')
702 f.write(GNUPLOT_COMMON)
703 f.write(
705 set output 'files_by_date.png'
706 unset key
707 set xdata time
708 set timefmt "%Y-%m-%d"
709 set format x "%Y-%m-%d"
710 set ylabel "Files"
711 set xtics rotate by 90
712 plot 'files_by_date.dat' using 1:2 smooth csplines
713 """)
714 f.close()
716 # Lines of Code
717 f = open(path + '/lines_of_code.plot', 'w')
718 f.write(GNUPLOT_COMMON)
719 f.write(
721 set output 'lines_of_code.png'
722 unset key
723 set xdata time
724 set timefmt "%s"
725 set format x "%Y-%m-%d"
726 set ylabel "Lines"
727 set xtics rotate by 90
728 plot 'lines_of_code.dat' using 1:2 w lines
729 """)
730 f.close()
732 os.chdir(path)
733 files = glob.glob(path + '/*.plot')
734 for f in files:
735 print '>> gnuplot %s' % os.path.basename(f)
736 os.system('gnuplot %s' % f)
738 def printHeader(self, f):
739 f.write(
740 """<?xml version="1.0" encoding="UTF-8"?>
741 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
742 <html xmlns="http://www.w3.org/1999/xhtml">
743 <head>
744 <title>GitStats</title>
745 <link rel="stylesheet" href="gitstats.css" type="text/css" />
746 <meta name="generator" content="GitStats" />
747 </head>
748 <body>
749 """)
751 def printNav(self, f):
752 f.write("""
753 <div class="nav">
754 <ul>
755 <li><a href="index.html">General</a></li>
756 <li><a href="activity.html">Activity</a></li>
757 <li><a href="authors.html">Authors</a></li>
758 <li><a href="files.html">Files</a></li>
759 <li><a href="lines.html">Lines</a></li>
760 <li><a href="tags.html">Tags</a></li>
761 </ul>
762 </div>
763 """)
766 usage = """
767 Usage: gitstats [options] <gitpath> <outputpath>
769 Options:
772 if len(sys.argv) < 3:
773 print usage
774 sys.exit(0)
776 gitpath = sys.argv[1]
777 outputpath = os.path.abspath(sys.argv[2])
779 try:
780 os.makedirs(outputpath)
781 except OSError:
782 pass
783 if not os.path.isdir(outputpath):
784 print 'FATAL: Output path is not a directory or does not exist'
785 sys.exit(1)
787 print 'Git path: %s' % gitpath
788 print 'Output path: %s' % outputpath
790 os.chdir(gitpath)
792 print 'Collecting data...'
793 data = GitDataCollector()
794 data.collect(gitpath)
796 print 'Generating report...'
797 report = HTMLReportCreator()
798 report.create(data, outputpath)