forked from mgalloy/mgunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idlroutines.py
executable file
·134 lines (111 loc) · 3.09 KB
/
idlroutines.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
#!/usr/bin/env python
import getopt
import os
import re
import sys
header = '''function %s::init, _extra=e
compile_opt strictarr
if (~self->MGutTestCase::init(_extra=e)) then return, 0
'''
footer = '''
return, 1
end
'''
def print_routines(functions, procedures):
if procedures:
print 'procedures:'
for p in procedures:
print ' %s' % p
if functions:
print 'functions:'
for f in functions:
print ' %s' % f
def print_testing_routine(routines, is_function):
if routines:
for i, r in enumerate(routines):
if i == 0:
prefix = ' self->addTestingRoutine, ['
else:
prefix = ' '
if i == len(routines) - 1:
if is_function:
suffix = '], /is_function'
else:
suffix = ']'
else:
suffix = ', $'
print "%s'%s'%s" % (prefix, r, suffix)
def print_testing_routines(functions, procedures, procedure_last, testname):
functions.reverse()
procedures.reverse()
print header % testname
if procedure_last:
print_testing_routine(procedures, False)
print_testing_routine(functions, True)
else:
print_testing_routine(functions, True)
print_testing_routine(procedures, False)
print footer
def determine_testname(filename):
basename = os.path.basename(filename)
parts = os.path.splitext(basename)
base = parts[0]
if base.endswith('__define'):
base = base[0:-8]
base = base + '_ut'
return base
def find_routines(filename):
regex = re.compile('(function|pro)')
functions = []
procedures = []
procedure_last = True
with open(filename, 'r') as f:
contents = f.readlines()
for line in contents:
match = regex.match(line)
if match:
args = line.split(',')
tokens = args[0].split()
if tokens[0] == 'function':
functions.append(tokens[1])
procedure_last = False
elif tokens[0] == 'pro':
procedures.append(tokens[1])
procedure_last = True
return functions, procedures, procedure_last
def usage():
print 'usage: routines.py [OPTIONS] [filename]'
print ''
print '-h|--help : print this help message'
print '-t|--testing : display mgunit code to add testing routines for this file'
print '-f |--file <arg> : specify filename to parse'
sys.exit(0)
def main(argv):
# defaults
testing = False
filename = ''
try:
opts, args = getopt.getopt(argv, "hf:t", ['help', 'file=', 'testing'])
except getopt.GetoptError:
usage()
sys.exit(2)
if len(args) > 0:
filename = args[0]
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
elif opt in ('-t', '--testing'):
testing = True
elif opt in ('-f', '--file'):
filename = arg
if len(filename) == 0:
usage()
filename = os.path.expanduser(filename)
functions, procedures, procedure_last = find_routines(filename)
if testing:
testname = determine_testname(filename)
print_testing_routines(functions, procedures, procedure_last, testname)
else:
print_routines(functions, procedures)
if __name__ == '__main__':
main(sys.argv[1:])