From 71e0c80655b9c2df2334cc06f303b1863a1b63fe Mon Sep 17 00:00:00 2001 From: Foo Date: Tue, 20 Feb 2024 22:44:09 +0100 Subject: [PATCH] cosmetic changes to Monitor.py --- samples/utils/Monitor.py | 68 ++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/samples/utils/Monitor.py b/samples/utils/Monitor.py index ce1ff26..8436364 100644 --- a/samples/utils/Monitor.py +++ b/samples/utils/Monitor.py @@ -7,17 +7,18 @@ from optparse import OptionParser import time -def stripEndl(line): - if len(line) > 0 and line[-1] == '\n': - return line[:-1] - return line +def onlyOnceTrue(iterable): + firstTime = True + for element in iterable: + yield firstTime, element + firstTime = False class ProcessHandler: def __init__(self, cmd_line, location, sleep_initial=None): if not sleep_initial == None: # print('sleeping {} [s]'.format(sleep_initial)) time.sleep(float(sleep_initial)) - self.cmd = cmd_line.strip().split() + self.cmd = cmd_line.split() if not location == None: self.cmd[0] = os.path.join(location, self.cmd[0]) self.thread = threading.Thread(target=self.run_) @@ -25,12 +26,8 @@ def __init__(self, cmd_line, location, sleep_initial=None): self.stdout = '' self.stderr = '' - def to_string(self, prune): - if prune: - temp = list(self.cmd) - temp[0] = os.path.basename(temp[0]) - return ' '.join(temp) - return ' '.join(self.cmd) + def __str__(self): + return ' '.join([os.path.basename(piece) if isFirst else piece for isFirst, piece in onlyOnceTrue(self.cmd)]) def run_(self): hndlr = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@ -46,13 +43,8 @@ def get(self): return self.stdout, self.stderr def fromFile(source, location=None, sleep_in_between=None): - processes = [] with open(source, 'r') as stream: - first = True - for line in stream: - processes.append(ProcessHandler(line.strip(), location, None if first else sleep_in_between)) - first = False - return processes + return [ProcessHandler(line.strip(), location, None if firstTime else sleep_in_between) for firstTime, line in onlyOnceTrue(stream.readlines())] class Monitor: LAYOUT_TEMPLATE=""" @@ -82,36 +74,29 @@ class Monitor: """ SHELL_TEMPLATE = """ -
-
$CMD
+
+
{CMD}

-$LINES +{LINES}
""" def makeShell(process, w): - content = Monitor.SHELL_TEMPLATE - content = content.replace('$WIDTH', str(w)) - content = content.replace('$CMD', process.to_string(True)) - shell_content = '' - out, err = process.get() - for line in StringIO(out).readlines(): - shell_content += '
{}
\n'.format( stripEndl(line) ) - for line in StringIO(err).readlines(): - shell_content += '
{}
\n'.format( stripEndl(line) ) - content = content.replace('$LINES', shell_content) - return content + out, err = process.get() + shell_content = '\n'.join(['
{}
'.format( line.strip() ) for line in StringIO(out).readlines()]) + shell_content += '\n'.join(['
{}
'.format( line.strip() ) for line in StringIO(err).readlines()]) + return Monitor.SHELL_TEMPLATE.format(**{ + 'WIDTH': w, + 'CMD': str(process), + 'LINES': shell_content + }) def make(cmd_source, destination, location=None, sleep_in_between=None): procs = ProcessHandler.fromFile(cmd_source, location, sleep_in_between) - monitor_page = Monitor.LAYOUT_TEMPLATE w = int(round(100 / len(procs), 0)) - shells = '' - for proc in procs: - shells += '{}\n'.format( Monitor.makeShell(proc, w) ) - monitor_page = monitor_page.replace('$SHELLS', shells) + shells = '\n'.join([Monitor.makeShell(proc, w) for proc in procs]) with open(destination, 'w') as stream: - stream.write(monitor_page) + stream.write(Monitor.LAYOUT_TEMPLATE.replace('$SHELLS', shells)) def test(): tmpdirname = tempfile.TemporaryDirectory() @@ -125,12 +110,8 @@ def test(): print('open in a browser {}'.format(output)) def monitor(options): - try: - Monitor.make(options.cmd, options.dest, options.location, options.sleep) - print() - print('open in a browser {} to see the results sent by the run processes'.format(options.dest)) - except: - sys.exit(1) + Monitor.make(options.cmd, options.dest, options.location, options.sleep) + print('\nopen in a browser {} to see the results sent by the run processes'.format(options.dest)) def main(): parser = OptionParser() @@ -155,6 +136,5 @@ def main(): print(''.join(['Running {}'.format(line) for line in stream.readlines()])) print('\n\nwaiting for all the spawned processes to complete ...\n\n') - if __name__ == '__main__': main()