File: format_makefile.py

package info (click to toggle)
cp2k 2025.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 372,052 kB
  • sloc: fortran: 963,262; ansic: 64,495; f90: 21,676; python: 14,419; sh: 11,382; xml: 2,173; makefile: 953; pascal: 845; perl: 492; cpp: 345; lisp: 297; csh: 16
file content (43 lines) | stat: -rwxr-xr-x 1,002 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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