Last active
May 10, 2016 21:02
-
-
Save alexras/a296927ef857d7b2229f to your computer and use it in GitHub Desktop.
Demultiplex the output of `tail -f *.log`
This file contains hidden or 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
| #!/usr/bin/env python | |
| import argparse | |
| import re | |
| import os | |
| import sys | |
| file_delimiter_regex = re.compile('.*?\=\=\> (.*?) \<\=\=\n') | |
| def tail_demux(filename, output_dir): | |
| if not os.path.exists(filename): | |
| sys.exit('File "%s" not found' % (filename)) | |
| os.makedirs(output_dir) | |
| current_file = os.path.join(output_dir, 'preamble') | |
| output_files = {} | |
| with open(filename, 'r') as fp: | |
| for line in fp: | |
| if len(line) == 1: | |
| continue | |
| line_match = file_delimiter_regex.match(line) | |
| if line_match is not None: | |
| current_file = os.path.join(output_dir, os.path.basename(line_match.group(1))) | |
| else: | |
| if current_file not in output_files: | |
| output_files[current_file] = open(current_file, 'w') | |
| output_files[current_file].write(line) | |
| for fp in output_files.values(): | |
| fp.flush() | |
| fp.close() | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description='demultiplex the output of `tail -f *.log`') | |
| parser.add_argument('filename', help='the file to demux') | |
| parser.add_argument('--output_dir', '-o', | |
| help='the directory in which the demuxed logs should be stored', | |
| default='demuxed') | |
| args = parser.parse_args() | |
| tail_demux(**vars(args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment