Last active
December 18, 2015 08:29
-
-
Save tbl3rd/5754061 to your computer and use it in GitHub Desktop.
Sugar a dict with object syntax.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Work around the usual object orientation problem with the | |
# "Get out of Python free!" card. | |
# | |
class Oops: | |
def __init__(self, **stuff): | |
self.__dict__.update(stuff) | |
def start_capture_subprocesses(logdir): | |
""" | |
Start a console capture subprocess for each node in configstore, | |
all writing into logdir and return them. | |
""" | |
subs = [] | |
stamp = time.strftime("%Y-%m-%d--%H-%M-%S") | |
for node in configstore.nodes(): | |
bmc = configstore.node_hostname(node, "-bmc") | |
stderr, stdout = make_capture_files(bmc, logdir, stamp) | |
suspender = logdir + '/' + bmc + '.suspend' | |
start = make_monitor_sol_subprocess_starter(bmc, stderr, stdout, 99) | |
stuff = { 'bmc' : bmc, | |
'suspend' : make_capture_suspend(bmc, suspender), | |
'suspended' : make_capture_suspended(suspender, stderr), | |
'popen' : start(), | |
'start' : start } | |
subs.append(Oops(**stuff)) | |
return subs | |
def monitor_subprocesses(subs): | |
""" | |
Restart any restartable subprocesses in subs that have exited. | |
""" | |
for sub in subs: | |
if not sub.suspended(): | |
if sub.popen: | |
code = sub.popen.poll() | |
if code: | |
flushout("%s console capture exited with %d\n" | |
% (sub.bmc, code)) | |
if code > 0 or -code in [signal.SIGSEGV, signal.SIGABRT]: | |
sub.popen = sub.start() | |
else: | |
sub.suspend() | |
def main(av): | |
if len(av) == 2: | |
logdir = av[1] | |
if not os.path.exists(logdir): | |
os.makedirs(logdir) | |
subs = start_capture_subprocesses(logdir) | |
def monitor(): monitor_subprocesses(subs) | |
def terminate(): terminate_subprocesses(subs) | |
commit_suicide_soon_after_midnight(monitor, terminate) | |
return 0 | |
else: | |
show_usage(av[0]) | |
return 1 | |
if __name__ == "__main__": | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment