forked from kasun/python-tail
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtail.py
More file actions
Latest commit
185 lines (140 loc) · 6.88 KB
/
Copy pathtail.py
File metadata and controls
185 lines (140 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
'''
Python-Tail - Unix tail follow implementation in Python.
python-tail can be used to monitor changes to a file.
Example:
import tail
# Create a tail instance
t = self.Tail('file-to-be-followed')
# Register a callback function to be called when a new line is found in the followed file.
# If no callback function is registerd, new lines would be printed to standard out.
t.register_callback(callback_function)
# Follow the file with 5 seconds as sleep time between iterations.
# If sleep time is not provided 1 second is used as the default time.
t.follow(s=5) '''
# Author - Alexis Okuwa <alexisokuwa at gmail.com>
# Source - https://github.com/wojons/python-tail
# Forked - https://github.com/kasun/python-tail
importos
importsys
importtime
classTail(object):
''' Represents a tail command. '''
follow_filename=False
follows=dict()
def__init__(self):
''' Initiate a Tail instance.
Check for file validity, assigns callback function to standard out.
Arguments:
tailed_file - File to be followed. '''
deftail(self, files, callback=None):
iftype(files).__name__=='str': #we allow strings we will just handle the converstion
files= [files]
forfilenameinfiles: #make sure we have good files all around
try:
self.check_file_validity(filename)
exceptTailErrorase:
sys.stderr.write(str(e))
iffilenamenotinself.follows: #make sure we dont have the file already
self.new_follow(filename)
self.set_callback(filename, callback)
#self.callback = sys.stdout.write
defset_follow_filename(self, boolean):
"""
Set true or false if you want program to follow file even if inode changes like when logrotate runes see gnu tail --folow=name
"""
self.follow_filename=boolean
deffollow(self, s=1, max_time=None, loop_max=True):
''' Do a tail follow. If a callback function is registered it is called with every new line.
Else printed to standard out.
Arguments:
s - Number of seconds to wait between each iteration; Defaults to 1.
max_time - the appoxment number of seocnds the loop should run for
loop_max - the max number of times the loop should run over
'''
forfilenameinself.follows:
ifself.get_filename_fd(filename) isNone:
self.open_file(filename) #get all the file points
ifself.get_filename_fd(filename) !=None:
self.follows[filename]['fd'].seek(0, os.SEEK_END) #handle all the seeking
follow_t=time.time()
whileloop_max!=0and (max_time==Noneortime.time() <=max_time+follow_t):
start_t=time.time() #gotta keep track of how long we been in this
forfilename, file_inself.follows.iteritems():
iffile_['fd'] !=None:
file_=self.read_2_EOF(filename, file_['fd'])
else:
self.open_file(filename)
ifself.follow_filename==True: #check to see if inode has changed
self.check_inode(filename)
ifloop_max!=True:
loop_max-=1
ifloop_max==0: continue
time_lap=time.time()-start_t
if(time_lap<s): time.sleep(s-time_lap)
defread_2_EOF(self, filename, file_):
iffile_==None: returnNone
line=True#reset line value
whileline:
curr_position=file_.tell() #get current postion
line=file_.readline() #read the next line we use readline() and not readlines because we may read a line that is still being written to.
ifnotline:
file_.seek(curr_position) #reset the seek point so we can try from the same point again
else:
self.exec_callback(line, filename)
returnfile_
defexec_callback(self, line, filename):
iffilenameinself.followsandself.follows[filename].get('callback') isnotNone:
self.follows[filename]['callback']({'line':line, 'filename':filename})
else:
print ({'line':line, 'filename':filename})
defget_inode(self, filename):
try:
returnos.stat(filename).st_ino
exceptOSErrorase:
returnNone
defget_filename_inode(self, filename):
iffilenameinself.follows: returnself.follows[filename].get('inode')
defget_file_num(self, filename):
try:
returnself.tailed_files.index(filename)
except:
returnNone
defset_callback(self, filename, callback):
self.follows[filename]['callback'] =callback
defopen_file(self, filename):
try:
ifnotos.access(filename, os.F_OK): returnNone
ifnotos.access(filename, os.R_OK): returnNone
ifos.path.isdir(filename): returnNone
self.follows[filename]['fd'] =open(filename)
self.follows[filename]['inode'] =self.get_inode(filename)
except (IOError, OSError) ase:
pass
defget_filename_fd(self, filename):
iffilenameinself.follows: returnself.follows[filename].get('fd')
defcheck_inode(self, filename):
try:
ifself.get_inode(filename) !=self.get_filename_inode(filename): #check if the inode has changed
self.read_2_EOF(filename, self.get_filename_fd(filename)) #read the current but old file to EOF
self.open_file(filename) # reopen the file pointer to the replacment file
exceptOSErrorase:
pass
defnew_follow(self, filename, overwrite=False):
iffilenamenotinself.followsoroverwriteisTrue:
self.follows[filename] = {'fd': None, 'callback': None, 'inode': None}
returnTrue
returnTrue
defcheck_file_validity(self, file_):
''' Check whether the a given file exists, readable and is a file '''
ifnotos.access(file_, os.F_OK):
raiseTailError("File '%s' does not exist"% (file_))
ifnotos.access(file_, os.R_OK):
raiseTailError("File '%s' not readable"% (file_))
ifos.path.isdir(file_):
raiseTailError("File '%s' is a directory"% (file_))
classTailError(Exception):
def__init__(self, msg):
self.message=msg
def__str__(self):
returnself.message