Run shfmt as part of the reformat target.
[tinc] / reformat.py
1 #!/usr/bin/env python3
2
3 from os import path, environ
4 from sys import stderr
5 import subprocess as subp
6 import glob
7
8 source_root = path.dirname(path.realpath(__file__))
9 source_root = environ.get("MESON_SOURCE_ROOT", source_root)
10
11 astyle_cmd = [
12     "astyle",
13     "--options=.astylerc",
14     "--recursive",
15     "*.c",
16     "*.h",
17 ]
18
19 shfmt_cmd = [
20     "shfmt",
21     "-i", "2",
22     "-s",
23     "-w",
24 ]
25
26 for path in "**/*.sh", "**/*.test", ".ci/**/*.sh":
27     shfmt_cmd.extend(glob.glob(path, root_dir=source_root, recursive=True))
28
29 for cmd in astyle_cmd, shfmt_cmd:
30     try:
31         result = subp.run(cmd, cwd=source_root, check=True)
32     except FileNotFoundError as e:
33         print("Warning: missing", cmd[0], file=stderr)