-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathharness.py
More file actions
executable file
·153 lines (133 loc) · 5.65 KB
/
harness.py
File metadata and controls
executable file
·153 lines (133 loc) · 5.65 KB
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
#!/usr/bin/python
from __future__ import print_function
__author__ = 'ed lane'
import sys
import jsonpickle
import __main__
FIRST = 0
LAST = 1
ALL = 2
ARGS = 'args'
KWARGS = 'kwargs'
SELF = 'self'
CLASS = 2
class Harness_globals():
harness_file_name = '/tmp/harness-' + sys.argv[0].rsplit('/', 1)[1].rsplit('.', 1)[0] + '.jsonpkl'
f = open(harness_file_name, 'a+')
f.seek(0)
json_string = f.read()
json_dict = {}
try:
json_dict = jsonpickle.loads(json_string)
except:
json_dict = {}
@staticmethod
def save():
Harness_globals.f.truncate(0)
print (jsonpickle.dumps(Harness_globals.json_dict), file=Harness_globals.f)
# text menu for replaying a function using previously saved parameters
def replay():
func_list = []
for x in sorted(Harness_globals.json_dict):
func_list.append(x)
while True:
print ('c : <continue>')
print ('q : <quit>')
print ('d : <disable menu>')
print ('--------------')
n = 0
for x in func_list:
print (n, ':', x)
n += 1
func_input = raw_input('\nenter function # to replay ===> ')
if func_input == 'c':
return
elif func_input == 'q':
exit()
elif func_input == 'd':
Harness_globals.json_dict['__enable_menu__'] = True
Harness_globals.save()
return
else:
func_int = int(func_input)
# print (Harness_globals.json_dict[func_list[func_int]])
print ('function =', func_list[func_int])
more = len(Harness_globals.json_dict[func_list[func_int]]) > 1
i = 0
for every in Harness_globals.json_dict[func_list[func_int]]:
if more:
print ('----------- ', i)
i += 1
print ('*args = ')
for each in sorted(every[ARGS]):
# print *args
print (each)
print ('**kwargs = ')
for each in sorted(every[KWARGS]):
print (each, '=', every[KWARGS][each])
func_input = raw_input('call function using these parameters??? (Y/n) (x=delete) ===> ')
if func_input == '':
# No input default to a 'Y'
func_input = 'Y'
if func_input in 'x':
del(Harness_globals.json_dict[func_list[func_int]])
del(func_list[func_int])
Harness_globals.save()
continue
if func_input in 'Yy':
module_name = func_list[func_int].split('.')[0]
function_name = func_list[func_int].split('.')[1]
if SELF in Harness_globals.json_dict[func_list[func_int]][FIRST]:
# this is an class instance
function = Harness_globals.json_dict[func_list[func_int]][FIRST][SELF][2]
else:
# this is a classless "top module" function
function = getattr(sys.modules[module_name], function_name)
args = Harness_globals.json_dict[func_list[func_int]][FIRST][ARGS]
kwargs = Harness_globals.json_dict[func_list[func_int]][FIRST][KWARGS]
print ('function = ', func_list[func_int], 'result = ', function(*args, **kwargs), '\n')
# decorator to save/restore function parameters at run-time
# This is useful for capturing function parameters and then later replaying/debugging that function in a
# symbolic debugger such as Eclipse or Pycharm
def decor_record(recall):
def func_record(func):
def inner(*args, **kwargs):
if func.func_code.co_varnames[0] == 'self':
class_name = args[0].__class__.__name__
mod_func_name = func.func_globals['__name__'] + '.' + class_name + '.' + func.__name__
Harness_globals.json_dict[mod_func_name][FIRST][SELF] = (args[0], func.func_name, func)
else:
mod_func_name = func.func_globals['__name__'] + '.' + func.__name__
if mod_func_name in Harness_globals.json_dict and recall == FIRST:
# this function's params already existed in harness file
# so return saved params...
if func.func_code.co_varnames[0] == 'self':
args = args[:1]
args += Harness_globals.json_dict[mod_func_name][FIRST][ARGS][1:]
# args = Harness_globals.json_dict[mod_func_name][FIRST][ARGS]
else:
args = Harness_globals.json_dict[mod_func_name][FIRST][ARGS]
kwargs = Harness_globals.json_dict[mod_func_name][FIRST][KWARGS]
return func(*args, **kwargs)
# first time we have seen params for this function...
# OR the decorator specified replacement on every call
# so save the params to harness file
if not Harness_globals.json_dict.has_key(mod_func_name) or recall == LAST:
Harness_globals.json_dict[mod_func_name] = []
d = {}
d[ARGS] = args
d[KWARGS] = kwargs
Harness_globals.json_dict[mod_func_name].append(d)
print ('saving... ' + mod_func_name + ' = ' + repr(d))
Harness_globals.save()
# if func.func_code.co_varnames[0] == 'self':
# Harness_globals.json_dict[mod_func_name][FIRST][SELF] = (args[0], func.func_name, func)
return func(*args, **kwargs)
return inner
return func_record
@decor_record(FIRST)
def multiply(*args):
result = 1
for x in args:
result = result * x
return result