Created
January 23, 2010 07:54
-
-
Save saltycrane/284491 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import gzip | |
import re | |
def get_lines(log_glob): | |
"""Return an iterator of each line in all files matching log_glob. | |
Lines are sorted most recent first. | |
Files are sorted by the integer in the suffix of the log filename. | |
Suffix may be one of the following: | |
.X (where X is an integer) | |
.X.gz (where X is an integer) | |
If the filename does not end in either suffix, it is treated as if X=0 | |
""" | |
def sort_by_suffix(a, b): | |
def get_suffix(fname): | |
m = re.search(r'.(?:\.(\d+))?(?:\.gz)?$', fname) | |
if m.lastindex: | |
suf = int(m.group(1)) | |
else: | |
suf = 0 | |
return suf | |
return get_suffix(a) - get_suffix(b) | |
filelist = glob.glob(log_glob) | |
for filename in sorted(filelist, sort_by_suffix): | |
if filename.endswith('.gz'): | |
fh = gzip.open(filename) | |
else: | |
fh = open(filename) | |
for line in reversed(fh.readlines()): | |
yield line | |
fh.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment