-
Notifications
You must be signed in to change notification settings - Fork 10
/
plz
executable file
·103 lines (72 loc) · 2.43 KB
/
plz
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
97
98
99
100
101
102
103
#!/usr/bin/env python2
# hodge podge of utility stuff for use in the Makefile that used to be done with
# shell bits, but that windows compat tho
from __future__ import print_function, unicode_literals
import datetime
import glob
import os
import re
import subprocess
import sys
def main(sysargs=sys.argv[:]):
top = os.environ.get('TOP',
os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
commands = {
'clean': _clean,
'copyright': _print_copyright,
'date': _print_date,
'test-count': _print_test_count
}
if len(sysargs) < 2 or sysargs[1] in ('-h', '--help'):
print('Usage: {} <{}> [whatever]'.format(
os.path.basename(sysargs[0]), '|'.join(commands.keys()))
)
return 1
func = commands[sysargs[1]]
return func(top, sysargs[1:])
def _clean(top, sysargs):
for filename in _get_cleanable_files(top):
print('clean: removing path {!r}'.format(filename), file=sys.stderr)
os.remove(filename)
return 0
def _get_cleanable_files(top):
for candidate in glob.iglob(os.path.join(top, 'coverage.*')):
yield candidate
for candidate in glob.iglob(os.path.join(top, 'gfmrun-*-*-*')):
yield candidate
def _print_copyright(top, _):
with open(os.path.join(top, 'LICENSE')) as infile:
for line in infile.readlines():
if line.lower().startswith('copyright'):
print(re.sub('[Cc]opyright ', '', line.strip()))
return 0
def _print_date(*_):
print(datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S%z'))
return 0
def _print_test_count(top, sysargs):
filenames = sysargs[1:]
if len(filenames) == 0:
filenames = [os.path.join(top, 'README.md')]
total = 0
pattern = _get_frobs_pattern(top)
for filename in filenames:
with open(filename) as infile:
total += _count_examples(pattern, infile.readlines())
print(total)
return 0
def _get_frobs_pattern(top):
frobs = [
frob.strip() for frob in subprocess.check_output([
'go', 'run',
os.path.join(top, 'cmd', 'gfmrun', 'main.go'), 'list-frobs'
]).decode('utf-8').splitlines()
]
return re.compile('^``` ({})'.format('|'.join(frobs)))
def _count_examples(pattern, lines):
n = 0
for line in lines:
if pattern.match(line):
n += 1
return n
if __name__ == '__main__':
sys.exit(main())