|
11 | 11 | import requests |
12 | 12 | import tempfile |
13 | 13 | import subprocess |
14 | | -import collections |
15 | 14 | import urllib.parse |
16 | 15 | from typing import Tuple |
17 | 16 |
|
18 | 17 | pwd = os.getcwd() |
19 | 18 | os.chdir(os.path.abspath(os.path.dirname(sys.argv[0]))) |
20 | 19 | from parameters import * |
| 20 | +from utils import update_service_ci_and_pom |
| 21 | +from utils import update_root_pom |
| 22 | +from utils import update_version |
21 | 23 | from generate_data import sdk_automation as sdk_automation_data |
22 | 24 | os.chdir(pwd) |
23 | 25 |
|
@@ -76,9 +78,12 @@ def generate( |
76 | 78 | logging.error('[GENERATE] Autorest fail') |
77 | 79 | return False |
78 | 80 |
|
79 | | - update_service_ci_and_pom(sdk_root, service) |
| 81 | + module = ARTIFACT_FORMAT.format(service) |
| 82 | + group = GROUP_ID |
| 83 | + output_folder = OUTPUT_FOLDER_FORMAT.format(service) |
| 84 | + update_service_ci_and_pom(sdk_root, group, service, module) |
80 | 85 | update_root_pom(sdk_root, service) |
81 | | - update_version(sdk_root, service) |
| 86 | + update_version(sdk_root, output_folder) |
82 | 87 |
|
83 | 88 | return True |
84 | 89 |
|
@@ -189,145 +194,6 @@ def compare_with_maven_package(sdk_root, service, stable_version, |
189 | 194 | os.remove(old_jar) |
190 | 195 |
|
191 | 196 |
|
192 | | -def add_module_to_modules(modules: str, module: str) -> str: |
193 | | - post_module = re.search(r'([^\S\n\r]*)</modules>', modules) |
194 | | - indent = post_module.group(1) |
195 | | - indent += ' ' |
196 | | - |
197 | | - all_module = set(re.findall(r'<module>(.*?)</module>', modules)) |
198 | | - all_module.add(module) |
199 | | - all_module = [ |
200 | | - indent + POM_MODULE_FORMAT.format(module) |
201 | | - for module in sorted(all_module) |
202 | | - ] |
203 | | - |
204 | | - return '<modules>\n' + ''.join(all_module) + post_module.group() |
205 | | - |
206 | | - |
207 | | -def add_module_to_default_profile(pom: str, module: str) -> Tuple[bool, str]: |
208 | | - for profile in re.finditer(r'<profile>[\s\S]*?</profile>', pom): |
209 | | - profile_value = profile.group() |
210 | | - if re.search(r'<id>default</id>', profile_value): |
211 | | - if len(re.findall('<modules>', profile_value)) > 1: |
212 | | - logging.error( |
213 | | - '[POM][Profile][Skip] find more than one <modules> in <profile> default' |
214 | | - ) |
215 | | - return (False, '') |
216 | | - modules = re.search(r'<modules>[\s\S]*</modules>', profile_value) |
217 | | - if not modules: |
218 | | - logging.error( |
219 | | - '[POM][Profile][Skip] Cannot find <modules> in <profile> default' |
220 | | - ) |
221 | | - return (False, '') |
222 | | - modules_update = add_module_to_modules(modules.group(), module) |
223 | | - pre_modules = pom[:profile.start() + modules.start()] |
224 | | - post_modules = pom[profile.start() + modules.end():] |
225 | | - return (True, pre_modules + modules_update + post_modules) |
226 | | - logging.error( |
227 | | - '[POM][Profile][Skip] cannot find <profile> with <id> default') |
228 | | - return (False, '') |
229 | | - |
230 | | - |
231 | | -def add_module_to_pom(pom: str, module: str) -> Tuple[bool, str]: |
232 | | - if pom.find('<module>{0}</module>'.format(module)) >= 0: |
233 | | - logging.info('[POM][Skip] pom already has module {0}'.format(module)) |
234 | | - return (True, pom) |
235 | | - |
236 | | - if len(re.findall('<modules>', pom)) > 1: |
237 | | - if pom.find('<profiles>') >= 0: |
238 | | - return add_module_to_default_profile(pom, module) |
239 | | - logging.error('[POM][Skip] find more than one <modules> in pom') |
240 | | - return (False, '') |
241 | | - |
242 | | - modules = re.search(r'<modules>[\s\S]*</modules>', pom) |
243 | | - if not modules: |
244 | | - logging.error('[POM][Skip] Cannot find <modules> in pom') |
245 | | - return (False, '') |
246 | | - |
247 | | - modules_update = add_module_to_modules(modules.group(), module) |
248 | | - pre_modules = pom[:modules.start()] |
249 | | - post_modules = pom[modules.end():] |
250 | | - return (True, pre_modules + modules_update + post_modules) |
251 | | - |
252 | | - |
253 | | -def update_root_pom(sdk_root: str, service: str): |
254 | | - pom_file = os.path.join(sdk_root, 'pom.xml') |
255 | | - if not os.path.exists(pom_file): |
256 | | - logging.error('[POM][Skip] cannot find root pom') |
257 | | - return |
258 | | - |
259 | | - module = 'sdk/{0}'.format(service) |
260 | | - with open(pom_file, 'r') as fin: |
261 | | - pom = fin.read() |
262 | | - |
263 | | - logging.info('[POM][Process] dealing with root pom') |
264 | | - success, pom = add_module_to_pom(pom, module) |
265 | | - if success: |
266 | | - with open(pom_file, 'w') as fout: |
267 | | - fout.write(pom) |
268 | | - logging.info('[POM][Success] Write to root pom') |
269 | | - |
270 | | - |
271 | | -def update_service_ci_and_pom(sdk_root: str, service: str): |
272 | | - folder = os.path.join(sdk_root, 'sdk/{0}'.format(service)) |
273 | | - module = ARTIFACT_FORMAT.format(service) |
274 | | - ci_yml_file = os.path.join(folder, 'ci.yml') |
275 | | - pom_xml_file = os.path.join(folder, 'pom.xml') |
276 | | - |
277 | | - if os.path.exists(ci_yml_file): |
278 | | - with open(ci_yml_file, 'r') as fin: |
279 | | - ci_yml = yaml.safe_load(fin) |
280 | | - sdk_type: str = ci_yml.get('extends', dict()).get('parameters', dict()).get('SDKType', '') |
281 | | - if type(sdk_type) == str and sdk_type.lower() == 'data': |
282 | | - os.rename(ci_yml_file, os.path.join(os.path.dirname(ci_yml_file), 'ci.data.yml')) |
283 | | - ci_yml = yaml.safe_load(CI_FORMAT.format(service, module)) |
284 | | - else: |
285 | | - ci_yml = yaml.safe_load(CI_FORMAT.format(service, module)) |
286 | | - |
287 | | - if not (type(ci_yml.get('extends')) == dict and |
288 | | - type(ci_yml['extends'].get('parameters')) == dict and |
289 | | - type(ci_yml['extends']['parameters'].get('Artifacts')) == list): |
290 | | - logging.error('[CI][Skip] Unexpected ci.yml format') |
291 | | - else: |
292 | | - artifacts: list = ci_yml['extends']['parameters']['Artifacts'] |
293 | | - for artifact in artifacts: |
294 | | - if (artifact.get('name') == module and |
295 | | - artifact.get('groupId') == GROUP_ID): |
296 | | - logging.info( |
297 | | - '[CI][Skip] ci.yml already has module {0}'.format(module)) |
298 | | - break |
299 | | - else: |
300 | | - artifacts.append({ |
301 | | - 'name': module, |
302 | | - 'groupId': GROUP_ID, |
303 | | - 'safeName': module.replace('-', '') |
304 | | - }) |
305 | | - ci_yml_str = yaml.dump(ci_yml, |
306 | | - sort_keys = False, |
307 | | - Dumper = ListIndentDumper) |
308 | | - ci_yml_str = re.sub('(\n\S)', r'\n\1', ci_yml_str) |
309 | | - |
310 | | - with open(ci_yml_file, 'w') as fout: |
311 | | - fout.write(CI_HEADER) |
312 | | - fout.write(ci_yml_str) |
313 | | - logging.info('[CI][Success] Write to ci.yml') |
314 | | - |
315 | | - if os.path.exists(pom_xml_file): |
316 | | - with open(pom_xml_file, 'r') as fin: |
317 | | - pom_xml = fin.read() |
318 | | - else: |
319 | | - pom_xml = POM_FORMAT.format(service = service, |
320 | | - group_id = GROUP_ID, |
321 | | - artifact_id = module) |
322 | | - |
323 | | - logging.info('[POM][Process] dealing with pom.xml') |
324 | | - success, pom_xml = add_module_to_pom(pom_xml, module) |
325 | | - if success: |
326 | | - with open(pom_xml_file, 'w') as fout: |
327 | | - fout.write(pom_xml) |
328 | | - logging.info('[POM][Success] Write to pom.xml') |
329 | | - |
330 | | - |
331 | 197 | def get_version( |
332 | 198 | sdk_root: str, |
333 | 199 | service: str, |
|
0 commit comments