From 570dcc8ab45db2268188b4fe0d3af06cffb6cd9d Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Fri, 8 Nov 2013 21:03:28 +0200 Subject: [PATCH] threaded transfer window --- aerofs/aerofs-transfers | 79 ++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/aerofs/aerofs-transfers b/aerofs/aerofs-transfers index 90c5ff0..361f00f 100755 --- a/aerofs/aerofs-transfers +++ b/aerofs/aerofs-transfers @@ -5,9 +5,9 @@ from datetime import datetime from datetime import timedelta import re,signal -import subprocess +import subprocess,threading -VERSION=1 +VERSION=2 W= '30' R= '31' @@ -71,7 +71,6 @@ def colorize(string): return string - def count_running(string, stats): ''' Counts the running executions ''' @@ -138,9 +137,7 @@ def str_short(s,stats): def print_stats(stats): ''' Prints logged errors, and the status line ''' #sys.stdout.write(SAVE) - - for e in range(2): - sys.stdout.write(pos(e+1,0)+CLRLN) + e=5 sys.stdout.write(pos(e+1,0)+"="*10+"AeroFS Transfers"+"="*10+" "+human_time()+CLRLN) if (stats['running']): sys.stdout.write(pos(e+2,0)+"Last update: "+stats['running'][0][0]+CLRLN) @@ -158,6 +155,22 @@ def print_stats(stats): sys.stdout.write(DOWN+CLRBLN+CLRLN) #sys.stdout.write(LOAD) + +def print_activities(): + ''' Prints activity log ''' + + try: + activities = Threaded("aerofs-sh activities -c 5") + activities.start() + activities.join() + for e,l in enumerate(activities.readstdout().strip().split('\n')): + sys.stdout.write(pos(e+1,0)+l+CLRLN) + except KeyboardInterrupt: + pass + except TypeError: + activities.stop() + activities.join() + def human_time(): t=datetime.now().strftime("%I:%M:%S %p") @@ -175,41 +188,81 @@ def termsize(): rows, columns = os.popen('stty size', 'r').read().split() return (int(rows),int(columns)) +class Threaded(threading.Thread): + def __init__(self,command): + self.stdout = None + self.stderr = None + self.command=command + self.p = None + threading.Thread.__init__(self) + + def run(self): + self.p = subprocess.Popen(self.command.split(), + shell=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + def readline(self): + try: + line = self.p.stdout.readline() + #line=lf.readline() + return line + except: + return "CleanerTimeout" + + def readstdout(self): + self.stdout, self.stderr = self.p.communicate() + return self.stdout + + def stop(self): + self.p.terminate() options=setup_options() -stats={'time':datetime.now(), +stats={'time':datetime.now()-timedelta(seconds=60), 'running':[], 'files':[], 'size': termsize()} sys.stdout.write(CLR+pos(0,0)+"Launching...") -proc = subprocess.Popen(['aerofs-sh','transfers'],stdout=subprocess.PIPE) +#proc = subprocess.Popen(['aerofs-sh','transfers'],stdout=subprocess.PIPE) +transfers = Threaded("aerofs-sh transfers") +transfers.start() +for e in range(5): + sys.stdout.write(pos(e+1,0)+CLRLN) while 1: try: sys.stdout.flush() # set a 3 second timeout for the line read. - signal.signal(signal.SIGALRM, readinput) + signal.signal(signal.SIGALRM, transfers.readline) signal.alarm(3) - line=readinput(proc) + line=transfers.readline() if not line: raise EndProgram + + if ( datetime.now() - stats['time'] > timedelta(seconds=30) ): + print_activities() + stats['time'] = datetime.now() + + stats=remove_running(stats) + stats=count_running(line,stats) # timeout returns a special string, in this case we re-read if line=="CleanerTimeout": print_stats(stats) continue - stats=count_running(line,stats) - stats=remove_running(stats) - # store only maximum number of error lines # if line empty, read next if line.strip()=="": continue print_stats(stats) except EndProgram,KeyboardInterrupt: + transfers.stop() + transfers.join() + sys.stdout.write(DOWN+'\n') sys.stdout.flush() + sys.exit(0)