123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. class DataCollector:
  10. def __init__(self):
  11. pass
  12. def collect(self, dir):
  13. self.dir = dir
  14. ##
  15. # TODO: get a dictionary of author
  16. def getAuthorInfo(self, author):
  17. return None
  18. ##
  19. # Get a list of authors
  20. def getAuthors(self):
  21. return []
  22. def getTotalAuthors(self):
  23. return -1
  24. def getTotalCommits(self):
  25. return -1
  26. def getTotalFiles(self):
  27. return -1
  28. def getTotalLOC(self):
  29. return -1
  30. class GitDataCollector(DataCollector):
  31. def collect(self, dir):
  32. DataCollector.collect(self, dir)
  33. def getAuthorInfo(self, author):
  34. res = { 'commits' : -1, 'commits_frac' : 1.5, 'date_first' : '0000-00-00', 'date_last' : '0000-00-00' }
  35. return res
  36. def getAuthors(self):
  37. lines = commands.getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
  38. return lines.split('\n')
  39. def getTotalAuthors(self):
  40. return int(commands.getoutput('git-log |git-shortlog -s |wc -l'))
  41. def getTotalCommits(self):
  42. return int(commands.getoutput('git-rev-list --all |wc -l'))
  43. def getTotalFiles(self):
  44. files = commands.getoutput('git-ls-files |wc -l')
  45. return int(files)
  46. pass
  47. class ReportCreator:
  48. def __init__(self):
  49. pass
  50. def create(self, data, path):
  51. self.data = data
  52. self.path = path
  53. class HTMLReportCreator(ReportCreator):
  54. def create(self, data, path):
  55. ReportCreator.create(self, data, path)
  56. f = open(path + "/index.html", 'w')
  57. f.write("""<html>
  58. <head>
  59. <title>StatGit</title>
  60. </head>
  61. <body>
  62. """)
  63. f.write('<h1>StatGit</h1>')
  64. f.write('<dl>');
  65. f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime('%Y-%m-%d %H:%m:%S'));
  66. f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % ('0000-00-00', '0000-00-00'))
  67. f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
  68. f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
  69. f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
  70. f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
  71. f.write('</dl>');
  72. f.write('<h2>Authors</h2>')
  73. f.write('<table class="authors">')
  74. f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
  75. for author in data.getAuthors():
  76. info = data.getAuthorInfo(author)
  77. 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']))
  78. f.write('</table>')
  79. f.write('</body>\n</html>');
  80. f.close()
  81. pass
  82. usage = """
  83. Usage: statgit [options] <gitpath> <outputpath>
  84. Options:
  85. -o html
  86. """
  87. if len(sys.argv) < 3:
  88. print usage
  89. sys.exit(0)
  90. gitpath = sys.argv[1]
  91. outputpath = sys.argv[2]
  92. print 'Git path: %s' % gitpath
  93. print 'Output path: %s' % outputpath
  94. os.chdir(gitpath)
  95. data = GitDataCollector()
  96. data.collect(gitpath)
  97. report = HTMLReportCreator()
  98. report.create(data, outputpath)