-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathreplace_manifest_version.py
More file actions
57 lines (44 loc) · 1.82 KB
/
replace_manifest_version.py
File metadata and controls
57 lines (44 loc) · 1.82 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
import os
import re
import sys
def replace_versions(base_dir, new_version='1.0'):
"""
Replace version numbers in __manifest__.py files
using the pattern: ([\'\"]version[\'\"]:\s?[\'\"])(?:\d+{digit_str})([\'\"],)
and replacement: $1{new_version}$2
"""
base_digit_str = "\.\d+"
digit_str = ""
updated_count = 0
for _ in range(5):
digit_str += base_digit_str
pattern_text = f"([\'\"]version[\'\"]:\s?[\'\"])(?:\d+{digit_str})([\'\"],)"
pattern = re.compile(pattern_text)
for root, _, files in os.walk(base_dir):
if '__manifest__.py' in files:
file_path = os.path.join(root, '__manifest__.py')
with open(file_path, 'r+', encoding='utf-8') as f:
content = f.read()
new_content, replacements = pattern.subn(
rf"\g<1>{new_version}\g<2>", # Using named groups to avoid ambiguity
content
)
if replacements > 0:
f.seek(0)
f.truncate()
f.write(new_content)
updated_count += 1
print(f"Updated {file_path} to version '{new_version}'")
print(f"\nTotal files updated: {updated_count}")
if __name__ == "__main__":
new_version = '1.0'
if len(sys.argv) < 2:
print("Usage: python version_updater.py <directory> [new_version]")
print(f"Default version: {new_version}")
sys.exit(1)
target_dir = sys.argv[1]
version = sys.argv[2] if len(sys.argv) > 2 else new_version
if not os.path.isdir(target_dir):
print(f"Error: Directory not found - {target_dir}")
sys.exit(1)
replace_versions(target_dir, version)