github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/log_check.py (about) 1 #!/usr/bin/env python 2 """Simple looped check over provided file for regex content.""" 3 from __future__ import print_function 4 5 import argparse 6 import os 7 import subprocess 8 import sys 9 import time 10 11 12 class check_result: 13 success = 0 14 failure = 1 15 exception = 2 16 17 18 def check_file(check_string, file_path): 19 print('Checking for:\n{}'.format(check_string)) 20 for _ in range(0, 10): 21 try: 22 subprocess.check_call( 23 ['sudo', 'egrep', check_string, file_path]) 24 print('Log content found. No need to continue.') 25 return check_result.success 26 except subprocess.CalledProcessError as e: 27 if e.returncode == 1: 28 time.sleep(1) 29 else: 30 return check_result.exception 31 print('Unexpected error with file check.') 32 return check_result.failure 33 34 35 def raise_if_file_not_found(file_path): 36 if not os.path.exists(file_path): 37 raise ValueError('File not found: {}'.format(file_path)) 38 39 40 def parse_args(argv=None): 41 parser = argparse.ArgumentParser( 42 description='File content check.') 43 parser.add_argument( 44 'regex', help='Regex string to check file with.') 45 parser.add_argument( 46 'file_path', help='Path to file to check.') 47 return parser.parse_args(argv) 48 49 50 def main(argv=None): 51 args = parse_args(argv) 52 try: 53 raise_if_file_not_found(args.file_path) 54 except ValueError as e: 55 print(e) 56 sys.exit(check_result.exception) 57 58 sys.exit(check_file(args.regex, args.file_path)) 59 60 61 if __name__ == '__main__': 62 sys.exit(main())