Package Jobber :: Module logfile
[hide private]
[frames] | no frames]

Source Code for Module Jobber.logfile

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2009, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 as published by 
 8  # the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13   
14  import os 
15  import time 
16  from cStringIO import StringIO 
17  # posixfile module is deprecated, so defining ourselves 
18  SEEK_END = 2  
19   
20  EOF_MARKER = '<<<<<EOF>>>>>' 
21  MESSAGE_MARKER = '<<<<<JOBMESSAGE>>>>>' 
22   
23 -class LogFile(object):
24
25 - def __init__(self, status, logfilename):
26 self.status = status 27 self.finished = status.isFinished() 28 self.filename = logfilename 29 fn = self.getFilename() 30 self.openfile = open(fn, "a+")
31
32 - def getFilename(self):
33 return self.filename
34
35 - def hasContents(self):
36 return os.path.exists(self.getFilename())
37
38 - def getStatus(self):
39 return self.status
40
41 - def getFile(self):
42 if self.openfile: 43 # Don't close this, because we're using it to write 44 return self.openfile 45 # Please to enjoy a read-only handle 46 return open(self.getFilename(), "r")
47
48 - def getText(self):
49 f = self.getFile() 50 f.seek(0) 51 return f.read()
52
53 - def readlines(self):
54 io = StringIO(self.getText()) 55 return io.readlines()
56
57 - def getLines(self):
58 f = self.getFile() 59 offset = 0 60 f.seek(0, SEEK_END) 61 remaining = f.tell() 62 return self.generate_lines(f, offset, remaining)
63
64 - def generate_lines(self, f, offset, remaining):
65 f.seek(offset) 66 for line in f.readlines(remaining): 67 yield line 68 del f
69
70 - def write(self, text):
71 f = self.openfile 72 f.seek(0, SEEK_END) 73 offset = 0 74 f.write(text) 75 f.flush()
76
77 - def msg(self, text):
78 self.write('%s %s' % (MESSAGE_MARKER, text))
79
80 - def finish(self):
81 if self.openfile: 82 self.write('\n%s' % EOF_MARKER) 83 os.fsync(self.openfile.fileno()) 84 del self.openfile
85