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