diff --git a/doc/README b/doc/README index c7f64e4..718ea87 100644 --- a/doc/README +++ b/doc/README @@ -1,7 +1,7 @@ gitstats is a statistics generator for git repositories. It is mostly intended for developers, as a way to check some development statistics for a project. -Currently it produces only HTML output with tables and graphs. +Currently it produces HTML output with tables and graphs and pretty JSON. Requirements ============ diff --git a/gitstats b/gitstats index 48cbf1e..5140d10 100755 --- a/gitstats +++ b/gitstats @@ -4,6 +4,7 @@ import datetime import getopt import glob +import json import os import pickle import platform @@ -25,6 +26,7 @@ os.environ['LC_ALL'] = 'C' GNUPLOT_COMMON = 'set terminal png transparent size 640,240\nset size 1.0,1.0\n' ON_LINUX = (platform.system() == 'Linux') WEEKDAYS = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') +JSONFILE = 'gitstats.json' exectime_internal = 0.0 exectime_external = 0.0 @@ -728,6 +730,24 @@ def html_header(level, text): name = html_linkify(text) return '\n%s\n\n' % (level, name, name, text, level) +class GitDataCollectorJSONEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, set): + return list(obj) + if isinstance(obj, datetime.timedelta): + return str(obj) + if isinstance(obj, GitDataCollector): + return obj.__dict__ + # Let the base class default method raise the TypeError + return json.JSONEncoder.default(self, obj) + +class JSONReportCreator(ReportCreator): + def create(self, data, filename): + f = open(filename, 'w') + json.dump(data, f, indent=True, + cls=GitDataCollectorJSONEncoder) + f.close() + class HTMLReportCreator(ReportCreator): def create(self, data, path): ReportCreator.create(self, data, path) @@ -1472,10 +1492,14 @@ class GitStats: os.chdir(rundir) - print 'Generating report...' + print 'Generating HTML report...' report = HTMLReportCreator() report.create(data, outputpath) + print 'Generating JSON report...' + report = JSONReportCreator() + report.create(data, os.path.join(outputpath, JSONFILE)) + time_end = time.time() exectime_internal = time_end - time_start print 'Execution time %.5f secs, %.5f secs (%.2f %%) in external commands)' % (exectime_internal, exectime_external, (100.0 * exectime_external) / exectime_internal) @@ -1483,9 +1507,9 @@ class GitStats: print 'You may now run:' print print ' sensible-browser \'%s\'' % os.path.join(outputpath, 'index.html').replace("'", "'\\''") + print ' sensible-notepad \'%s\'' % os.path.join(outputpath, JSONFILE).replace("'", "'\\''") print if __name__=='__main__': g = GitStats() g.run(sys.argv[1:]) -