-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_compile.py
86 lines (75 loc) · 2.83 KB
/
single_compile.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
import optparse
import os
import sys
import time
import itertools
import chatstatlib
USAGE = "%prog [options] FILE..."
options = optparse.OptionParser(usage=USAGE)
options.add_option("-v", "--verbose", action="store_true",
dest="verbose", default=False, help="verbose output")
options.add_option("-c", "--channels-only", action="store_true",
dest="channelsOnly", default=False,
help="only find channels when searching for files")
options.add_option("-s", "--stat", dest="stat", default="talk",
help="type of stat to output")
options.add_option("-l", "--limit", dest="limit", default=0, type="int",
help="Limit to how many people to show (0 for all)")
options.add_option("-f", "--format", dest="format", default="irssi",
help="Log file format")
options.add_option("-o", "--out", dest="out", default="-",
help="Output file (- for stdout)")
options.add_option("--list-stats", dest="listStats", action="store_true",
help="List all statistics analyzers and exit")
def main():
opts, args = options.parse_args()
if opts.listStats:
for name in sorted(chatstatlib.counters):
print name
return 0
if not args:
print "No file names given!"
return 1
if opts.format not in chatstatlib.formats:
print "unimplemented log format", opts.format
return 1
if opts.stat not in chatstatlib.counters:
print "unimplemented stat type", opts.stat
return 1
if opts.out == '-':
outfile = sys.stdout
else:
outfile = open(os.path.expanduser(opts.out), 'w')
# Choose log file format (irssi, weechat, bip, etc.)
formatModule = chatstatlib.formats[opts.format]
# Choose a search regexp based on --channel
if opts.channelsOnly:
fregexp = formatModule.LOG_CHANNEL_NAME_RE
else:
fregexp = formatModule.LOG_FILE_NAME_RE
fileNames = sorted(chatstatlib.findFiles(args, fregexp))
if not fileNames:
print "no log files found"
return 1
lineParserGen = formatModule.lineParser # one generator per file
counter, sink = chatstatlib.counters[opts.stat]()
messageCallbacks = [sink]
for fileName in fileNames:
if opts.verbose:
print "processing", fileName,
start = time.time()
lineParser = formatModule.lineParser()
chatstatlib.parseFile(fileName, lineParser, messageCallbacks,
formatModule.ENCODING)
if opts.verbose:
print "({0:.3} seconds)".format(time.time() - start)
rank = chatstatlib.counterToRank(counter)
if opts.limit:
iterable = itertools.islice(rank, opts.limit)
else:
iterable = rank
print >> outfile, "rank nick count"
for i, (nick, count) in zip(itertools.count(1), iterable):
print >> outfile, i, nick, count
if __name__ == "__main__":
exit(main())