mirror of
https://github.com/cp2k/cp2k.git
synced 2026-07-29 06:35:28 -04:00
precommit: Add simple Makefile formatter
This commit is contained in:
parent
5c1a921a00
commit
e974820ca4
7 changed files with 105 additions and 57 deletions
44
tools/precommit/format_makefile.py
Executable file
44
tools/precommit/format_makefile.py
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# author: Ole Schuett
|
||||
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: format_makefile.py <file>")
|
||||
sys.exit(1)
|
||||
makefile = Path(sys.argv[1])
|
||||
|
||||
lines_out = []
|
||||
continuation = False
|
||||
for line in makefile.read_text(encoding="utf8").split("\n"):
|
||||
|
||||
# Remove trailing whitespaces.
|
||||
line = line.rstrip()
|
||||
|
||||
# Detect continued lines.
|
||||
prev_continuation = continuation
|
||||
continuation = line.endswith("\\")
|
||||
|
||||
# Continued lines are indented 8 spaces.
|
||||
if prev_continuation:
|
||||
lines_out.append(" " * 8 + line.strip())
|
||||
|
||||
# Tabbed lines are indented with excatly one tab.
|
||||
elif line.startswith("\t"):
|
||||
lines_out.append("\t" + line.strip())
|
||||
|
||||
# All other lines are not indented.
|
||||
else:
|
||||
lines_out.append(line.strip())
|
||||
|
||||
makefile.write_text("\n".join(lines_out), encoding="utf8")
|
||||
|
||||
|
||||
main()
|
||||
|
||||
# EOF
|
||||
Loading…
Add table
Add a link
Reference in a new issue