-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcount.py
96 lines (82 loc) · 2.8 KB
/
wordcount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import subprocess, json, os, shutil, pytz
commits = set(subprocess.check_output(['git', 'rev-list', '--all']).split())
nameroot = 'wordcount'
txtname= nameroot + '.txt'
dbname = nameroot + '.json'
tmpdir = os.path.join(os.environ['TMPDIR'], nameroot)
try:
dbfile = open(dbname, 'r')
dbdict = json.load(dbfile)
dbfile.close()
except IOError:
print "Couldn't open %s, starting with a clean dictionary"%(dbname)
dbdict = { }
knowncommits = set(dbdict.keys())
newcommits = commits - knowncommits
for commit in newcommits:
date = subprocess.check_output(['git', 'log', '-n', '1', '--pretty=%ad',
commit]).strip()
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
os.mkdir(tmpdir)
gitcmd = subprocess.Popen(['git', 'archive', commit], stdout=subprocess.PIPE)
tarcmd = subprocess.Popen(['tar', '-x', '-C', tmpdir], stdin=gitcmd.stdout,
stdout=subprocess.PIPE)
gitcmd.stdout.close()
tarcmd.communicate()[0]
def makeandcount(filename):
makecmd = subprocess.Popen(['make', filename], cwd = tmpdir)
makecmd.wait()
if makecmd.returncode == 0:
totext = subprocess.Popen(['pdftotext', filename, '-'],
stdout = subprocess.PIPE, cwd = tmpdir)
remnum = subprocess.Popen(['egrep', '-o', '[a-zA-Z]+'],
stdout = subprocess.PIPE, stdin = totext.stdout, cwd = tmpdir)
remlet = subprocess.Popen(['egrep', '-e', r'\w\w+'],
stdout = subprocess.PIPE, stdin = remnum.stdout, cwd = tmpdir)
counter= subprocess.Popen(['wc', '-w'], stdin = remlet.stdout,
stdout = subprocess.PIPE, cwd = tmpdir)
totext.stdout.close()
count = counter.communicate()[0].strip()
try:
count = int(count)
return count
except:
raise
return None
for name in ['thesis.pdf', 'main.pdf']:
wordcount = makeandcount(name)
if wordcount is not None:
break
dbdict[commit] = {
'date' : date,
'wordcount' : wordcount
}
# Write the database
dbfile = open(dbname, 'w')
json.dump(dbdict, dbfile)
dbfile.close()
# Sort the info for output
from dateutil import parser
sortlist = [ ]
for commit, info in dbdict.iteritems():
# The git log messages contain timezone information, convert everything to UTC
dt = parser.parse(info['date']).astimezone(pytz.utc)
wc = info['wordcount']
if wc is not None:
sortlist.append((dt, wc))
sortlist.sort()
# Define the text format for dates in the output file.
def fmt(date):
return date.strftime('%Y.%m.%d %H:%M:%S')
txtfile = open(txtname, 'w')
txtfile.write('### %s ###\n'%(txtname))
lastwc = None
for date, wc in sortlist:
tformat = fmt(date)
if wc != lastwc:
txtfile.write("%s %d\n"%(tformat, wc))
lastwc = wc
txtfile.close()
# You might want to copy your results to a remote server, e.g.
#subprocess.Popen(['scp', txtname, 'example.com:wordcount_olli.txt'])