Package PyFoam :: Package Execution :: Module BasicWatcher
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Execution.BasicWatcher

 1  #  ICE Revision: $Id: BasicWatcher.py 8195 2007-11-19 09:13:52Z bgschaid $  
 2  """Watches the output of Foam-run""" 
 3   
 4  from os import path 
 5  import stat 
 6  import os 
 7  from time import sleep 
 8   
 9  from PyFoam.Basics.LineReader import LineReader 
10   
11 -class BasicWatcher(object):
12 """Base class for watching the output of commands 13 14 Works like the UNIX-command 'tail -f <file>': the last lines of the file are output. 15 If the file grows then these lines are output as they arrive""" 16
17 - def __init__(self,filename,silent=False,tailLength=1000,sleep=0.1):
18 """@param filename: name of the logfile to watch 19 @param silent: if True no output is sent to stdout 20 @param tailLength: number of bytes at the end of the fail that should be output. 21 Because data is output on a per-line-basis 22 @param sleep: interval to sleep if no line is returned""" 23 24 self.filename=filename 25 self.silent=silent 26 self.tail=tailLength 27 self.sleep=sleep 28 self.isTailing=False 29 30 if not path.exists(self.filename): 31 print "Error: Logfile ",self.filename,"does not exist" 32 33 self.reader=LineReader()
34
35 - def getSize(self):
36 """@return: the current size (in bytes) of the file""" 37 return os.stat(self.filename)[stat.ST_SIZE]
38
39 - def start(self):
40 """Reads the file and does the processing""" 41 42 currSize=self.getSize() 43 fh=open(self.filename) 44 45 self.startHandle() 46 47 while 1: 48 status=self.reader.read(fh) 49 if status: 50 line=self.reader.line 51 if (currSize-self.reader.bytesRead())<=self.tail: 52 if not self.isTailing: 53 self.isTailing=True 54 self.timeHandle() 55 self.tailingHandle() 56 57 if not self.silent: 58 print line 59 60 self.lineHandle(line) 61 else: 62 if self.reader.userSaidStop(): 63 break 64 try: 65 sleep(self.sleep) 66 except KeyboardInterrupt,e: 67 print "Keyboard interrupt" 68 break 69 70 self.stopHandle() 71 72 fh.close()
73
74 - def startHandle(self):
75 """to be called before the program is started""" 76 pass
77
78 - def stopHandle(self):
79 """called after the program has stopped""" 80 pass
81
82 - def tailingHandle(self):
83 """called when the first line is output""" 84 pass
85
86 - def lineHandle(self,line):
87 """called every time a new line is read""" 88 pass
89