-
Notifications
You must be signed in to change notification settings - Fork 6
/
updateVersionNumber.py
executable file
·103 lines (94 loc) · 4.34 KB
/
updateVersionNumber.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import argparse
import fnmatch
import os
def find_files():
matching_filters = ['pom.xml', 'HowToDeploy.txt', 'calvalus.properties', 'directory-listing.xsl',
'HadoopProcessingService.java', 'ProductionTool.java',
'calvalus.config', 'INSTALL.txt', 'ReportGeneratorTest.java' ]
matching_filters = ['pom.xml', '*.java', '*.jsp', '*.txt', '*.properties','*.xsl','*.config']
matches = []
for root, dirnames, filenames in os.walk('.'):
for filter in matching_filters:
for filename in fnmatch.filter(filenames, filter):
matches.append(os.path.join(root, filename))
return matches
def read_files_list():
matches = []
for line in open('updateVersionNumberFiles.cfg'):
matches.append(line.strip())
return matches
def update_version_numbers():
parser = argparse.ArgumentParser(description='Update version number in code.')
parser.add_argument('--dry-run', action='store_true', default=False,
required=False, help="Don't write any files, just pretend.")
parser.add_argument('--not-exact', action='store_true', default=False,
required=False, help="Don't be extremely exact with matching.")
parser.add_argument('--find-files', action='store_true', default=False,
required=False, help="Searches for files containing the given old version number.")
parser.add_argument('kind', choices=['calvalus', 'snap'])
parser.add_argument('old_version', action='store')
parser.add_argument('new_version', action='store')
args = parser.parse_args()
print (args)
exact = not args.not_exact
if args.find_files:
filesToChange = find_files()
else:
filesToChange = read_files_list()
for file in filesToChange:
print ()
print (file)
with open(file) as f:
fileContent = f.readlines()
changes = False
changedLines = []
fileContentNew = []
for line in fileContent:
if args.kind == 'calvalus':
if 'version>' + args.old_version + '</' in line:
changedLines.append("old:"+line)
line = line.replace(args.old_version, args.new_version)
changes = True
changedLines.append("new:"+line)
elif 'calvalus-' + args.old_version in line:
if exact:
if not 'calvalus-' + args.old_version + '-' in line:
changedLines.append("old:"+line)
line = line.replace(args.old_version, args.new_version)
changes = True
changedLines.append("new:"+line)
else:
changedLines.append("old:"+line)
line = line.replace(args.old_version, args.new_version)
changes = True
changedLines.append("new:"+line)
elif 'ersion ' + args.old_version in line:
changedLines.append("old:"+line)
line = line.replace(args.old_version, args.new_version)
changes = True
changedLines.append("new:"+line)
elif args.kind == 'snap':
if 'snap-' + args.old_version in line:
if exact:
if not 'snap-' + args.old_version + '-' in line:
changedLines.append("old:"+line)
line = line.replace(args.old_version, args.new_version)
changes = True
changedLines.append("new:"+line)
else:
changedLines.append("old:"+line)
line = line.replace(args.old_version, args.new_version)
changes = True
changedLines.append("new:"+line)
fileContentNew.append(line)
if changes:
if args.dry_run:
for line in changedLines:
print (line, end='')
else:
fileHandle = open(file, 'w')
fileHandle.writelines(fileContentNew)
fileHandle.close()
if __name__ == '__main__':
update_version_numbers()