File: format_makefile.py

package info (click to toggle)
cp2k 2025.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 366,832 kB
  • sloc: fortran: 955,049; f90: 21,676; ansic: 18,058; python: 13,378; sh: 12,179; xml: 2,173; makefile: 964; pascal: 845; perl: 492; lisp: 272; cpp: 137; csh: 16
file content (43 lines) | stat: -rwxr-xr-x 1,002 bytes parent folder | download
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