threaded transfer window

This commit is contained in:
ville rantanen
2013-11-08 21:03:28 +02:00
parent 3cc37d143b
commit 570dcc8ab4

View File

@@ -5,9 +5,9 @@ from datetime import datetime
from datetime import timedelta from datetime import timedelta
import re,signal import re,signal
import subprocess import subprocess,threading
VERSION=1 VERSION=2
W= '30' W= '30'
R= '31' R= '31'
@@ -71,7 +71,6 @@ def colorize(string):
return string return string
def count_running(string, stats): def count_running(string, stats):
''' Counts the running executions ''' ''' Counts the running executions '''
@@ -138,9 +137,7 @@ def str_short(s,stats):
def print_stats(stats): def print_stats(stats):
''' Prints logged errors, and the status line ''' ''' Prints logged errors, and the status line '''
#sys.stdout.write(SAVE) #sys.stdout.write(SAVE)
e=5
for e in range(2):
sys.stdout.write(pos(e+1,0)+CLRLN)
sys.stdout.write(pos(e+1,0)+"="*10+"AeroFS Transfers"+"="*10+" "+human_time()+CLRLN) sys.stdout.write(pos(e+1,0)+"="*10+"AeroFS Transfers"+"="*10+" "+human_time()+CLRLN)
if (stats['running']): if (stats['running']):
sys.stdout.write(pos(e+2,0)+"Last update: "+stats['running'][0][0]+CLRLN) 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(DOWN+CLRBLN+CLRLN)
#sys.stdout.write(LOAD) #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(): def human_time():
t=datetime.now().strftime("%I:%M:%S %p") t=datetime.now().strftime("%I:%M:%S %p")
@@ -175,41 +188,81 @@ def termsize():
rows, columns = os.popen('stty size', 'r').read().split() rows, columns = os.popen('stty size', 'r').read().split()
return (int(rows),int(columns)) 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() options=setup_options()
stats={'time':datetime.now(), stats={'time':datetime.now()-timedelta(seconds=60),
'running':[], 'running':[],
'files':[], 'files':[],
'size': termsize()} 'size': termsize()}
sys.stdout.write(CLR+pos(0,0)+"Launching...") 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: while 1:
try: try:
sys.stdout.flush() sys.stdout.flush()
# set a 3 second timeout for the line read. # set a 3 second timeout for the line read.
signal.signal(signal.SIGALRM, readinput) signal.signal(signal.SIGALRM, transfers.readline)
signal.alarm(3) signal.alarm(3)
line=readinput(proc) line=transfers.readline()
if not line: if not line:
raise EndProgram 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 # timeout returns a special string, in this case we re-read
if line=="CleanerTimeout": if line=="CleanerTimeout":
print_stats(stats) print_stats(stats)
continue continue
stats=count_running(line,stats)
stats=remove_running(stats)
# store only maximum number of error lines
# if line empty, read next # if line empty, read next
if line.strip()=="": if line.strip()=="":
continue continue
print_stats(stats) print_stats(stats)
except EndProgram,KeyboardInterrupt: except EndProgram,KeyboardInterrupt:
transfers.stop()
transfers.join()
sys.stdout.write(DOWN+'\n')
sys.stdout.flush() sys.stdout.flush()
sys.exit(0) sys.exit(0)