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