|
| 1 | +""" |
| 2 | +This module runs various style tools to format source code files. It is meant to be used |
| 3 | +in conjunction with the python module precommit (https://pre-commit.com/) to help enforce |
| 4 | +guidelines before code is committed to a git repository. |
| 5 | +""" |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import argparse |
| 9 | +import shutil |
| 10 | +import sys |
| 11 | +from typing import ( |
| 12 | + Optional, |
| 13 | + Sequence |
| 14 | +) |
| 15 | + |
| 16 | +def main(argv: Optional[Sequence[str]] = None) -> int: |
| 17 | + """ |
| 18 | + Format specified text source files |
| 19 | +
|
| 20 | + :param argv: list of filenames to process |
| 21 | + :return program exit code, 0 = success, > 0 fail (1 for generic error) |
| 22 | + """ |
| 23 | + |
| 24 | + parser = argparse.ArgumentParser() |
| 25 | + parser.add_argument( |
| 26 | + 'filenames', nargs='*', |
| 27 | + help='Filenames pre-commit believes are changed.', |
| 28 | + ) |
| 29 | + |
| 30 | + # Eventually we can add other tool options here... |
| 31 | + parser.add_argument( |
| 32 | + '-a', '--astyle', type=str, |
| 33 | + help='path to astyle config file', |
| 34 | + ) |
| 35 | + parser.add_argument( |
| 36 | + '-c', '--clangformat', type=str, |
| 37 | + help='path to clang-format options file', |
| 38 | + ) |
| 39 | + |
| 40 | + args = parser.parse_args(argv) |
| 41 | + |
| 42 | + if not (args.astyle or args.clangformat): |
| 43 | + parser.error('No action requested, add --astyle or --clangformat') |
| 44 | + |
| 45 | + # Bail early if there are no files to process |
| 46 | + if len(args.filenames) == 0: |
| 47 | + print('No files') |
| 48 | + return 0 |
| 49 | + |
| 50 | + # Note: sourcetree doesn't use the same system path when running commit hooks (there |
| 51 | + # is a bug in their tool). We can try to fix that here (not really sure what |
| 52 | + # happens on windows -- so not handling that now) |
| 53 | + if sys.platform != 'win32': |
| 54 | + if '/usr/local/bin' not in os.environ['PATH']: |
| 55 | + os.environ['PATH'] += os.pathsep + '/usr/local/bin' |
| 56 | + |
| 57 | + if args.astyle: |
| 58 | + # check to make sure astyle is installed |
| 59 | + locate = shutil.which('astyle') |
| 60 | + |
| 61 | + if locate is None: |
| 62 | + print('astyle executable not found on in PATH, is it installed?') |
| 63 | + print('Consult your favorite package manager for installation') |
| 64 | + print('(e.g. \'brew install astyle\' on mac or \'apt-get install astyle\' on Ubuntu)') |
| 65 | + return 1 |
| 66 | + |
| 67 | + # Check to make sure that options file is present |
| 68 | + if not os.path.exists(args.astyle): |
| 69 | + print(f'{args.astyle} not found. Please check that the file exists') |
| 70 | + return 1 |
| 71 | + |
| 72 | + # Run astyle |
| 73 | + for fname in args.filenames: |
| 74 | + # Since check=True below, this will throw an exception if the call |
| 75 | + # to astyle fails for some reason. Note, as long as the options and |
| 76 | + # filename passed to astyle are OK, it should never fail (always returns |
| 77 | + # a '0' whether it formatted a file or not. Need to check the output |
| 78 | + # to know if it actually changed anything in the file or not. |
| 79 | + astyle_result = subprocess.run(["astyle", |
| 80 | + "--options=" + args.astyle, fname], |
| 81 | + capture_output=True, text=True, |
| 82 | + check=True) |
| 83 | + |
| 84 | + if 'Formatted' in astyle_result.stdout: |
| 85 | + print('Formatted: ' + fname) |
| 86 | + |
| 87 | + |
| 88 | + if args.clangformat: |
| 89 | + # check to make sure astyle is installed |
| 90 | + locate = shutil.which('clang-format') |
| 91 | + |
| 92 | + if locate is None: |
| 93 | + print('clang-format executable not found on in PATH, is it installed?') |
| 94 | + print('Consult your favorite package manager for installation') |
| 95 | + print('(e.g. \'brew install clang-format\' on mac or \'apt-get install clang-format\' on Debian/Ubuntu)') |
| 96 | + return 1 |
| 97 | + |
| 98 | + # Check to make sure that options file is present |
| 99 | + if not os.path.exists(args.clangformat): |
| 100 | + print(f'{args.clangformat} not found. Please check that the file exists') |
| 101 | + return 1 |
| 102 | + |
| 103 | + # Run clang-format |
| 104 | + for fname in args.filenames: |
| 105 | + # Since check=True below, this will throw an exception if the call |
| 106 | + # to clang-format fails for some reason. Note, as long as the options and |
| 107 | + # filename passed to clang-format are OK, it should never fail. |
| 108 | + |
| 109 | + # First do a dry run to see if the file needs formatting or not |
| 110 | + # (this is just to support better info being printed during execution) |
| 111 | + clang_result = subprocess.run(["clang-format", |
| 112 | + "--dry-run", |
| 113 | + "--Werror", |
| 114 | + "--style=file:" + args.clangformat, |
| 115 | + "-i", fname], |
| 116 | + capture_output=True, text=True) |
| 117 | + |
| 118 | + if clang_result.returncode != 0: |
| 119 | + # Error code set means the file needs formatting |
| 120 | + clang_result = subprocess.run(["clang-format", |
| 121 | + "--style=file:" + args.clangformat, |
| 122 | + "-i", fname]) |
| 123 | + |
| 124 | + if clang_result.returncode == 0: |
| 125 | + print('Formatted: ' + fname) |
| 126 | + else: |
| 127 | + print('clang-format can not format file: ' + clang_result.stdout) |
| 128 | + return 1 |
| 129 | + |
| 130 | + return 0 |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + sys.exit(main()) |
0 commit comments