3 # Will uncompress a gzipped file in "streaming" mode,
4 # like `tail -b +0 -f ` would do.
6 # If you pass -n <int> parameter it will try to act like
7 # a smart "tail" command: reads until the end of the current
8 # file, shows you the last <int> lines, then "follows" the file.
10 # If the writer is writing faster than we read, then it may never
11 # "detect" the "end of the file"...
14 # Author: Martin Langhoff <martin.langhoff@gmail.com>
15 # Copyright: Remote Learner US - http://www.remote-learner.net/
22 from collections
import deque
26 optslist
, args
= getopt
.getopt(sys
.argv
[1:], "tn:")
31 except getopt
.GetoptError
as err
:
32 # print help information and exit:
33 print(err
) # will print something like "option -a not recognized"
37 # lookbehind buffer, defined as a list
44 lbbufsize
=int(opts
['-n'])
46 # tidy up - no errors on ctrl-C, etc
47 def sig_exit(signum
, frame
):
48 # this will prompt fh cleanup, etc
50 signal
.signal(signal
.SIGHUP
, sig_exit
)
51 signal
.signal(signal
.SIGINT
, sig_exit
)
52 signal
.signal(signal
.SIGTERM
, sig_exit
)
54 f
= gzip
.open(args
[0], 'r')
56 # while / readline() sidesteps magic input buffering
61 ## we ignore IOError("CRC check failed")
62 ## because we are reading an "unfinished"
64 # line won't get unset if this triggers
71 except AttributeError:
72 # old gzip does not have "closed"
75 # got to the current tail of it
76 # dump our lookbehind buffer, stop buffering
77 for buf_line
in lbbuf
:
78 sys
.stdout
.write(buf_line
)
80 # internal gzip API use?
81 # f.decompress.flush()
85 sys
.stdout
.write(line
)
92 if len(lbbuf
) >= lbbufsize
: