import sys import re import termcolor SIZE = sys.argv[1] FILE = open(sys.argv[2], 'a') if len(sys.argv) >= 3 else None COLORS = {"data": "red", "hole": "green", "d": "cyan", "h": "yellow"} def write_log(text, color=None, attrs=[], end=""): if FILE: FILE.write(text + end) termcolor.cprint(text, color=color, attrs=attrs, end=end) def print_allocation(prefix, name, filetype, extra): write_log(prefix, color="white", attrs=["dark"]) write_log(name, color=COLORS[filetype], attrs=["bold"]) write_log(extra, color="white", attrs=["dark"]) def print_allocation_done(done, time, end): write_log(done, color="white", attrs=["dark"]) write_log(time, color="blue", attrs=["bold"]) write_log(end, color="white", attrs=["dark"]) def print_reading(prefix, name, precache, cached, postcache): ccolor = "yellow" if cached == "uncached" else "green" write_log(prefix, color="white", attrs=["dark"]) write_log(name, color=COLORS[filetype], attrs=["bold"]) write_log(precache, color="white", attrs=["dark"]) write_log(cached, color=ccolor, attrs=["bold"]) write_log(postcache, color="white", attrs=["dark"]) write_log(" ...", color="white", attrs=["dark"]) def print_stats_1(walltime, speed): write_log(" ") write_log(walltime, color="blue", attrs=["bold"]) write_log(", ", color="white", attrs=["dark"]) write_log(speed, color="green", attrs=["bold"]) write_log(", ", color="white", attrs=["dark"]) def print_stats_2(kerntime, kernunits, cpu): write_log(kerntime, color="cyan", attrs=["bold"], end=" ") write_log(kernunits, color="cyan", attrs=["bold"]) write_log(", ", color="white", attrs=["dark"]) write_log(cpu, color="red", attrs=["bold"]) def print_iteration(prefix, cur, sep, total): write_log(prefix, color="magenta", attrs=["bold", "underline"]) write_log(cur, color="green", attrs=["bold", "underline"]) write_log(sep, color="magenta", attrs=["bold", "underline"]) write_log(total, color="blue", attrs=["bold", "underline"], end="\n") text = "" while c := sys.stdin.read(1): text += c line = text text = "" if match := re.match(f"(Allocating file: )({SIZE}M-(data|hole|mixed-\d+(d|h)))( ...)", line): filetype = match.group(3) if match.group(4) is None else match.group(4) print_allocation(match.group(1), match.group(2), filetype, match.group(5)) elif match := re.match(r"( \[Done: )(\d+\.\d+s)(\]\n)", line): print_allocation_done(match.group(1), match.group(2), match.group(3)) elif match := re.match(f"(Reading: )({SIZE}M-(data|hole|mixed-\d+(d|h)))( \()((un)?cached)( on \w*\))\n", line): filetype = match.group(3) if match.group(4) is None else match.group(4) print_reading(match.group(1), match.group(2), match.group(5), match.group(6), match.group(8)) elif match := re.match(r"\d+ bytes \([\w,\. ]*\) copied, (\d+\.\d+ s), ((\d+ MB/s)|(\d\.\d GB/s))\n", line): print_stats_1(match.group(1), match.group(2)) elif match := re.match("(\d+\.\d+)(s kern), (\d+% cpu)", line): print_stats_2(match.group(1), match.group(2), match.group(3)) elif match := re.match(r"\d+\+\d+ records (in|out)\n", line): pass elif match := re.match("(Iteration )(\d+)( / )(\d+)\n", line): print_iteration(match.group(1), match.group(2), match.group(3), match.group(4)) elif c == "\n": write_log(line) else: text = line