2
"""Simple looped check over provided file for regex content."""
3
from __future__ import print_function
18
def check_file(check_string, file_path):
19
print('Checking for:\n{}'.format(check_string))
20
for _ in range(0, 10):
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:
30
return check_result.exception
31
print('Unexpected error with file check.')
32
return check_result.failure
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))
40
def parse_args(argv=None):
41
parser = argparse.ArgumentParser(
42
description='File content check.')
44
'regex', help='Regex string to check file with.')
46
'file_path', help='Path to file to check.')
47
return parser.parse_args(argv)
51
args = parse_args(argv)
53
raise_if_file_not_found(args.file_path)
54
except ValueError as e:
56
sys.exit(check_result.exception)
58
sys.exit(check_file(args.regex, args.file_path))
61
if __name__ == '__main__':