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