forked from autotest/autotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubcommand.py
263 lines (216 loc) · 8.34 KB
/
subcommand.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
__author__ = """Copyright Andy Whitcroft, Martin J. Bligh - 2006, 2007"""
import sys, os, time, signal, cPickle, logging
from autotest.client.shared import error, utils
# entry points that use subcommand must set this to their logging manager
# to get log redirection for subcommands
logging_manager_object = None
def parallel(tasklist, timeout=None, return_results=False):
"""
Run a set of predefined subcommands in parallel.
@param tasklist: A list of subcommand instances to execute.
@param timeout: Number of seconds after which the commands should timeout.
@param return_results: If True instead of an AutoServError being raised
on any error a list of the results|exceptions from the tasks is
returned. [default: False]
"""
run_error = False
for task in tasklist:
task.fork_start()
remaining_timeout = None
if timeout:
endtime = time.time() + timeout
results = []
for task in tasklist:
if timeout:
remaining_timeout = max(endtime - time.time(), 1)
try:
status = task.fork_waitfor(timeout=remaining_timeout)
except error.AutoservSubcommandError:
run_error = True
else:
if status != 0:
run_error = True
results.append(cPickle.load(task.result_pickle))
task.result_pickle.close()
if return_results:
return results
elif run_error:
message = 'One or more subcommands failed:\n'
for task, result in zip(tasklist, results):
message += 'task: %s returned/raised: %r\n' % (task, result)
raise error.AutoservError(message)
def parallel_simple(function, arglist, log=True, timeout=None,
return_results=False):
"""
Each element in the arglist used to create a subcommand object,
where that arg is used both as a subdir name, and a single argument
to pass to "function".
We create a subcommand object for each element in the list,
then execute those subcommand objects in parallel.
NOTE: As an optimization, if len(arglist) == 1 a subcommand is not used.
@param function: A callable to run in parallel once per arg in arglist.
@param arglist: A list of single arguments to be used one per subcommand;
typically a list of machine names.
@param log: If True, output will be written to output in a subdirectory
named after each subcommand's arg.
@param timeout: Number of seconds after which the commands should timeout.
@param return_results: If True instead of an AutoServError being raised
on any error a list of the results|exceptions from the function
called on each arg is returned. [default: False]
@returns None or a list of results/exceptions.
"""
if not arglist:
logging.warn('parallel_simple was called with an empty arglist, '
'did you forget to pass in a list of machines?')
# Bypass the multithreading if only one machine.
if len(arglist) == 1:
arg = arglist[0]
if return_results:
try:
result = function(arg)
except Exception, e:
return [e]
return [result]
else:
function(arg)
return
subcommands = []
for arg in arglist:
args = [arg]
if log:
subdir = str(arg)
else:
subdir = None
subcommands.append(subcommand(function, args, subdir))
return parallel(subcommands, timeout, return_results=return_results)
class subcommand(object):
fork_hooks, join_hooks = [], []
def __init__(self, func, args, subdir = None):
# func(args) - the subcommand to run
# subdir - the subdirectory to log results in
if subdir:
self.subdir = os.path.abspath(subdir)
if not os.path.exists(self.subdir):
os.mkdir(self.subdir)
self.debug = os.path.join(self.subdir, 'debug')
if not os.path.exists(self.debug):
os.mkdir(self.debug)
else:
self.subdir = None
self.debug = None
self.func = func
self.args = args
self.lambda_function = lambda: func(*args)
self.pid = None
self.returncode = None
def __str__(self):
return str('subcommand(func=%s, args=%s, subdir=%s)' %
(self.func, self.args, self.subdir))
@classmethod
def register_fork_hook(cls, hook):
""" Register a function to be called from the child process after
forking. """
cls.fork_hooks.append(hook)
@classmethod
def register_join_hook(cls, hook):
""" Register a function to be called when from the child process
just before the child process terminates (joins to the parent). """
cls.join_hooks.append(hook)
def redirect_output(self):
if self.subdir and logging_manager_object:
tag = os.path.basename(self.subdir)
logging_manager_object.tee_redirect_debug_dir(self.debug, tag=tag)
def fork_start(self):
sys.stdout.flush()
sys.stderr.flush()
r, w = os.pipe()
self.returncode = None
self.pid = os.fork()
if self.pid: # I am the parent
os.close(w)
self.result_pickle = os.fdopen(r, 'r')
return
else:
os.close(r)
# We are the child from this point on. Never return.
signal.signal(signal.SIGTERM, signal.SIG_DFL) # clear handler
if self.subdir:
os.chdir(self.subdir)
self.redirect_output()
try:
for hook in self.fork_hooks:
hook(self)
result = self.lambda_function()
os.write(w, cPickle.dumps(result, cPickle.HIGHEST_PROTOCOL))
exit_code = 0
except Exception, e:
logging.exception('function failed')
exit_code = 1
os.write(w, cPickle.dumps(e, cPickle.HIGHEST_PROTOCOL))
os.close(w)
try:
for hook in self.join_hooks:
hook(self)
finally:
sys.stdout.flush()
sys.stderr.flush()
os._exit(exit_code)
def _handle_exitstatus(self, sts):
"""
This is partially borrowed from subprocess.Popen.
"""
if os.WIFSIGNALED(sts):
self.returncode = -os.WTERMSIG(sts)
elif os.WIFEXITED(sts):
self.returncode = os.WEXITSTATUS(sts)
else:
# Should never happen
raise RuntimeError("Unknown child exit status!")
if self.returncode != 0:
print "subcommand failed pid %d" % self.pid
print "%s" % (self.func,)
print "rc=%d" % self.returncode
print
if self.debug:
stderr_file = os.path.join(self.debug, 'autoserv.stderr')
if os.path.exists(stderr_file):
for line in open(stderr_file).readlines():
print line,
print "\n--------------------------------------------\n"
raise error.AutoservSubcommandError(self.func, self.returncode)
def poll(self):
"""
This is borrowed from subprocess.Popen.
"""
if self.returncode is None:
try:
pid, sts = os.waitpid(self.pid, os.WNOHANG)
if pid == self.pid:
self._handle_exitstatus(sts)
except os.error:
pass
return self.returncode
def wait(self):
"""
This is borrowed from subprocess.Popen.
"""
if self.returncode is None:
pid, sts = os.waitpid(self.pid, 0)
self._handle_exitstatus(sts)
return self.returncode
def fork_waitfor(self, timeout=None):
if not timeout:
return self.wait()
else:
end_time = time.time() + timeout
while time.time() <= end_time:
returncode = self.poll()
if returncode is not None:
return returncode
time.sleep(1)
utils.nuke_pid(self.pid)
print "subcommand failed pid %d" % self.pid
print "%s" % (self.func,)
print "timeout after %ds" % timeout
print
return None