#!/usr/software/bin/python2.7 -utt ''' Wrapper script to filter the given info file. ''' import os import subprocess import argparse import tempfile import datetime import shutil def parse_args(): ''' Parse and validate the given arguments ''' desc = "Picks out the patterns of the filter file from the input info file." parser = argparse.ArgumentParser(description=desc) parser.add_argument("-o", "--outputdir", help='Output directory where filtered info files will be created') parser.add_argument("-f", "--filter-file", required=True, help='Pattern to be filtered from input info files') parser.add_argument("info_files", nargs='*', help="Info file(s) that needs to be filtered") args = parser.parse_args() if len(args.info_files) < 1: parser.error('Input info files not specified') filter_file = args.filter_file if not os.path.exists(filter_file): parser.error("File %s does not exist\n" % filter_file) if not args.outputdir: output_dir = os.getcwd() else: output_dir = args.outputdir return args.info_files, filter_file, output_dir def run_cmd(pattern, info_file, final_info): ''' Frame; Run; Validate the status of the command. Returns True if the command got run successfully. Else return False ''' lcov_info_filter_path = "/usr/software/rats/bin/lcov_info_filter" lcov_path = "/usr/software/test/projects/ncov_tool/new/lcov/bin/lcov" filtered_info = tempfile.mkstemp()[1] tmp_file = tempfile.mkstemp()[1] filter_cmd = [lcov_info_filter_path, '--include-patterns', pattern, '-o', filtered_info, info_file] try: subprocess.check_call(filter_cmd) except subprocess.CalledProcessError as err: print ("Error running the command %s: %s" % (filter_cmd, err)) return False append_cmd = [lcov_path, '-a', filtered_info] if os.path.exists(final_info): append_cmd.extend(['-a', final_info]) append_cmd.extend(['-o', tmp_file]) try: subprocess.check_call(append_cmd) except subprocess.CalledProcessError as err: print ("Error running the command %s: %s" % (append_cmd, err)) return False shutil.copy(tmp_file, final_info) try: os.remove(filtered_info) os.remove(tmp_file) except OSError as err: print ("Error removing file %s: %s" % (filtered_info, err)) return False return True def generate_filtered_info(info_files, filter_file, output_dir): ''' Generated the filtered info file for each of the input info files. ''' for info_file in info_files: if not os.path.exists(info_file): print ("Info file %s does not exist\n" % info_file) continue timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f") info_to_append = os.path.basename(info_file).split(".")[0] final_info = "%s/final_%s_%s.info" \ % (output_dir, info_to_append, timestamp) ff_handle = open(filter_file, 'r') for line in ff_handle: line = line.strip() if not run_cmd(line, info_file, final_info): print ("Error Generating the filtered info file.") break print ("Generated filtered info file - %s" % (final_info)) def main(): ''' Main ''' (info_files, filter_file, output_dir) = parse_args() generate_filtered_info(info_files, filter_file, output_dir) main()