-
Notifications
You must be signed in to change notification settings - Fork 5
/
docs.py
119 lines (87 loc) · 3.05 KB
/
docs.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Check gambit documentation in config files
==========================================
Usage e.g.,
python docs.py capabilities extra
"""
import os
try:
from subprocess import getoutput
except ImportError:
from subprocess import check_output
getoutput = lambda command: check_output(command, shell=True)
from import_yaml import load, duplicates
try:
GAMBIT = os.environ["GAMBIT"]
except KeyError:
raise RuntimeError("You must export GAMBIT=/path/to/gambit/executable")
START = dict(models='MODEL', capabilities='CAPABILITIES')
END = ['Calling MPI_Finalize', 'GAMBIT']
EXTRA = set()
def filter_(lines, start, end):
"""
:returns: Iterator over non-trivial lines in gambit output
"""
yield_ = False
for line in lines:
if not line:
continue
elif line.startswith(start):
yield_ = True
elif any(line.startswith(e) for e in end):
break
elif yield_:
yield line
def dupes(command):
"""
:returns: Duplicated entries in gambit config
"""
file_name = "{}/config/{}.dat".format(GAMBIT, command)
with open(file_name, 'r') as yaml_file:
return duplicates(yaml_file)
def docs(command):
"""
:returns: Entries in gambit config
"""
file_name = "{}/config/{}.dat".format(GAMBIT, command)
with open(file_name, 'r') as yaml_file:
return set(load(yaml_file).keys())
def call(command):
"""
:returns: Entries found when gambit called
"""
shell = r"{}/gambit {} | sed 's/\x1b\[[0-9;]*m//g'".format(GAMBIT, command)
output = getoutput(shell)
lines = output.splitlines()
items = [line.split()[0] for line in filter_(lines, START[command], END)]
return set(items)
def missing(command):
"""
:returns: Entries that are missing in gambit docs
"""
return call(command) - docs(command)
def extra(command):
"""
:returns: Entries that are extra in gambit docs
"""
return docs(command) - call(command)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Check gambit documentation. You must export GAMBIT=...')
parser.add_argument('command', type=str, help='which documentation to check', choices=['capabilities', 'models'])
parser.add_argument('mode', type=str, help='which test to perform', choices=['missing', 'duplicates', 'extra'])
args = parser.parse_args()
if args.mode == 'missing':
missing_ = missing(args.command)
message = "The {}: {} were missing descriptions"
assert not missing_, message.format(args.command, missing_)
elif args.mode == 'extra':
extra_ = extra(args.command)
message = "The {}: {} are extra"
assert not extra_ or extra_ == EXTRA, message.format(args.command, extra_)
elif args.mode == "duplicates":
duplicates_ = dupes(args.command)
message = "The {}: {} were duplicated"
assert not duplicates_, message.format(args.command, duplicates_)
else:
raise ValueError("Unknown mode: {}".format(args.mode))