Fix: pre-commit and tox issues
[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 """Main function for branch cutting a new stable release."""
7
8 import re
9 import requests
10 import python_lib
11
12 # pylint: disable=wrong-import-order
13 from pathlib import Path
14 from bs4 import BeautifulSoup
15
16 # from lxml import etree
17 from defusedxml import lxml as etree
18
19 """Get the version from the groupId and artifactId."""
20
21
22 def get_version_for_artifact(group_id, artifact_id):
23     """Retrive version number from the groupId and artifactId."""
24     versions_list = []
25     url = f"https://repo1.maven.org/maven2/org/opendaylight/{group_id}/{artifact_id}/"
26     response = requests.get(url).content
27     soup = BeautifulSoup(response, "html.parser")
28     try:
29         html_lines = str(soup.find_all("pre")[0]).splitlines()
30     except IndexError:
31         return "NOT FOUND"
32     for line in html_lines:
33         # Use a regular expression to find version
34         pattern = re.compile(r"\d+\.\d+\.\d+")
35         title = pattern.search(line)
36         try:
37             versions_list.append(title.group())
38         except AttributeError:
39             pass
40     return python_lib.find_highest_revision(versions_list)
41
42
43 # get all xml files
44 for path in Path(python_lib.bumping_dir).rglob("*.xml"):
45     if "test/resources" in str(path):
46         continue
47     parser = etree.XMLParser(resolve_entities=False, no_network=True)
48     tree = etree.parse(path, parser)
49     root = tree.getroot()
50     # update major and minor artifacts versions
51     if "pom.xml" in str(path):
52         prefix = "{" + root.nsmap[None] + "}"
53         # line above can trigger a 'KeyError: None' outside pom.xml and
54         # feature.xml files.
55         all_elements = tree.findall(f".//{prefix}parent") + tree.findall(
56             f".//{prefix}dependency"
57         )
58         for element in all_elements:
59             group_id_elem = element.find(f"{prefix}groupId")
60             artifact_id_elem = element.find(f"{prefix}artifactId")
61             version = element.find(f"{prefix}version")
62             try:
63                 if "org.opendaylight" in group_id_elem.text and version is not None:
64                     # skip artifacts containing items in skipped list
65                     skipped = ["${project.version}", "SNAPSHOT", "@project.version@"]
66                     if not any(x in version.text for x in skipped):
67                         new_version = get_version_for_artifact(
68                             group_id_elem.text.split(".")[2], artifact_id_elem.text
69                         )
70                         if python_lib.check_minor_version(version, new_version):
71                             print(
72                                 python_lib.log_artifact(
73                                     path,
74                                     group_id_elem,
75                                     artifact_id_elem,
76                                     version.text,
77                                     new_version,
78                                 )
79                             )
80                             version.text = new_version
81                             tree.write(
82                                 path,
83                                 encoding="UTF-8",
84                                 pretty_print=True,
85                                 doctype='<?xml version="1.0" encoding="UTF-8"?>',
86                             )
87             except AttributeError:
88                 pass
89     # update feature versions
90     if "feature.xml" in str(path):
91         prefix = "{" + root.nsmap[None] + "}"
92         # line above can trigger a 'KeyError: None' outside pom.xml and
93         # feature.xml files.
94         all_features = tree.findall(f".//{prefix}feature")
95         # feature versions add +1
96         for feature in all_features:
97             try:
98                 if (
99                     feature.attrib["version"]
100                     and feature.attrib["version"] != "${project.version}"
101                 ):
102                     current_version = feature.attrib["version"]
103                     # workaround for float feature versions
104                     nums = current_version[1:-1].split(",")
105                     if "." in nums[0]:
106                         nums[0] = str(round((float(nums[0]) + 0.01), 2))
107                     else:
108                         nums[0] = str(int(nums[0]) + 1)
109                         nums[1] = str(int(nums[1]) + 1)
110                     result = "[" + ",".join(nums) + ")"
111                     feature.attrib["version"] = result
112                     print(
113                         python_lib.log_artifact(
114                             path=path, version=current_version, new_version=result
115                         )
116                     )
117                     standalone = ""
118                     if tree.docinfo.standalone:
119                         standalone = ' standalone="yes"'
120                     tree.write(
121                         path,
122                         encoding="UTF-8",
123                         pretty_print=True,
124                         doctype=f'<?xml version="1.0" encoding="UTF-8"{standalone}?>',
125                     )
126             except KeyError:
127                 pass