Skip to content

Instantly share code, notes, and snippets.

@t3rmin4t0r
Created October 24, 2014 07:27
Show Gist options
  • Save t3rmin4t0r/c450c92607a96de2803d to your computer and use it in GitHub Desktop.
Save t3rmin4t0r/c450c92607a96de2803d to your computer and use it in GitHub Desktop.
transpose.py
import sys,re,math,os
import curses
from time import sleep
running=re.compile(r'Status: Running \(application id: (application_[0-9_]*)\)')
tasks=re.compile(r'(Map [0-9]*|Reducer [0-9]*): ([\-0-9]*)(\(.[0-9]*\))?/([\-0-9]*)')
log = open("log", "w")
debug = lambda a: (log.write(str(a)+"\n"),log.flush())
class DisplayWindow(object):
def __init__(self):
pass
def start(self, title):
self.title = title
self.stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
self.stdscr.clear()
def update(self, values):
(y,x) = self.stdscr.getmaxyx()
y = y - 2
w = 0
self.stdscr.clear()
self.stdscr.addstr(0, 4, self.title, curses.A_UNDERLINE)
j = 0
for (i, (k, v)) in enumerate(values.items()):
complete = lambda n: n[0] != '-' and n[0] == n[1]
iforelse = lambda a,b,c: (a and b) or c
s = "%s: %s%s/%s" % (k, v[0], v[2] or "", v[1])
w = max(w, len(s))
if(i and (i % y == 0)):
j += int(w*1.5)
self.stdscr.addstr(i % y + 2, j, s, iforelse(complete(v), curses.A_DIM, curses.A_BOLD))
self.stdscr.refresh()
def end(self):
curses.echo()
curses.nocbreak()
curses.endwin()
self.stdscr = None
def main(args):
win = None
try:
stdin = os.fdopen(sys.stdin.fileno(), 'r', 100)
for l in stdin:
if not l:
break
l = l.strip()
app = running.match(l)
if app:
win = DisplayWindow()
win.start(app.group(1))
elif tasks.match(l):
t = [t.groups() for t in tasks.finditer(l.strip())]
d = dict([(k, (i, j, n)) for (k, i, n, j) in t])
win.update(d)
elif l:
if(win):
win.end()
win = None
print l
finally:
if win:
win.end()
win = None
main(sys.argv[1:])
log.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment