-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathspec-cleaner
More file actions
executable file
·81 lines (71 loc) · 2.91 KB
/
spec-cleaner
File metadata and controls
executable file
·81 lines (71 loc) · 2.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
import re
import sys
# Dictionary of macros to replace: key is the macro, value is its replacement
MACROS_TO_REPLACE = {
r'%make\b': '%make_build',
r'%makeinstall_std\b': '%make_install',
r'%configure2_5x\b': '%configure',
r'%apply_patches\b': '%autopatch -p1',
r'%makeinstallqt\b': '%make_install INSTALL_ROOT="%{buildroot}"',
r'\$\{?RPM_BUILD_ROOT\}?': '%{buildroot}',
r'%setup_compile_flags\b': '%set_build_flags',
# Add more macros here as needed
# Example:
# r'%old_macro\b': '%new_macro',
}
def replace_macros_and_clean(file_path):
try:
# Read the file content
with open(file_path, 'r') as file:
original_content = file.read()
content = original_content
# Track changes
changes = []
# Replace macros using the dictionary
for old_macro, new_macro in MACROS_TO_REPLACE.items():
# Use regex to replace only whole macros
new_content, count = re.subn(old_macro, new_macro, content)
if count > 0:
changes.append(f"Replaced '{old_macro}' with '{new_macro}' {count} times")
content = new_content
# Remove lines containing %serverbuild or %serverbuild_hardened
lines = content.splitlines()
filtered_lines = []
server_build_removed = 0
server_build_pattern = re.compile(r'%serverbuild\b|%serverbuild_hardened\b')
for line in lines:
if server_build_pattern.search(line):
server_build_removed += 1
else:
filtered_lines.append(line)
if server_build_removed > 0:
changes.append(f"Removed {server_build_removed} lines containing %serverbuild or %serverbuild_hardened")
content = "\n".join(filtered_lines)
# Remove trailing spaces from each line
lines = content.splitlines()
cleaned_lines = [line.rstrip() for line in lines]
cleaned_content = "\n".join(cleaned_lines) + "\n" # Ensure trailing newline
# Check if trailing spaces were removed
if cleaned_content != content:
changes.append("Removed trailing spaces from all lines")
# Check if any changes were made
if changes:
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.write(cleaned_content)
print(f"Changes in file '{file_path}':")
for change in changes:
print(f" - {change}")
else:
print(f"Nothing to clean in file '{file_path}'.")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: /usr/bin/spec-cleaner foo.spec")
else:
file_path = sys.argv[1]
replace_macros_and_clean(file_path)