diff options
author | David Robillard <d@drobilla.net> | 2023-04-03 09:00:43 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-04-06 07:19:10 -0400 |
commit | c6e748d4fd22a3defec5a5086df3714d405452d6 (patch) | |
tree | 6700dc49fe6817a98cccd33352203863322d2c3d /scripts/check_formatting.py | |
parent | a4d50af7bae8d5e2f0f27e714292cdad68a36eac (diff) | |
download | serd-c6e748d4fd22a3defec5a5086df3714d405452d6.tar.gz serd-c6e748d4fd22a3defec5a5086df3714d405452d6.tar.bz2 serd-c6e748d4fd22a3defec5a5086df3714d405452d6.zip |
Check formatting of project Turtle files
Diffstat (limited to 'scripts/check_formatting.py')
-rwxr-xr-x | scripts/check_formatting.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/scripts/check_formatting.py b/scripts/check_formatting.py new file mode 100755 index 00000000..e3342603 --- /dev/null +++ b/scripts/check_formatting.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +# Copyright 2023 David Robillard <d@drobilla.net> +# SPDX-License-Identifier: ISC + +"""Run a file through a formatter and error if the output differs.""" + +import argparse +import difflib +import os +import shlex +import subprocess +import sys + +DEVNULL = subprocess.DEVNULL +PIPE = subprocess.PIPE + + +def run_formatter(command, good_path): + """Run the formatter and compare the output.""" + + with subprocess.Popen( + command, stderr=DEVNULL, stdout=PIPE, encoding="utf-8" + ) as proc: + out = list(proc.stdout) + + with open(good_path, "r", encoding="utf-8") as good: + good_lines = list(good) + + same = True + for line in difflib.unified_diff(good_lines, out, fromfile=good_path): + sys.stderr.write(line) + same = False + + return same + + +def main(): + """Run the command line tool.""" + + parser = argparse.ArgumentParser( + usage="%(prog)s [OPTION]... INPUT TOOL [ARG]...", description=__doc__ + ) + + parser.add_argument("--wrapper", default="", help="executable wrapper") + parser.add_argument("input", help="file to check") + parser.add_argument("tool", help="formatter executable") + parser.add_argument("arg", nargs=argparse.REMAINDER, help="tool argument") + + args = parser.parse_args(sys.argv[1:]) + path = os.path.normpath(args.input) + command = shlex.split(args.wrapper) + [args.tool] + args.arg + [path] + + return 0 if run_formatter(command, args.input) else 1 + + +if __name__ == "__main__": + sys.exit(main()) |