More todo items and stubs.
[gitstats.git] / statgit
blobf4172048b37fefcca6bf1ad3dd6f2fa1dc88bdef
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 def getoutput(cmd):
11 print '>> %s' % cmd
12 output = commands.getoutput(cmd)
13 return output
15 class DataCollector:
16 def __init__(self):
17 pass
20 # This should be the main function to extract data from the repository.
21 def collect(self, dir):
22 self.dir = dir
25 # : get a dictionary of author
26 def getAuthorInfo(self, author):
27 return None
30 # Get a list of authors
31 def getAuthors(self):
32 return []
34 def getFirstCommitDate(self):
35 return datetime.datetime.now()
37 def getLastCommitDate(self):
38 return datetime.datetime.now()
40 def getTags(self):
41 return []
43 def getTotalAuthors(self):
44 return -1
46 def getTotalCommits(self):
47 return -1
49 def getTotalFiles(self):
50 return -1
52 def getTotalLOC(self):
53 return -1
55 class GitDataCollector(DataCollector):
56 def collect(self, dir):
57 DataCollector.collect(self, dir)
59 self.total_authors = int(getoutput('git-log |git-shortlog -s |wc -l'))
60 self.total_commits = int(getoutput('git-rev-list --all |wc -l'))
61 self.total_files = int(getoutput('git-ls-files |wc -l'))
62 self.total_lines = int(getoutput('git-ls-files |xargs cat |wc -l'))
64 def getAuthorInfo(self, author):
65 commits = int(getoutput('git-rev-list --all --author="%s" |wc -l' % author))
66 commits_frac = (100 * float(commits)) / self.getTotalCommits()
67 date_first = '0000-00-00'
68 date_last = '0000-00-00'
69 rev_last = getoutput('git-rev-list --all --author="%s" -n 1' % author)
70 rev_first = getoutput('git-rev-list --all --author="%s" |tail -n 1' % author)
71 date_first = self.revToDate(rev_first)
72 date_last = self.revToDate(rev_last)
74 res = { 'commits': commits, 'commits_frac': commits_frac, 'date_first': date_first, 'date_last': date_last }
75 return res
77 def getAuthors(self):
78 lines = getoutput('git-rev-list --all --pretty=format:%an |grep -v ^commit |sort |uniq')
79 return lines.split('\n')
81 def getTags(self):
82 lines = getoutput('git-show-ref --tags |cut -d/ -f3')
83 return lines.split('\n')
85 def getTagDate(self, tag):
86 return self.revToDate('tags/' + tag)
88 def getTotalAuthors(self):
89 return self.total_authors
91 def getTotalCommits(self):
92 return self.total_commits
94 def getTotalFiles(self):
95 return self.total_files
97 def getTotalLOC(self):
98 return self.total_lines
100 def revToDate(self, rev):
101 stamp = int(getoutput('git-log --pretty=format:%%at "%s" -n 1' % rev))
102 return datetime.datetime.fromtimestamp(stamp).strftime('%Y-%m-%d')
104 class ReportCreator:
105 def __init__(self):
106 pass
108 def create(self, data, path):
109 self.data = data
110 self.path = path
112 class HTMLReportCreator(ReportCreator):
113 def create(self, data, path):
114 ReportCreator.create(self, data, path)
116 f = open(path + "/index.html", 'w')
117 f.write("""<html>
118 <head>
119 <title>StatGit</title>
120 <link rel="stylesheet" href="statgit.css" type="text/css" />
121 </head>
122 <body>
123 """)
124 format = '%Y-%m-%d %H:%m:%S'
126 f.write('<h1>StatGit</h1>')
128 f.write('<dl>');
129 f.write('<dt>Generated</dt><dd>%s</dd>' % datetime.datetime.now().strftime(format));
130 f.write('<dt>Report Period</dt><dd>%s to %s</dd>' % (data.getFirstCommitDate().strftime(format), data.getLastCommitDate().strftime(format)))
131 f.write('<dt>Total Files</dt><dd>%s</dd>' % data.getTotalFiles())
132 f.write('<dt>Total Lines of Code</dt><dd>%s</dd>' % data.getTotalLOC())
133 f.write('<dt>Total Commits</dt><dd>%s</dd>' % data.getTotalCommits())
134 f.write('<dt>Authors</dt><dd>%s</dd>' % data.getTotalAuthors())
135 f.write('</dl>');
137 f.write("""<ul>
138 <li><a href="activity.html">Activity</a></li>
139 <li><a href="authors.html">Authors</a></li>
140 <li><a href="files.html">Files</a></li>
141 </ul>
142 """)
144 f.write('<h2>Authors</h2>')
146 f.write('<table class="authors">')
147 f.write('<tr><th>Author</th><th>Commits (%)</th><th>First commit</th><th>Last commit</th></tr>')
148 for author in data.getAuthors():
149 info = data.getAuthorInfo(author)
150 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']))
151 f.write('</table>')
153 f.write('<h2>Tags</h2>')
154 f.write('<table>')
155 f.write('<tr><th>Name</th><th>Date</th><th>Developers</th></tr>')
156 for tag in data.getTags():
157 f.write('<tr><td>%s</td><td></td></tr>' % tag)
158 f.write('</table>')
160 f.write('</body>\n</html>');
161 f.close()
162 pass
164 usage = """
165 Usage: statgit [options] <gitpath> <outputpath>
167 Options:
168 -o html
171 if len(sys.argv) < 3:
172 print usage
173 sys.exit(0)
175 gitpath = sys.argv[1]
176 outputpath = sys.argv[2]
178 print 'Git path: %s' % gitpath
179 print 'Output path: %s' % outputpath
181 os.chdir(gitpath)
183 print 'Collecting data...'
184 data = GitDataCollector()
185 data.collect(gitpath)
187 print 'Generating report...'
188 report = HTMLReportCreator()
189 report.create(data, outputpath)