X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;ds=sidebyside;f=djbdns%2Fio.py;h=1e41470452a30e4c56438ff2ab1ea84a210b0566;hb=3a5a400178399afcbc3051df860231f3c4cf4ff7;hp=1d68fac428ce534652ad50e81c56869121c33a0e;hpb=4645a2edd7eeb0470d5796f4f15f02d23429288e;p=djbdns-logparse.git diff --git a/djbdns/io.py b/djbdns/io.py index 1d68fac..1e41470 100644 --- a/djbdns/io.py +++ b/djbdns/io.py @@ -31,25 +31,29 @@ def parse_logfile(file : TextIO): # Open a pipe to tai64nlocal. We'll write lines of our input file # (the log file) to it, and read back the same lines but with # friendly timestamps in them. - tai = Popen(["tai64nlocal"], stdin=PIPE, stdout=PIPE, text=True, bufsize=0) + with Popen(["tai64nlocal"], + stdin=PIPE, + stdout=PIPE, + text=True, + bufsize=0) as tai: - for line in file: - tai.stdin.write(line) - line = tai.stdout.readline() + if not tai.stdin or not tai.stdout: + return - friendly_line = handle_tinydns_log(line) - if not friendly_line: - friendly_line = handle_dnscache_log(line) + for line in file: + tai.stdin.write(line) + line = tai.stdout.readline() + + friendly_line = handle_tinydns_log(line) if not friendly_line: - friendly_line = line - - try: - print(friendly_line) - except BrokenPipeError: - # If our stdout is being piped to another process and if - # that process closes the pipe, this error will be raised - # the next time we try to write to stdout. - break - - # Ensure that the pipe gets closed. - tai.communicate() + friendly_line = handle_dnscache_log(line) + if not friendly_line: + friendly_line = line + + try: + print(friendly_line) + except BrokenPipeError: + # If our stdout is being piped to another process and if + # that process closes the pipe, this error will be raised + # the next time we try to write to stdout. + break