-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathgenerate_coverage.py
executable file
·197 lines (138 loc) · 3.71 KB
/
generate_coverage.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
#
# Imports
import sys
from optparse import OptionParser
import os
import re
#
# Globals and constants
EXCLUDE_BASE = [
'MixedContainer',
'_MemberSpec',
]
PATTERN = "^class\s*(\w*)"
RE_PATTERN = re.compile(PATTERN)
#
# Functions for external use
def generate_coverage(outfile, infilename, options):
exclude_classes = EXCLUDE_BASE
exclude_classes.extend(options.exclude_additional.split())
wrt = outfile.write
mod_name = get_mod_name(infilename)
wrt(HEADER % (mod_name, ))
generate_coverage_1(wrt, infilename, mod_name, exclude_classes)
wrt(FOOTER)
def generate_coverage_1(wrt, infilename, mod_name, exclude_classes):
infile = open(infilename, 'r')
for line in infile:
mo = RE_PATTERN.search(line)
if mo:
name = mo.group(1)
if (name not in exclude_classes and
not name.startswith('Sax')):
wrt(" '%s': %s.%s,\n" % (name, mod_name, name, ))
infile.close()
def get_mod_name(infilename):
s1 = os.path.split(infilename)[1]
s2 = os.path.splitext(s1)[0]
return s2
#
# Classes
#
# Functions for internal use and testing
#
# Templates
HEADER = '''\
#!/usr/bin/env python
#
# Imports
import sys
from optparse import OptionParser
import %s
#
# Globals and constants
CLASSES = {
'''
FOOTER = '''\
}
#
# Functions for external use
#
# Classes
#
# Functions for internal use and testing
def test(verbose):
instances = []
for name, class_ in CLASSES.iteritems():
instance = class_()
instances.append(instance)
return instances
USAGE_TEXT = """
python %prog [options] <somefile.xxx>
example:
python %prog somefile.xxx"""
def usage(parser):
parser.print_help()
sys.exit(1)
def main():
parser = OptionParser(USAGE_TEXT)
parser.add_option("-v", "--verbose", action="store_true",
dest="verbose", default=False,
help="produce additional (verbose) output")
(options, args) = parser.parse_args()
if len(args) != 0:
usage(parser)
test(options.verbose)
if __name__ == "__main__":
#import pdb; pdb.set_trace()
main()
'''
# End Templates
#
USAGE_TEXT = """
Generate a dictionary of class names and classes from a (superclass)
module generated by generateDS.py.
python %prog [options] <somefile.py>
Example:
python %prog somefile.py"""
def usage(parser):
parser.print_help()
sys.exit(1)
def main():
parser = OptionParser(USAGE_TEXT)
parser.add_option("-x", "--exclude-additional", type="string",
dest="exclude_additional", default='',
help="additional class names to be excluded (blank separated).")
parser.add_option("-f", "--force", action="store_true",
dest="force", default=False,
help="force over-write of outfile without asking.")
(options, args) = parser.parse_args()
outfilename = None
if len(args) == 2:
infilename = args[0]
outfilename = args[1]
elif len(args) == 1:
infilename = args[0]
outfilename = None
else:
usage(parser)
if outfilename is None:
outfile = sys.stdout
else:
if os.path.exists(outfilename):
if options.force:
outfile = open(outfilename, 'w')
else:
sys.stderr.write('Outfile (%s) exists. '
'Use -f (or --force) to override.\n' %
(outfilename, ))
sys.exit(1)
else:
outfile = open(outfilename, 'w')
generate_coverage(outfile, infilename, options)
if outfilename is not None:
outfile.close()
if __name__ == "__main__":
#import pdb; pdb.set_trace()
main()