-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytail.py
53 lines (50 loc) · 1.66 KB
/
pytail.py
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
#!/usr/local/bin/python
import select
from select import kqueue, kevent
from time import sleep
import os
import sys
filename = "access"
fh = open(filename, "r")
kq = kqueue()
event = [
kevent(fh, filter=select.KQ_FILTER_VNODE,
flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR,
fflags=select.KQ_NOTE_DELETE |
select.KQ_NOTE_RENAME |
select.KQ_NOTE_WRITE)
]
print fh.read(),
kq.control(event,0)
try:
while True:
r_events = kq.control(None,1)
for event in r_events:
if event.fflags & select.KQ_NOTE_WRITE:
print fh.read(),
if event.fflags & select.KQ_NOTE_DELETE:
kq.close()
fh.close()
exit(0)
elif event.fflags & select.KQ_NOTE_RENAME:
event.flags = select.KQ_EV_DELETE
kq.control([event],0,0)
fh.close()
sleep(1)
fh = open(filename,"r")
kq.control([kevent(fh,
filter=select.KQ_FILTER_VNODE,
flags=select.KQ_EV_ADD |
select.KQ_EV_CLEAR,
fflags=select.KQ_NOTE_DELETE |
select.KQ_NOTE_RENAME |
select.KQ_NOTE_WRITE)],
0
)
elif event.ident == 1000:
event.flags = select.KQ_EV_DISABLE
kq.control([event],0,0)
except KeyboardInterrupt:
kq.close()
fh.close()
exit(1)