Debian Codegraph @

misc/scripts/file_format.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import sys
4
5 if len(sys.argv) < 2:
6 print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.")
7 sys.exit(1)
8
9 BOM = b"\xef\xbb\xbf"
10
11 changed = []
12 invalid = []
13
14 for file in sys.argv[1:]:
15 try:
16 with open(file, "rt", encoding="utf-8") as f:
17 original = f.read()
18 except UnicodeDecodeError:
19 invalid.append(file)
20 continue
21
22 if original == "":
23 continue
24
25 EOL = "\r\n" if file.endswith((".csproj", ".sln", ".bat")) or file.startswith("misc/msvs") else "\n"
26 WANTS_BOM = file.endswith((".csproj", ".sln"))
27
28 revamp = EOL.join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip(EOL) + EOL
29
30 new_raw = revamp.encode(encoding="utf-8")
31 if not WANTS_BOM and new_raw.startswith(BOM):
32 new_raw = new_raw[len(BOM) :]
33 elif WANTS_BOM and not new_raw.startswith(BOM):
34 new_raw = BOM + new_raw
35
36 with open(file, "rb") as f:
37 old_raw = f.read()
38
39 if old_raw != new_raw:
40 changed.append(file)
41 with open(file, "wb") as f:
42 f.write(new_raw)
43
44 if changed:
45 for file in changed:
46 print(f"FIXED: {file}")
47 if invalid:
48 for file in invalid:
49 print(f"REQUIRES MANUAL CHANGES: {file}")
50 sys.exit(1)