OILS / metrics / line_counts.py View on Github | oilshell.org

104 lines, 61 significant
1#!/usr/bin/env python2
2"""
3wc_html.py
4
5Filter for HTML
6"""
7from __future__ import print_function
8
9import os
10import sys
11
12
13def log(msg, *args):
14 if args:
15 msg = msg % args
16 print(msg, file=sys.stderr)
17
18
19def main(argv):
20
21 id_ = argv[1]
22 title = argv[2]
23 comment = argv[3]
24 tmp_dir = argv[4]
25
26 total_lines = None
27 rows = []
28
29 # Parse
30 num_files = 0
31 for line in sys.stdin:
32 line = line.strip()
33 count, rel_path = line.split(None, 1)
34 count = int(count)
35
36 if rel_path == 'total':
37 total_lines = count
38 else:
39 rows.append((count, rel_path))
40 num_files += 1
41
42 # Append to row
43 index_tsv = os.path.join(tmp_dir, 'INDEX.tsv')
44
45 anchor = 'i%s' % id_
46
47 # Quirk: 'wc' doesn't print total line when there's one file.
48 # TODO: Should we get rid of the terminal/text version of this report, and
49 # not use 'wc'?
50
51 if total_lines is None:
52 total_lines = rows[-1][0]
53
54 record = {
55 'id': id_,
56 'total_lines': total_lines,
57 'total_lines_str': '{:,}'.format(total_lines),
58 'num_files': num_files,
59 'title': title,
60 'anchor': anchor,
61 'comment': comment,
62 }
63
64 with open(index_tsv, 'a') as f:
65 # The link looks like #i01
66 f.write('%(title)s\t#%(anchor)s\t%(total_lines)d\t%(num_files)d\n' % record)
67
68 # Write our HTML
69 html = os.path.join(tmp_dir, '%02d.html' % int(id_))
70
71 with open(html, 'w') as f:
72 print('''\
73<a name="%(anchor)s"></a>
74<h2>%(title)s</h2>
75<p>%(comment)s</p>
76''' % record, file=f)
77
78 # Note: not using csv2html.py directly, since it's too many small files.
79
80 print('''\
81<div class="wc">
82 <pre class="counts">''', file=f)
83
84 for count, rel_path in rows:
85 count_str = '{:,}'.format(count)
86 url = 'https://github.com/oilshell/oil/blob/master/%s' % rel_path
87 print('<a href="%s">%-40s</a> %10s' % (url, rel_path, count_str), file=f)
88
89 summary = '<span class="summary">%(total_lines_str)s lines in %(num_files)d files</span>' % record
90 print('', file=f)
91 print(summary, end='', file=f)
92 print('''\
93 </pre>
94</div>''' % record, file=f)
95
96 return 0
97
98
99if __name__ == '__main__':
100 try:
101 sys.exit(main(sys.argv))
102 except RuntimeError as e:
103 print('FATAL: %s' % e, file=sys.stderr)
104 sys.exit(1)