add CSV output format

This commit is contained in:
Alexandre Dulaunoy 2012-05-26 14:25:28 +02:00
parent f88c379221
commit f9a1fd3920

View file

@ -35,7 +35,7 @@ def gitlog(location = None):
l.append(line) l.append(line)
return l return l
def logmessage(name = None, email = None, when = None, message = None, repo = None, commitfactor = 4): def logmessage(name = None, email = None, when = None, message = None, repo = None, commitfactor = 4, format="text"):
logmessage = "" logmessage = ""
t=datetime.datetime.fromtimestamp(when) t=datetime.datetime.fromtimestamp(when)
d=t-datetime.timedelta(hours=commitfactor) d=t-datetime.timedelta(hours=commitfactor)
@ -47,9 +47,12 @@ def logmessage(name = None, email = None, when = None, message = None, repo = No
else: else:
projecttime[projectkey] = commitfactor projecttime[projectkey] = commitfactor
if format == "text":
logmessage = "From "+d.ctime()+" to "+t.ctime() + "\n" logmessage = "From "+d.ctime()+" to "+t.ctime() + "\n"
logmessage += " "+name+" ("+email+") worked on "+ repo +"\n" logmessage += " "+name+" ("+email+") worked on "+ repo +"\n"
logmessage += " and did the following: "+ message +"\n" logmessage += " and did the following: "+ message
elif format == "csv":
logmessage = d.ctime()+"|"+t.ctime()+"|"+unicode(commitfactor)+"|"+name+"|"+message
return logmessage return logmessage
usage = "usage: %s path_to_git_repos" % sys.argv[0] usage = "usage: %s path_to_git_repos" % sys.argv[0]
@ -57,6 +60,7 @@ parser = OptionParser(usage)
parser.add_option("-d", "--debug", action="store_true", dest="debug", help="output debug messages", default=False) parser.add_option("-d", "--debug", action="store_true", dest="debug", help="output debug messages", default=False)
parser.add_option("-w", "--commitfactor", dest="commitfactor", help="work time factor per commit, default is 4 hours",default=4, type="int") parser.add_option("-w", "--commitfactor", dest="commitfactor", help="work time factor per commit, default is 4 hours",default=4, type="int")
parser.add_option("-t", "--total", action="store_true", dest="total", help="total hours worked for each user per repository/project", default=False) parser.add_option("-t", "--total", action="store_true", dest="total", help="total hours worked for each user per repository/project", default=False)
parser.add_option("-f", "--outputformat", dest="format", help="output format text, csv (default is text)", default="text", type="string")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
@ -72,9 +76,13 @@ for repo in args:
(name, email, when, message) = line.split("|") (name, email, when, message) = line.split("|")
except: except:
continue continue
print logmessage(name=unicode(name), email=email, when=float(when), commitfactor = options.commitfactor, message=unicode(message), repo = os.path.basename(os.path.normpath(repo))) print logmessage(name=unicode(name), email=email, when=float(when), commitfactor = options.commitfactor, message=unicode(message), repo = os.path.basename(os.path.normpath(repo)), format=options.format)
if options.total: if options.total:
for x in projecttime: for x in projecttime:
if options.format == "text":
print x + "->" + unicode(projecttime[x])+" hours." print x + "->" + unicode(projecttime[x])+" hours."
elif options.format == "csv":
print x.split(":")[0]+"|"+x.split(":")[1]+"|"+unicode(projecttime[x])