Feat: add a script to script/bump_mri_versions
[releng/builder.git] / scripts / bump_mri_versions / main.py
1 # Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
2 #
3 # This program and the accompanying materials are made available under the
4 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 # and is available at http://www.eclipse.org/legal/epl-v10.html
6
7 import re
8 import requests
9 import python_lib
10 # pylint: disable=wrong-import-order
11 from pathlib import Path
12 from bs4 import BeautifulSoup
13 from lxml import etree
14
15
16 def get_version_for_artifact(group_id_elem, artifact_id_elem):
17     versions_list = []
18     url = f'https://repo1.maven.org/maven2/org/opendaylight/{group_id_elem}/{artifact_id_elem}/'
19     response = requests.get(url).content
20     soup = BeautifulSoup(response, 'html.parser')
21     try:
22         html_lines = str(soup.find_all('pre')[0]).splitlines()
23     except IndexError:
24         return "NOT FOUND"
25     for line in html_lines:
26         # Use a regular expression to find version
27         pattern = re.compile(r'\d+\.\d+\.\d+')
28         title = pattern.search(line)
29         try:
30             versions_list.append(title.group())
31         except AttributeError:
32             pass
33     return python_lib.find_highest_revision(versions_list)
34
35
36 # get all xml files
37 for path in Path(python_lib.bumping_dir).rglob('*.xml'):
38     if ("pom.xml" or "feature.xml" in str(path)):
39         if "test/resources" not in str(path):
40             tree = etree.parse(path)
41             root = tree.getroot()
42             # update major and minor artifacts versions
43             if "pom.xml" in str(path):
44                 prefix = "{" + root.nsmap[None] + "}"
45                 all_elements = tree.findall(
46                     f'.//{prefix}parent') + tree.findall(f'.//{prefix}dependency')
47                 for element in all_elements:
48                     group_id_elem = (element.find(f'{prefix}groupId'))
49                     artifact_id_elem = (element.find(f'{prefix}artifactId'))
50                     version = (element.find(f'{prefix}version'))
51                     try:
52                         if "org.opendaylight" in group_id_elem.text and version is not None:
53                             # skip artifacts containing items in skipped list
54                             skipped = ["${project.version}",
55                                     "SNAPSHOT", "@project.version@"]
56                             if not any(x in version.text for x in skipped):
57                                 new_version = get_version_for_artifact(
58                                     group_id_elem.text.split(".")[2], artifact_id_elem.text)
59                                 if python_lib.check_minor_version(version, new_version):
60                                     print(python_lib.log_artifact(
61                                         path, group_id_elem, artifact_id_elem, version.text, new_version))
62                                     version.text = new_version
63                                     tree.write(path, encoding="UTF-8", pretty_print=True,
64                                             doctype='<?xml version="1.0" encoding="UTF-8"?>')
65                     except AttributeError:
66                         pass
67
68         # update feature versions
69         if "feature.xml" in str(path):
70             prefix = "{" + root.nsmap[None] + "}"
71             all_featuress = tree.findall(f'.//{prefix}feature')
72
73             # feature versions add +1
74             for feature in all_featuress:
75                 try:
76                     if feature.attrib["version"] and feature.attrib["version"] != "${project.version}":
77                         current_version = feature.attrib["version"]
78                         # workaround for float feature versions
79                         nums = current_version[1:-1].split(',')
80                         if "." in nums[0]:
81                             nums[0] = str(round((float(nums[0]) + 0.01), 2))
82                         else:
83                             nums[0], nums[1] = str(
84                                 int(nums[0]) + 1), str(int(nums[1])+1)
85                         result = '[' + ','.join(nums) + ')'
86                         feature.attrib["version"] = result
87                         print(python_lib.log_artifact(
88                             path=path, version=current_version, new_version=result))
89                         standalone = ''
90                         if tree.docinfo.standalone:
91                             standalone = ' standalone="yes"'
92                         tree.write(path, encoding="UTF-8", pretty_print=True,
93                                    doctype=f'<?xml version="1.0" encoding="UTF-8"{standalone}?>')
94                 except KeyError:
95                     pass