Add an introductory comment.
[git-dm.git] / csvdump.py
blobc3f6b5ae2e7526c063e5388d48fc225bcd9753b9
2 # aggregate per-month statistics for people
4 import sys, datetime
5 import csv
7 class CSVStat:
8 def __init__ (self, name, email, employer, date):
9 self.name = name
10 self.email = email
11 self.employer = employer
12 self.added = self.removed = self.changesets = 0
13 self.date = date
14 def accumulate (self, p):
15 self.added = self.added + p.added
16 self.removed = self.removed + p.removed
17 self.changesets += 1
19 PeriodCommitHash = { }
21 def AccumulatePatch (p, Aggregate):
22 if (Aggregate == 'week'):
23 date = "%.2d-%.2d"%(p.date.isocalendar()[0], p.date.isocalendar()[1])
24 elif (Aggregate == 'year'):
25 date = "%.2d"%(p.date.year)
26 else:
27 date = "%.2d-%.2d-01"%(p.date.year, p.date.month)
28 authdatekey = "%s-%s"%(p.author.name, date)
29 if authdatekey not in PeriodCommitHash:
30 empl = p.author.emailemployer (p.email, p.date)
31 stat = CSVStat (p.author.name, p.email, empl, date)
32 PeriodCommitHash[authdatekey] = stat
33 else:
34 stat = PeriodCommitHash[authdatekey]
35 stat.accumulate (p)
37 ChangeSets = []
38 FileTypes = []
40 def store_patch(patch):
41 if not patch.merge:
42 employer = patch.author.emailemployer(patch.email, patch.date)
43 employer = employer.name.replace('"', '.').replace ('\\', '.')
44 author = patch.author.name.replace ('"', '.').replace ('\\', '.')
45 author = patch.author.name.replace ("'", '.')
46 try:
47 domain = patch.email.split('@')[1]
48 except:
49 domain = patch.email
50 ChangeSets.append([patch.commit, str(patch.date),
51 patch.email, domain, author, employer,
52 patch.added, patch.removed])
53 for (filetype, (added, removed)) in patch.filetypes.iteritems():
54 FileTypes.append([patch.commit, filetype, added, removed])
57 def save_csv (prefix='data'):
58 # Dump the ChangeSets
59 if len(ChangeSets) > 0:
60 fd = open('%s-changesets.csv' % prefix, 'w')
61 writer = csv.writer (fd, quoting=csv.QUOTE_NONNUMERIC)
62 writer.writerow (['Commit', 'Date', 'Domain',
63 'Email', 'Name', 'Affliation',
64 'Added', 'Removed'])
65 for commit in ChangeSets:
66 writer.writerow(commit)
68 # Dump the file types
69 if len(FileTypes) > 0:
70 fd = open('%s-filetypes.csv' % prefix, 'w')
71 writer = csv.writer (fd, quoting=csv.QUOTE_NONNUMERIC)
73 writer.writerow (['Commit', 'Type', 'Added', 'Removed'])
74 for commit in FileTypes:
75 writer.writerow(commit)
79 def OutputCSV (file):
80 if file is None:
81 return
82 writer = csv.writer (file, quoting=csv.QUOTE_NONNUMERIC)
83 writer.writerow (['Name', 'Email', 'Affliation', 'Date',
84 'Added', 'Removed', 'Changesets'])
85 for date, stat in PeriodCommitHash.items():
86 # sanitise names " is common and \" sometimes too
87 empl_name = stat.employer.name.replace ('"', '.').replace ('\\', '.')
88 author_name = stat.name.replace ('"', '.').replace ('\\', '.')
89 writer.writerow ([author_name, stat.email, empl_name, stat.date,
90 stat.added, stat.removed, stat.changesets])
92 __all__ = [ 'AccumulatePatch', 'OutputCSV', 'store_patch' ]