statgit 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/python
  2. # Copyright (c) 2007 Heikki Hokkanen <hoxu@users.sf.net>
  3. # GPLv2
  4. import commands
  5. import datetime
  6. import os
  7. import re
  8. import sys
  9. def getoutput(cmd):
  10. print '>> %s' % cmd
  11. output = commands.getoutput(cmd)
  12. return output
  13. class DataCollector:
  14. def __init__(self):
  15. pass
  16. ##
  17. # This should be the main function to extract data from the repository.
  18. def collect(self, dir):
  19. self.dir = dir
  20. ##
  21. # : get a dictionary of author
  22. def getAuthorInfo(self, author):
  23. return None
  24. ##
  25. # Get a list of authors
  26. def getAuthors(self):
  27. return []
  28. def getFirstCommitDate(self):
  29. return datetime.datetime.now()
  30. def getLastCommitDate(self):
  31. return datetime.datetime.now()
  32. def getTags(self):
  33. return []
  34. def getTotalAuthors(self):
  35. return -1
  36. def getTotalCommits(self):
  37. return -1
  38. def getTotalFiles(self):
  39. return -1
  40. def getTotalLOC(self):
  41. return -1
  42. class GitDataCollector(DataCollector):
  43. def collect(self, dir):
  44. DataCollector.collect(self, dir)
  45. self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
  46. self.total_commits = int(getoutput('git-rev-list --all |wc -l'))
  47. self.total_files = int(getoutput('git-ls-files |wc -l'))
  48. self.total_lines = int(getoutput('git-ls-files |xargs cat |wc -l'))
  49. def getAuthorInfo(self, author):
  50. commits = int(getoutput('git-rev-list --all --author="%s" |wc -l' % author))
  51. commits_frac = (100 * float(commits)) / self.getTotalCommits()
  52. date_first = '0000-00-00'
  53. date_last = '0000-00-00'
  54. rev_last = getoutput('git-rev-list --all --author="%s" -n 1' % author)
  55. rev_first = getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
  56. date_first = self.revToDate(rev_first)
  57. date_last = self.revToDate(rev_last)
  58. res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
  59. return res
  60. def getAuthors(self):
  61. lines = getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
  62. return lines.split('\n')
  63. def getTags(self):
  64. lines = getoutput('git-show-ref --tags |cut -d/ -f3')
  65. return lines.split('\n')
  66. def getTagDate(self, tag):
  67. return self.revToDate('tags/' + tag)
  68. def getTotalAuthors(self):
  69. return self.total_authors
  70. def getTotalCommits(self):
  71. return self.total_commits
  72. def getTotalFiles(self):
  73. return self.total_files
  74. def getTotalLOC(self):
  75. return self.total_lines
  76. def revToDate(self, rev):
  77. stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
  78. return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
  79. class ReportCreator:
  80. def __init__(self):
  81. pass
  82. def create(self, data, path):
  83. self.data = data
  84. self.path = path
  85. class HTMLReportCreator(ReportCreator):
  86. def create(self, data, path):
  87. ReportCreator.create(self, data, path)
  88. f = open(path + "/index.html", 'w')
  89. f.write("""<html>
  90. <head>
  91. <title>StatGit</title>
  92. <link rel="stylesheet" href="statgit.css" type="text/css" />
  93. </head>
  94. <body>
  95. """)
  96. format = '%Y-%m-%d %H:%m:%S'
  97. f.write('<h1>StatGit</h1>')
  98. f.write('<dl>');
  99. f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
  100. f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
  101. f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
  102. f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
  103. f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
  104. f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
  105. f.write('</dl>');
  106. f.write("""<ul>
  107. <li><a href="activity.html">Activity</a></li>
  108. <li><a href="authors.html">Authors</a></li>
  109. <li><a href="files.html">Files</a></li>
  110. </ul>
  111. """)
  112. f.write('<h2>Authors</h2>')
  113. f.write('<table class="authors">')
  114. f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
  115. for author in data.getAuthors():
  116. info = data.getAuthorInfo(author)
  117. 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']))
  118. f.write('</table>')
  119. f.write('<h2>Tags</h2>')
  120. f.write('<table>')
  121. f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
  122. for tag in data.getTags():
  123. f.write('<tr><td>%s</td><td></td></tr>' % tag)
  124. f.write('</table>')
  125. f.write('</body>\n</html>');
  126. f.close()
  127. pass
  128. usage = """
  129. Usage: statgit [options] <gitpath> <outputpath>
  130. Options:
  131. -o html
  132. """
  133. if len(sys.argv) < 3:
  134. print usage
  135. sys.exit(0)
  136. gitpath = sys.argv[1]
  137. outputpath = sys.argv[2]
  138. print 'Git path: %s' % gitpath
  139. print 'Output path: %s' % outputpath
  140. os.chdir(gitpath)
  141. print 'Collecting data...'
  142. data = GitDataCollector()
  143. data.collect(gitpath)
  144. print 'Generating report...'
  145. report = HTMLReportCreator()
  146. report.create(data, outputpath)