#!/usr/bin/python import os import re import time logdir = "log" def ifthen(cond, val1, val2, val0=None): if cond: return val1 if cond==None: return val0 return val2 def age_string(age): if age<60: return "%d s" % age if age<3600: return "%d m" % (age/60.0) if age<86400: return "%d h" % (age/3600.0) return "%d d" % (age/86400.0) def start_time(lines): string = "".join(lines) line = re.search(r"^\[(\d+-\d+-\d+ \d+:\d+:\d+)\]$", string, re.MULTILINE) if not line: return 0 try: return time.mktime(time.strptime(line.group(1), "%Y-%m-%d %H:%M:%S")) except: return 0 def end_time(lines): string = "".join(reversed(lines)) line = re.search(r"^\[(\d+-\d+-\d+ \d+:\d+:\d+)\]$", string, re.MULTILINE) if not line: return 0 try: return time.mktime(time.strptime(line.group(1), "%Y-%m-%d %H:%M:%S")) except: return 0 def build_state(lines, configuration): string = "".join(lines) if re.search(r"^ *%s +\[built .*\]" % configuration, string, re.MULTILINE): return True if re.search(r"^ *%s +\[incomplete\]" % configuration, string, re.MULTILINE): return False return None def run_state(lines): string = "".join(lines) if re.search(r"^Simulation finished successfully", string, re.MULTILINE): return True if re.search(r"^Simulation did not finish", string, re.MULTILINE): return False return None # Show all tasks have completed while True: print files = os.listdir(logdir) files = filter(lambda file: re.match(r".*\.out$", file), files) print "%s: %s machines" % (time.strftime("%Y-%m-%d %H:%M:%S"), len(files)) print (" %-10s %5s %5s %10s %s" % ("Machine", "age", "dur'n", "size", "state")) print " ============================================================================" outputs = [] for file in files: (machine, ext) = os.path.splitext(file) path = os.path.join(logdir, file) f = open(path, "r") lines = f.readlines() f.close() #duration = end_time(lines[-10]) - start_time(lines[:10]) duration = os.path.getmtime(path) - start_time(lines[:10]) age = time.time() - os.path.getmtime(path) size = os.path.getsize(path) good_conf_debug = build_state(lines[-1000:], "sim-debug") good_conf = build_state(lines[-1000:], "sim" ) good_sim = run_state(lines[-1000:]) done = len(lines)>0 and lines[-1] == "Distribution done.\n" output = (machine, age,duration, size, good_conf_debug, good_conf, good_sim, done) outputs.append(output) # sort by age outputs.sort(key=lambda output: output[1]) for output in outputs: (machine, age, duration, size, good_conf_debug, good_conf, good_sim, done) = output age_str = age_string(age) duration_str = age_string(duration) good_conf_debug_str = ifthen(good_conf_debug, " [sim-debug]", "", "") good_conf_str = ifthen(good_conf , " [sim]" , "", "") good_sim_str = ifthen(good_sim , " [success]" , "", "" ) done_str = ifthen(done, " [done]", " working...", "") state_str = "".join([good_conf_debug_str, good_conf_str, good_sim_str, done_str]) print (" %-10s %5s %5s %10d %s" % (machine, age_str, duration_str, size, state_str)) time.sleep(60)