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