Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added filename feature to follow() #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3
0.31
42 changes: 36 additions & 6 deletions src/tailer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import sys
import time
import os

if sys.version_info < (3,):
range = xrange
Expand Down Expand Up @@ -150,14 +151,20 @@ def head(self, lines=10):
else:
return []

def follow(self, delay=1.0):
def follow(self, delay=1.0, filename=None):
"""\
Iterator generator that returns lines as data is added to the file.

Based on: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157035

If filename is passed, will make sure that the file has not been deleted,
or that it has not been rotated. This feature is useful for compatability with
logrotate.
"""
trailing = True

if filename is not None:
curino = os.fstat(self.file.fileno()).st_ino

while 1:
where = self.file.tell()
line = self.file.readline()
Expand All @@ -177,9 +184,32 @@ def follow(self, delay=1.0):
trailing = False
yield line
else:
should_seek = True
if filename is not None:
reopen = False
try:
if os.stat(filename).st_ino != curino:
self.file.close()
reopen = True
except:
# file probably has been deleted
curino = None
reopen = True

if reopen:
try:
self.file = open(filename, "r")
curino = os.fstat(self.file.fileno()).st_ino
should_seek = False
except:
# a new file does not exist yet
time.sleep(delay)

if should_seek:
self.seek(where)
time.sleep(delay)

trailing = True
self.seek(where)
time.sleep(delay)

def __iter__(self):
return self.follow()
Expand Down Expand Up @@ -213,7 +243,7 @@ def head(file, lines=10):
"""
return Tailer(file).head(lines)

def follow(file, delay=1.0):
def follow(file, delay=1.0, filename=None):
"""\
Iterator generator that returns lines as data is added to the file.

Expand All @@ -233,7 +263,7 @@ def follow(file, delay=1.0):
>>> fo.close()
>>> os.remove('test_follow.txt')
"""
return Tailer(file, end=True).follow(delay)
return Tailer(file, end=True).follow(delay, filename)

def _test():
import doctest
Expand Down