-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCompilerDEBUG.py
More file actions
66 lines (38 loc) · 1.54 KB
/
RemoveCompilerDEBUG.py
File metadata and controls
66 lines (38 loc) · 1.54 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
import os
import re
import sys
def FormatNewlines(filePath):
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")
if not os.path.isfile(filePath):
logging.error(f"File not found - {filePath}")
sys.exit(1)
with open(filePath, "r") as file:
codeContent = file.read()
def LogAffectedLines(pattern):
matches = list(
re.finditer(pattern, codeContent, flags=re.MULTILINE | re.DOTALL)
)
if matches:
lineRanges = []
for match in matches:
startLine = codeContent.count("\n", 0, match.start()) + 1
endLine = codeContent.count("\n", 0, match.end()) + 1
if startLine == endLine:
lineRanges.append(f"{startLine}")
else:
lineRanges.append(f"{startLine}-{endLine}")
logging.debug("Removing compiler DEBUG section.")
logging.debug(f" Lines affected: {', '.join(lineRanges)}.")
pattern = r"(?s)(#ifdef DEBUG.*?#endif\n)"
LogAffectedLines(pattern)
codeContent = re.sub(pattern, r"", codeContent, flags=re.MULTILINE)
with open(filePath, "w") as file:
logging.info(f"File '{filePath}' has been formatted successfully.")
file.write(codeContent)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python RemoveCompilerDEBUG.py <filePath>")
print(f"Received arguments: {sys.argv}")
sys.exit(1)
FormatNewlines(sys.argv[1])