-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathreplace_util.py
209 lines (183 loc) · 6.91 KB
/
replace_util.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import argparse
import os
import time
import in_place
from log import log, Log
from artifact_id_pair import ArtifactIdPair
from version_update_item import VersionUpdateItem
X_VERSION_UPDATE = 'x-version-update'
X_INCLUDE_UPDATE = 'x-include-update'
ARTIFACT_ID_PAIRS = 'artifact_id_pairs'
VERSION_UPDATE_ITEMS = 'version_update_items'
config = {
'appconfiguration': {
'sdk/appconfiguration/azure-spring-cloud-test-appconfiguration-config/pom.xml': {
VERSION_UPDATE_ITEMS: (
VersionUpdateItem('org.springframework.boot:spring-boot-starter-parent', '2.5.0'),
)
}
},
'cosmos': {
'sdk/cosmos/azure-spring-data-cosmos-test/pom.xml': {
VERSION_UPDATE_ITEMS: (
VersionUpdateItem('org.springframework.boot:spring-boot-starter-parent', '2.5.0'),
)
}
},
'spring': {
'sdk/spring/azure-spring-boot-test-parent/pom.xml': {
VERSION_UPDATE_ITEMS: (
VersionUpdateItem('org.springframework.boot:spring-boot-starter-parent', '2.5.0'),
)
},
'sdk/spring/azure-spring-cloud-test-parent/pom.xml': {
VERSION_UPDATE_ITEMS: (
VersionUpdateItem('org.springframework.boot:spring-boot-starter-parent', '2.4.10'),
)
}
}
}
def main():
start_time = time.time()
change_to_root_dir()
log.debug('Current working directory = {}.'.format(os.getcwd()))
args = get_args()
init_log(args)
replace(args.module)
elapsed_time = time.time() - start_time
log.info('elapsed_time = {}'.format(elapsed_time))
def change_to_root_dir():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.chdir('../../..')
def get_args():
parser = argparse.ArgumentParser(
description = 'Replace artifact id in pom file.'
)
parser.add_argument(
'--module',
type = str,
choices = ['spring', 'cosmos', 'appconfiguration'],
required = False,
default = 'cosmos',
help = 'Specify the target module.'
)
parser.add_argument(
'--log',
type = str,
choices = ['debug', 'info', 'warn', 'error', 'none'],
required = False,
default = 'info',
help = 'Set log level.'
)
parser.add_argument(
'--color',
type = str,
choices = ['true', 'false'],
required = False,
default = 'true',
help = 'Whether need colorful log.'
)
return parser.parse_args()
def init_log(args):
log_dict = {
'debug': Log.DEBUG,
'info': Log.INFO,
'warn': Log.WARN,
'error': Log.ERROR,
'none': Log.NONE
}
log.set_log_level(log_dict[args.log])
color_dict = {
'true': True,
'false': False
}
log.set_color(color_dict[args.color])
def replace(module):
"""
Replace action
:param module: module name
"""
for pom in config[module].keys():
replace_artifact_id(module, pom)
replace_version(module, pom)
def get_str(tuple_obj):
"""
Return str list for tuple obj for logger.
:param tuple_obj: tuple obj
:return: string list
"""
str_list = list()
for item in tuple_obj:
str_list.append(str(item))
return str_list
def replace_artifact_id(module, pom):
"""
Replace artifactId in dependency and plugin part.
:param module: module name
:param pom: pom file path
"""
log.debug('Replacing artifact id in file: {}'.format(pom, module))
pom_dict = config[module][pom]
if ARTIFACT_ID_PAIRS not in pom_dict:
log.warn('No config key {} in pom parameters.'.format(ARTIFACT_ID_PAIRS))
return
artifact_id_pairs = pom_dict[ARTIFACT_ID_PAIRS]
log.debug('Module: {}, artifact ids: {}'.format(module, get_str(artifact_id_pairs)))
with in_place.InPlace(pom) as file:
line_num = 0
for line in file:
line_num = line_num + 1
for artifact_id_pair in artifact_id_pairs:
if artifact_id_pair.old_artifact_id in line:
new_line = line.replace(artifact_id_pair.old_artifact_id, artifact_id_pair.new_artifact_id)
log.debug('Updating artifact id in line {}'.format(line_num))
log.debug(' old_line = {}.'.format(line.strip('\n')))
log.debug(' new_line = {}.'.format(new_line.strip('\n')))
line = new_line
file.write(line)
def replace_version(module, pom):
"""
Replace version in dependency and plugin part.
:param module: module name
:param pom: pom file path
"""
log.debug('Replacing version in file: {}'.format(pom))
pom_dict = config[module][pom]
if VERSION_UPDATE_ITEMS not in pom_dict:
log.warn('No config key {} in pom parameters.'.format(VERSION_UPDATE_ITEMS))
return
version_update_items = pom_dict[VERSION_UPDATE_ITEMS]
log.debug('Module: {}, versions: {}'.format(module, get_str(version_update_items)))
with in_place.InPlace(pom) as file:
line_num = 0
for line in file:
line_num = line_num + 1
for version_update_item in version_update_items:
if version_update_item.id in line:
# update version in dependency part
if X_VERSION_UPDATE in line:
old_version = line[(line.index('<version>') + 9):line.index('</version>')]
if old_version != version_update_item.new_version:
new_line = line.replace(old_version, version_update_item.new_version)
log.debug('Updating version of dependency in line {}'.format(line_num))
log.debug(' old_line = {}.'.format(line.strip('\n')))
log.debug(' new_line = {}.'.format(new_line.strip('\n')))
line = new_line
else:
log.warn('The same with new version in dependency part.')
# update version in plugin part
elif X_INCLUDE_UPDATE in line:
old_version = line[(line.index('[') + 1):line.index(']')]
if old_version != version_update_item.new_version:
new_line = line.replace(old_version, version_update_item.new_version)
log.debug('Updating line {}'.format(line_num))
log.debug(' old_line = {}.'.format(line.strip('\n')))
log.debug(' new_line = {}.'.format(new_line.strip('\n')))
line = new_line
else:
log.warn('The same with new version in plugin part.')
file.write(line)
if __name__ == '__main__':
main()