Add non-zero exit code on whitespace check failure

This commit is contained in:
Richard Berger
2020-06-16 07:06:37 -04:00
parent 61235308f1
commit 036b2bd82c

View File

@ -3,6 +3,7 @@
# #
# Written by Richard Berger (Temple University) # Written by Richard Berger (Temple University)
import os import os
import sys
import glob import glob
import re import re
import yaml import yaml
@ -76,6 +77,7 @@ def fix_file(path, check_result):
shutil.move(newfile, path) shutil.move(newfile, path)
def check_folder(directory, config, fix=False, verbose=False): def check_folder(directory, config, fix=False, verbose=False):
success = True
files = [] files = []
for base_path in config['include']: for base_path in config['include']:
@ -104,14 +106,19 @@ def check_folder(directory, config, fix=False, verbose=False):
if result['encoding'] == 'unknown': if result['encoding'] == 'unknown':
print("[Error] Unknown text encoding @ {}".format(path)) print("[Error] Unknown text encoding @ {}".format(path))
has_resolvable_errors = False has_resolvable_errors = False
success = False
elif result['encoding'] == 'ISO-8859-1': elif result['encoding'] == 'ISO-8859-1':
print("[Error] Found ISO-8859-1 encoding instead of UTF-8 @ {}".format(path)) print("[Error] Found ISO-8859-1 encoding instead of UTF-8 @ {}".format(path))
has_resolvable_errors = True has_resolvable_errors = True
if has_resolvable_errors and fix: if has_resolvable_errors:
print("Applying automatic fixes to file:", path) if fix:
fix_file(path, result) print("Applying automatic fixes to file:", path)
fix_file(path, result)
else:
success = False
return success
def main(): def main():
parser = argparse.ArgumentParser(description='Utility for detecting and fixing whitespace issues in LAMMPS') parser = argparse.ArgumentParser(description='Utility for detecting and fixing whitespace issues in LAMMPS')
@ -127,7 +134,8 @@ def main():
else: else:
config = yaml.load(DEFAULT_CONFIG, Loader=yaml.FullLoader) config = yaml.load(DEFAULT_CONFIG, Loader=yaml.FullLoader)
check_folder(args.DIRECTORY, config, args.fix, args.verbose) if not check_folder(args.DIRECTORY, config, args.fix, args.verbose):
sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":
main() main()