123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 sys
  10. import time
  11. GNUPLOT_COMMON = 'set terminal png transparent\nset size 0.5,0.5\n'
  12. def getoutput(cmd, quiet = False):
  13. if not quiet:
  14. print '>> %s' % cmd
  15. output = commands.getoutput(cmd)
  16. return output
  17. def getkeyssortedbyvalues(dict):
  18. return map(lambda el : el[1], sorted(map(lambda el : (el[1], el[0]), dict.items())))
  19. # TODO getdictkeyssortedbyvaluekey(dict, key) - eg. dict['author'] = { 'commits' : 512 } - ...key(dict, 'commits')
  20. class DataCollector:
  21. def __init__(self):
  22. self.stamp_created = time.time()
  23. pass
  24. ##
  25. # This should be the main function to extract data from the repository.
  26. def collect(self, dir):
  27. self.dir = dir
  28. ##
  29. # : get a dictionary of author
  30. def getAuthorInfo(self, author):
  31. return None
  32. def getActivityByDayOfWeek(self):
  33. return {}
  34. def getActivityByHourOfDay(self):
  35. return {}
  36. ##
  37. # Get a list of authors
  38. def getAuthors(self):
  39. return []
  40. def getFirstCommitDate(self):
  41. return datetime.datetime.now()
  42. def getLastCommitDate(self):
  43. return datetime.datetime.now()
  44. def getStampCreated(self):
  45. return self.stamp_created
  46. def getTags(self):
  47. return []
  48. def getTotalAuthors(self):
  49. return -1
  50. def getTotalCommits(self):
  51. return -1
  52. def getTotalFiles(self):
  53. return -1
  54. def getTotalLOC(self):
  55. return -1
  56. class GitDataCollector(DataCollector):
  57. def collect(self, dir):
  58. DataCollector.collect(self, dir)
  59. self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
  60. self.total_commits = int(getoutput('git-rev-list HEAD |wc -l'))
  61. self.total_files = int(getoutput('git-ls-files |wc -l'))
  62. self.total_lines = int(getoutput('git-ls-files -z |xargs -0 cat |wc -l'))
  63. self.activity_by_hour_of_day = {} # hour -> commits
  64. self.activity_by_day_of_week = {} # day -> commits
  65. self.activity_by_month_of_year = {} # month [1-12] -> commits
  66. self.activity_by_hour_of_week = {} # weekday -> hour -> commits
  67. self.authors = {} # name -> {commits, first_commit_stamp, last_commit_stamp}
  68. # author of the month
  69. self.author_of_month = {} # month -> author -> commits
  70. self.author_of_year = {} # year -> author -> commits
  71. self.commits_by_month = {} # month -> commits
  72. self.commits_by_year = {} # year -> commits
  73. self.first_commit_stamp = 0
  74. self.last_commit_stamp = 0
  75. # tags
  76. self.tags = {}
  77. lines = getoutput('git-show-ref --tags').split('\n')
  78. for line in lines:
  79. if len(line) == 0:
  80. continue
  81. (hash, tag) = line.split(' ')
  82. tag = tag.replace('refs/tags/', '')
  83. output = getoutput('git-log "%s" --pretty=format:"%%at %%an" -n 1' % hash)
  84. if len(output) > 0:
  85. parts = output.split(' ')
  86. stamp = 0
  87. try:
  88. stamp = int(parts[0])
  89. except ValueError:
  90. stamp = 0
  91. self.tags[tag] = { 'stamp': stamp, 'hash' : hash, 'date' : datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d') }
  92. pass
  93. # Collect revision statistics
  94. # Outputs "<stamp> <author>"
  95. lines = getoutput('git-rev-list --pretty=format:"%at %an" HEAD |grep -v ^commit').split('\n')
  96. for line in lines:
  97. # linux-2.6 says "<unknown>" for one line O_o
  98. parts = line.split(' ')
  99. author = ''
  100. try:
  101. stamp = int(parts[0])
  102. except ValueError:
  103. stamp = 0
  104. if len(parts) > 1:
  105. author = ' '.join(parts[1:])
  106. date = datetime.datetime.fromtimestamp(float(stamp))
  107. # First and last commit stamp
  108. if self.last_commit_stamp == 0:
  109. self.last_commit_stamp = stamp
  110. self.first_commit_stamp = stamp
  111. # activity
  112. # hour
  113. hour = date.hour
  114. if hour in self.activity_by_hour_of_day:
  115. self.activity_by_hour_of_day[hour] += 1
  116. else:
  117. self.activity_by_hour_of_day[hour] = 1
  118. # day of week
  119. day = date.weekday()
  120. if day in self.activity_by_day_of_week:
  121. self.activity_by_day_of_week[day] += 1
  122. else:
  123. self.activity_by_day_of_week[day] = 1
  124. # hour of week
  125. if day not in self.activity_by_hour_of_week:
  126. self.activity_by_hour_of_week[day] = {}
  127. if hour not in self.activity_by_hour_of_week[day]:
  128. self.activity_by_hour_of_week[day][hour] = 1
  129. else:
  130. self.activity_by_hour_of_week[day][hour] += 1
  131. # month of year
  132. month = date.month
  133. if month in self.activity_by_month_of_year:
  134. self.activity_by_month_of_year[month] += 1
  135. else:
  136. self.activity_by_month_of_year[month] = 1
  137. # author stats
  138. if author not in self.authors:
  139. self.authors[author] = {}
  140. # TODO commits
  141. if 'last_commit_stamp' not in self.authors[author]:
  142. self.authors[author]['last_commit_stamp'] = stamp
  143. self.authors[author]['first_commit_stamp'] = stamp
  144. if 'commits' in self.authors[author]:
  145. self.authors[author]['commits'] += 1
  146. else:
  147. self.authors[author]['commits'] = 1
  148. # author of the month/year
  149. yymm = datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m')
  150. if yymm in self.author_of_month:
  151. if author in self.author_of_month[yymm]:
  152. self.author_of_month[yymm][author] += 1
  153. else:
  154. self.author_of_month[yymm][author] = 1
  155. else:
  156. self.author_of_month[yymm] = {}
  157. self.author_of_month[yymm][author] = 1
  158. if yymm in self.commits_by_month:
  159. self.commits_by_month[yymm] += 1
  160. else:
  161. self.commits_by_month[yymm] = 1
  162. yy = datetime.datetime.fromtimestamp(stamp).year
  163. if yy in self.author_of_year:
  164. if author in self.author_of_year[yy]:
  165. self.author_of_year[yy][author] += 1
  166. else:
  167. self.author_of_year[yy][author] = 1
  168. else:
  169. self.author_of_year[yy] = {}
  170. self.author_of_year[yy][author] = 1
  171. if yy in self.commits_by_year:
  172. self.commits_by_year[yy] += 1
  173. else:
  174. self.commits_by_year[yy] = 1
  175. # outputs "<stamp> <files>" for each revision
  176. self.files_by_stamp = {} # stamp -> files
  177. 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')
  178. for line in lines:
  179. parts = line.split(' ')
  180. if len(parts) != 2:
  181. continue
  182. (stamp, files) = parts[0:2]
  183. self.files_by_stamp[int(stamp)] = int(files)
  184. # extensions
  185. self.extensions = {} # extension -> files, lines
  186. lines = getoutput('git-ls-files').split('\n')
  187. for line in lines:
  188. base = os.path.basename(line)
  189. if base.find('.') == -1:
  190. ext = ''
  191. else:
  192. ext = base[(base.rfind('.') + 1):]
  193. if ext not in self.extensions:
  194. self.extensions[ext] = {'files': 0, 'lines': 0}
  195. self.extensions[ext]['files'] += 1
  196. self.extensions[ext]['lines'] += int(getoutput('wc -l < %s' % line, quiet = True))
  197. def getActivityByDayOfWeek(self):
  198. return self.activity_by_day_of_week
  199. def getActivityByHourOfDay(self):
  200. return self.activity_by_hour_of_day
  201. def getAuthorInfo(self, author):
  202. a = self.authors[author]
  203. commits = a['commits']
  204. commits_frac = (100 * float(commits)) / self.getTotalCommits()
  205. date_first = datetime.datetime.fromtimestamp(a['first_commit_stamp']).strftime('%Y-%m-%d')
  206. date_last = datetime.datetime.fromtimestamp(a['last_commit_stamp']).strftime('%Y-%m-%d')
  207. res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
  208. return res
  209. def getAuthors(self):
  210. return self.authors.keys()
  211. def getFirstCommitDate(self):
  212. return datetime.datetime.fromtimestamp(self.first_commit_stamp)
  213. def getLastCommitDate(self):
  214. return datetime.datetime.fromtimestamp(self.last_commit_stamp)
  215. def getTags(self):
  216. lines = getoutput('git-show-ref --tags |cut -d/ -f3')
  217. return lines.split('\n')
  218. def getTagDate(self, tag):
  219. return self.revToDate('tags/' + tag)
  220. def getTotalAuthors(self):
  221. return self.total_authors
  222. def getTotalCommits(self):
  223. return self.total_commits
  224. def getTotalFiles(self):
  225. return self.total_files
  226. def getTotalLOC(self):
  227. return self.total_lines
  228. def revToDate(self, rev):
  229. stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
  230. return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
  231. class ReportCreator:
  232. def __init__(self):
  233. pass
  234. def create(self, data, path):
  235. self.data = data
  236. self.path = path
  237. class HTMLReportCreator(ReportCreator):
  238. def create(self, data, path):
  239. ReportCreator.create(self, data, path)
  240. f = open(path + "/index.html", 'w')
  241. format = '%Y-%m-%d %H:%m:%S'
  242. self.printHeader(f)
  243. f.write('<h1>StatGit</h1>')
  244. self.printNav(f)
  245. f.write('<dl>');
  246. f.write('<dt>Generated</dt><dd>%s (in %d seconds)</dd>' % (datetime.datetime.now().strftime(format), time.time() - data.getStampCreated()));
  247. f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
  248. f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
  249. f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
  250. f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
  251. f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
  252. f.write('</dl>');
  253. f.write('</body>\n</html>');
  254. f.close()
  255. ###
  256. # Activity
  257. f = open(path + '/activity.html', 'w')
  258. self.printHeader(f)
  259. f.write('<h1>Activity</h1>')
  260. self.printNav(f)
  261. f.write('<h2>Last 30 days</h2>')
  262. f.write('<h2>Last 12 months</h2>')
  263. # Hour of Day
  264. f.write('\n<h2>Hour of Day</h2>\n\n')
  265. hour_of_day = data.getActivityByHourOfDay()
  266. f.write('<table><tr><th>Hour</th>')
  267. for i in range(1, 25):
  268. f.write('<th>%d</th>' % i)
  269. f.write('</tr>\n<tr><th>Commits</th>')
  270. fp = open(path + '/hour_of_day.dat', 'w')
  271. for i in range(0, 24):
  272. if i in hour_of_day:
  273. f.write('<td>%d</td>' % hour_of_day[i])
  274. fp.write('%d %d\n' % (i, hour_of_day[i]))
  275. else:
  276. f.write('<td>0</td>')
  277. fp.write('%d 0\n' % i)
  278. fp.close()
  279. f.write('</tr>\n<tr><th>%</th>')
  280. totalcommits = data.getTotalCommits()
  281. for i in range(0, 24):
  282. if i in hour_of_day:
  283. f.write('<td>%.2f</td>' % ((100.0 * hour_of_day[i]) / totalcommits))
  284. else:
  285. f.write('<td>0.00</td>')
  286. f.write('</tr></table>')
  287. f.write('<img src="hour_of_day.png" alt="Hour of Day" />')
  288. fg = open(path + '/hour_of_day.dat', 'w')
  289. for i in range(0, 24):
  290. if i in hour_of_day:
  291. fg.write('%d %d\n' % (i + 1, hour_of_day[i]))
  292. else:
  293. fg.write('%d 0\n' % (i + 1))
  294. fg.close()
  295. # Day of Week
  296. # TODO show also by hour of weekday?
  297. f.write('\n<h2>Day of Week</h2>\n\n')
  298. day_of_week = data.getActivityByDayOfWeek()
  299. f.write('<div class="vtable"><table>')
  300. f.write('<tr><th>Day</th><th>Total (%)</th></tr>')
  301. fp = open(path + '/day_of_week.dat', 'w')
  302. for d in range(0, 7):
  303. fp.write('%d %d\n' % (d + 1, day_of_week[d]))
  304. f.write('<tr>')
  305. f.write('<th>%d</th>' % (d + 1))
  306. if d in day_of_week:
  307. f.write('<td>%d (%.2f%%)</td>' % (day_of_week[d], (100.0 * day_of_week[d]) / totalcommits))
  308. else:
  309. f.write('<td>0</td>')
  310. f.write('</tr>')
  311. f.write('</table></div>')
  312. f.write('<img src="day_of_week.png" alt="Day of Week" />')
  313. fp.close()
  314. # Hour of Week
  315. f.write('\n<h2>Hour of Week</h2>\n\n')
  316. f.write('<table>')
  317. f.write('<tr><th>Weekday</th>')
  318. for hour in range(0, 24):
  319. f.write('<th>%d</th>' % (hour + 1))
  320. f.write('</tr>')
  321. for weekday in range(0, 7):
  322. f.write('<tr><th>%d</th>' % (weekday + 1))
  323. for hour in range(0, 24):
  324. try:
  325. commits = data.activity_by_hour_of_week[weekday][hour]
  326. except KeyError:
  327. commits = 0
  328. if commits != 0:
  329. f.write('<td>%d</td>' % commits)
  330. else:
  331. f.write('<td></td>')
  332. f.write('</tr>')
  333. f.write('</table>')
  334. # Month of Year
  335. f.write('\n<h2>Month of Year</h2>\n\n')
  336. f.write('<div class="vtable"><table>')
  337. f.write('<tr><th>Month</th><th>Commits (%)</th></tr>')
  338. fp = open (path + '/month_of_year.dat', 'w')
  339. for mm in range(1, 13):
  340. commits = 0
  341. if mm in data.activity_by_month_of_year:
  342. commits = data.activity_by_month_of_year[mm]
  343. f.write('<tr><td>%d</td><td>%d (%.2f %%)</td></tr>' % (mm, commits, (100.0 * commits) / data.getTotalCommits()))
  344. fp.write('%d %d\n' % (mm, commits))
  345. fp.close()
  346. f.write('</table></div>')
  347. f.write('<img src="month_of_year.png" alt="Month of Year" />')
  348. # Commits by year/month
  349. f.write('<h2>Commits by year/month</h2>')
  350. f.write('<div class="vtable"><table><tr><th>Month</th><th>Commits</th></tr>')
  351. for yymm in reversed(sorted(data.commits_by_month.keys())):
  352. f.write('<tr><td>%s</td><td>%d</td></tr>' % (yymm, data.commits_by_month[yymm]))
  353. f.write('</table></div>')
  354. f.write('<img src="commits_by_year_month.png" alt="Commits by year/month" />')
  355. fg = open(path + '/commits_by_year_month.dat', 'w')
  356. for yymm in sorted(data.commits_by_month.keys()):
  357. fg.write('%s %s\n' % (yymm, data.commits_by_month[yymm]))
  358. fg.close()
  359. # Commits by year
  360. f.write('<h2>Commits by year</h2>')
  361. f.write('<div class="vtable"><table><tr><th>Year</th><th>Commits (% of all)</th></tr>')
  362. for yy in reversed(sorted(data.commits_by_year.keys())):
  363. 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()))
  364. f.write('</table></div>')
  365. f.write('<img src="commits_by_year.png" alt="Commits by Year" />')
  366. fg = open(path + '/commits_by_year.dat', 'w')
  367. for yy in sorted(data.commits_by_year.keys()):
  368. fg.write('%d %d\n' % (yy, data.commits_by_year[yy]))
  369. fg.close()
  370. f.write('</body></html>')
  371. f.close()
  372. ###
  373. # Authors
  374. f = open(path + '/authors.html', 'w')
  375. self.printHeader(f)
  376. f.write('<h1>Authors</h1>')
  377. self.printNav(f)
  378. f.write('\n<h2>List of authors</h2>\n\n')
  379. f.write('<table class="authors">')
  380. f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
  381. for author in sorted(data.getAuthors()):
  382. info = data.getAuthorInfo(author)
  383. f.write('<tr><td>%s</td><td>%d (%.2f%%)</td><td>%s</td><td>%s</td></tr>' % (author, info['commits'], info['commits_frac'], info['date_first'], info['date_last']))
  384. f.write('</table>')
  385. f.write('\n<h2>Author of Month</h2>\n\n')
  386. f.write('<table>')
  387. f.write('<tr><th>Month</th><th>Author</th><th>Commits (%)</th></tr>')
  388. for yymm in reversed(sorted(data.author_of_month.keys())):
  389. authordict = data.author_of_month[yymm]
  390. authors = getkeyssortedbyvalues(authordict)
  391. authors.reverse()
  392. commits = data.author_of_month[yymm][authors[0]]
  393. 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]))
  394. f.write('</table>')
  395. f.write('\n<h2>Author of Year</h2>\n\n')
  396. f.write('<table><tr><th>Year</th><th>Author</th><th>Commits (%)</th></tr>')
  397. for yy in reversed(sorted(data.author_of_year.keys())):
  398. authordict = data.author_of_year[yy]
  399. authors = getkeyssortedbyvalues(authordict)
  400. authors.reverse()
  401. commits = data.author_of_year[yy][authors[0]]
  402. 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]))
  403. f.write('</table>')
  404. f.write('</body></html>')
  405. f.close()
  406. ###
  407. # Files
  408. f = open(path + '/files.html', 'w')
  409. self.printHeader(f)
  410. f.write('<h1>Files</h1>')
  411. self.printNav(f)
  412. f.write('<dl>\n')
  413. f.write('<dt>Total files</dt><dd>%d</dd>' % data.getTotalFiles())
  414. f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
  415. f.write('<dt>Average file size</dt><dd>%.2f bytes</dd>' % ((100.0 * data.getTotalLOC()) / data.getTotalFiles()))
  416. f.write('</dl>\n')
  417. # Files :: File count by date
  418. f.write('<h2>File count by date</h2>')
  419. fg = open(path + '/files_by_date.dat', 'w')
  420. for stamp in sorted(data.files_by_stamp.keys()):
  421. fg.write('%s %d\n' % (datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d'), data.files_by_stamp[stamp]))
  422. fg.close()
  423. f.write('<img src="files_by_date.png" alt="Files by Date" />')
  424. #f.write('<h2>Average file size by date</h2>')
  425. # Files :: Extensions
  426. f.write('\n<h2>Extensions</h2>\n\n')
  427. f.write('<table><tr><th>Extension</th><th>Files (%)</th><th>Lines (%)</th><th>Lines/file</th></tr>')
  428. for ext in sorted(data.extensions.keys()):
  429. files = data.extensions[ext]['files']
  430. lines = data.extensions[ext]['lines']
  431. 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))
  432. f.write('</table>')
  433. f.write('</body></html>')
  434. f.close()
  435. ###
  436. # Lines
  437. f = open(path + '/lines.html', 'w')
  438. self.printHeader(f)
  439. f.write('<h1>Lines</h1>')
  440. self.printNav(f)
  441. f.write('<dl>\n')
  442. f.write('<dt>Total lines</dt><dd>%d</dd>' % data.getTotalLOC())
  443. f.write('</dl>\n')
  444. f.write('</body></html>')
  445. f.close()
  446. ###
  447. # tags.html
  448. f = open(path + '/tags.html', 'w')
  449. self.printHeader(f)
  450. f.write('<h1>Tags</h1>')
  451. self.printNav(f)
  452. f.write('<dl>')
  453. f.write('<dt>Total tags</dt><dd>%d</dd>' % len(data.tags))
  454. if len(data.tags) > 0:
  455. f.write('<dt>Average commits per tag</dt><dd>%.2f</dd>' % (data.getTotalCommits() / len(data.tags)))
  456. f.write('</dl>')
  457. f.write('<table>')
  458. f.write('<tr><th>Name</th><th>Date</th></tr>')
  459. # sort the tags by date desc
  460. tags_sorted_by_date_desc = map(lambda el : el[1], reversed(sorted(map(lambda el : (el[1]['date'], el[0]), data.tags.items()))))
  461. for tag in tags_sorted_by_date_desc:
  462. f.write('<tr><td>%s</td><td>%s</td></tr>' % (tag, data.tags[tag]['date']))
  463. f.write('</table>')
  464. f.write('</body></html>')
  465. f.close()
  466. self.createGraphs(path)
  467. pass
  468. def createGraphs(self, path):
  469. print 'Generating graphs...'
  470. # hour of day
  471. f = open(path + '/hour_of_day.plot', 'w')
  472. f.write(GNUPLOT_COMMON)
  473. f.write(
  474. """
  475. set output 'hour_of_day.png'
  476. unset key
  477. set xrange [0.5:24.5]
  478. set xtics 4
  479. set ylabel "Commits"
  480. plot 'hour_of_day.dat' using 1:2:(0.5) w boxes fs solid
  481. """)
  482. f.close()
  483. # day of week
  484. f = open(path + '/day_of_week.plot', 'w')
  485. f.write(GNUPLOT_COMMON)
  486. f.write(
  487. """
  488. set output 'day_of_week.png'
  489. unset key
  490. set xrange [0.5:7.5]
  491. set xtics 1
  492. set ylabel "Commits"
  493. plot 'day_of_week.dat' using 1:2:(0.5) w boxes fs solid
  494. """)
  495. f.close()
  496. # Month of Year
  497. f = open(path + '/month_of_year.plot', 'w')
  498. f.write(GNUPLOT_COMMON)
  499. f.write(
  500. """
  501. set output 'month_of_year.png'
  502. unset key
  503. set xrange [0.5:12.5]
  504. set xtics 1
  505. set ylabel "Commits"
  506. plot 'month_of_year.dat' using 1:2:(0.5) w boxes fs solid
  507. """)
  508. f.close()
  509. # commits_by_year_month
  510. f = open(path + '/commits_by_year_month.plot', 'w')
  511. f.write(GNUPLOT_COMMON)
  512. f.write(
  513. # TODO rotate xtic labels by 90 degrees
  514. """
  515. set output 'commits_by_year_month.png'
  516. unset key
  517. set xdata time
  518. set timefmt "%Y-%m"
  519. set format x "%Y-%m"
  520. set xtics 15768000
  521. set ylabel "Commits"
  522. plot 'commits_by_year_month.dat' using 1:2:(0.5) w boxes fs solid
  523. """)
  524. f.close()
  525. # commits_by_year
  526. f = open(path + '/commits_by_year.plot', 'w')
  527. f.write(GNUPLOT_COMMON)
  528. f.write(
  529. """
  530. set output 'commits_by_year.png'
  531. unset key
  532. set xtics 1
  533. set ylabel "Commits"
  534. plot 'commits_by_year.dat' using 1:2:(0.5) w boxes fs solid
  535. """)
  536. f.close()
  537. # Files by date
  538. f = open(path + '/files_by_date.plot', 'w')
  539. f.write(GNUPLOT_COMMON)
  540. f.write(
  541. """
  542. set output 'files_by_date.png'
  543. unset key
  544. set xdata time
  545. set timefmt "%Y-%m-%d"
  546. set format x "%Y-%m-%d"
  547. set ylabel "Files"
  548. set xtics rotate by 90
  549. plot 'files_by_date.dat' using 1:2 smooth csplines
  550. """)
  551. f.close()
  552. os.chdir(path)
  553. files = glob.glob(path + '/*.plot')
  554. for f in files:
  555. print '>> gnuplot %s' % os.path.basename(f)
  556. os.system('gnuplot %s' % f)
  557. def printHeader(self, f):
  558. f.write(
  559. """<?xml version="1.0" encoding="UTF-8"?>
  560. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  561. <html xmlns="http://www.w3.org/1999/xhtml">
  562. <head>
  563. <title>StatGit</title>
  564. <link rel="stylesheet" href="statgit.css" type="text/css" />
  565. <meta name="generator" content="statgit" />
  566. </head>
  567. <body>
  568. """)
  569. def printNav(self, f):
  570. f.write("""
  571. <div class="nav">
  572. <ul>
  573. <li><a href="index.html">General</a></li>
  574. <li><a href="activity.html">Activity</a></li>
  575. <li><a href="authors.html">Authors</a></li>
  576. <li><a href="files.html">Files</a></li>
  577. <li><a href="lines.html">Lines</a></li>
  578. <li><a href="tags.html">Tags</a></li>
  579. </ul>
  580. </div>
  581. """)
  582. usage = """
  583. Usage: statgit [options] <gitpath> <outputpath>
  584. Options:
  585. """
  586. if len(sys.argv) < 3:
  587. print usage
  588. sys.exit(0)
  589. gitpath = sys.argv[1]
  590. outputpath = os.path.abspath(sys.argv[2])
  591. print 'Git path: %s' % gitpath
  592. print 'Output path: %s' % outputpath
  593. os.chdir(gitpath)
  594. print 'Collecting data...'
  595. data = GitDataCollector()
  596. data.collect(gitpath)
  597. print 'Generating report...'
  598. report = HTMLReportCreator()
  599. report.create(data, outputpath)