-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathupdate_version.py
70 lines (52 loc) · 1.94 KB
/
update_version.py
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
import sys
from pydatastructs.utils.tests.test_code_quality import _list_files
# Files to be updated with the new version number
# The first element of the tuple is the directory path
# and the second element is a lambda function that
# returns True if the file should be updated.
FILES_TO_BE_SCANNED = {
'pydatastructs': ('./pydatastructs', lambda _file: _file.endswith('.py')),
'docs': ('./docs/source', lambda _file: _file.endswith('.rst') or _file.endswith('.py'))
}
def update_version_in_files(file_paths, origin_version, new_version):
"""
Updates the version number in the specified files.
Parameters
==========
file_paths: list
List of file paths to be updated.
origin_version: str
The original version number to be replaced.
new_version: str
The new version number to replace the original.
Returns
=======
None
"""
was_updated = False
for path in file_paths:
with open(path, 'r') as file:
data = file.read()
if origin_version in data:
was_updated = True
data = data.replace(origin_version, new_version)
with open(path, 'w') as file:
file.write(data)
return was_updated
def main():
if len(sys.argv) != 3:
print('Usage: python update_version.py <origin_version> <new_version>')
return
origin_version, new_version = sys.argv[1], sys.argv[2]
print(f'Updating version number from {origin_version} to {new_version}...')
file_paths = ['README.md', 'setup.py']
for _, (dir_path, checker) in FILES_TO_BE_SCANNED.items():
file_paths.extend(_list_files(checker, dir_path))
was_updated = update_version_in_files(
file_paths, origin_version, new_version)
if was_updated:
print('Version number updated successfully!')
else:
print('WARNING: Version number not found in the specified files.')
if __name__ == "__main__":
main()