Skip to content

Commit 1276369

Browse files
committed
Tools: Add a remove_mutter_versions.py script
1 parent 305a6a4 commit 1276369

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

tools/remove_mutter_versions.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Simple script to remove mutter version blocks from vala files up to the specified version.
2+
#
3+
# For example for a block
4+
#
5+
# #if HAS_MUTTER44
6+
# ...
7+
# #else
8+
# ...
9+
# #endif
10+
#
11+
# and a specified version of 44 or higher only the code between #if and #else will be kept.
12+
13+
import os
14+
import re
15+
import sys
16+
17+
class MutterBlock:
18+
def __init__(self):
19+
self.skip_block = False
20+
self.discard = False
21+
self.keep_endif = False
22+
23+
24+
def remove_mutter_blocks(file_path, version):
25+
print ("Removing mutter blocks from file: " + file_path + " for version: " + str(version))
26+
27+
with open(file_path, 'r') as file:
28+
lines = file.readlines()
29+
30+
output_lines = []
31+
blocks = [MutterBlock()]
32+
version_pattern = re.compile(r'(#if|#elif) (!)?HAS_MUTTER(\d+)')
33+
34+
for line in lines:
35+
if line.startswith("#if"):
36+
blocks.append(MutterBlock())
37+
38+
if version_pattern.match(line):
39+
match = version_pattern.match(line)
40+
41+
current_version = int(match.group(3))
42+
if current_version <= version:
43+
if not blocks[-1].skip_block and match.group(2) == "!":
44+
blocks[-1].skip_block = True
45+
blocks[-1].discard = True
46+
continue
47+
elif not blocks[-1].skip_block and match.group(1) == "#elif":
48+
output_lines.append("#else\n")
49+
blocks[-1].keep_endif = True
50+
elif blocks[-1].skip_block: # we are already skipping so we probably are in an #elif block with an even lower version so we can discard right away
51+
blocks[-1].discard = True
52+
53+
blocks[-1].skip_block = True
54+
continue
55+
elif blocks[-1].skip_block and line.strip() == '#else':
56+
blocks[-1].discard = not blocks[-1].discard
57+
continue
58+
elif line.strip() == '#endif':
59+
block = blocks.pop()
60+
if block.skip_block and not block.keep_endif:
61+
continue
62+
63+
discard = False
64+
for b in blocks:
65+
if b.discard:
66+
discard = True
67+
break
68+
69+
if not discard:
70+
output_lines.append(line)
71+
72+
with open(file_path, 'w') as file:
73+
file.writelines(output_lines)
74+
75+
def remove_recursive(file_path, version):
76+
for file in os.listdir(file_path):
77+
if os.path.isdir(file_path + "/" + file) and not file.startswith(".") and not "build" in file:
78+
remove_recursive(file_path + "/" + file, version)
79+
elif file.endswith(".vala") or file.endswith(".vapi"):
80+
remove_mutter_blocks(file_path + "/" + file, version)
81+
82+
if __name__ == "__main__":
83+
if len(sys.argv) != 3:
84+
print("Usage: python remove_mutter_versions.py <file_path> <version>")
85+
sys.exit(1)
86+
87+
file_path = sys.argv[1]
88+
version = int(sys.argv[2])
89+
90+
if os.path.isdir(file_path):
91+
print ("Removing mutter blocks from all vala files in dir and subdirs: " + sys.argv[1] + " for version: " + sys.argv[2])
92+
remove_recursive(file_path, version)
93+
else:
94+
remove_mutter_blocks(file_path, version)
95+
96+
print ("Done! Also don't forget to update the meson.build files, README and remove outdated vapi.")

0 commit comments

Comments
 (0)