From d58f616bcb17191905ada84ae33dc6b1e449c201 Mon Sep 17 00:00:00 2001 From: Rudra Pratap Singh Date: Mon, 1 Jul 2024 19:19:28 +0530 Subject: [PATCH] [Extend] version-automation to `rockcraft.yaml` (#636) * extend version-automation to rockcraft.yaml * Correct name to upstream_version and previous_version & use of inline tuple map function * Removed version_tuple function --- .../SnapVersionModule/snap_version_module.py | 50 +++++++++++++++++++ updatesnap/updatesnapyaml.py | 7 ++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/updatesnap/SnapVersionModule/snap_version_module.py b/updatesnap/SnapVersionModule/snap_version_module.py index 0401a0a..92ce07a 100644 --- a/updatesnap/SnapVersionModule/snap_version_module.py +++ b/updatesnap/SnapVersionModule/snap_version_module.py @@ -62,6 +62,29 @@ def process_snap_version_data(upstreamversion, snap_name, version_schema, has_up return f"{upstreamversion}-{packagerelease}" +def process_rock_version_data(upstream_version, previous_version, version_schema, has_update): + """ Returns processed rock version""" + + match = re.match(version_schema, upstream_version) + if not match: + logging.warning("Version schema does not match with rock repository version") + return None + upstream_version = match.group(1).replace('_', '.') + + upstream_tuple = tuple(map(int, upstream_version.split('.'))) + prev_tuple = tuple(map(int, previous_version.split('-')[0].split('.'))) + + if upstream_tuple > prev_tuple: + return f"{upstream_version}-1" + # Determine package release number + if has_update: + packagerelease = int(previous_version.split('-')[-1]) + 1 + else: + packagerelease = int(previous_version.split('-')[-1]) + + return f"{upstream_version}-{packagerelease}" + + def is_version_update(snap, manager_yaml, arguments, has_update): """ Returns if snap version update available """ has_version_update = False @@ -87,3 +110,30 @@ def is_version_update(snap, manager_yaml, arguments, has_update): version_file.write(f"{snap_version}") return has_version_update + + +def is_rock_version_update(rock, manager_yaml, arguments, has_update): + """ Returns if rock version update available """ + has_version_update = False + if arguments.rock_version_schema == 'None': + return False + metadata = rock.process_metadata() + rock_version = process_rock_version_data(metadata['upstream-version'], metadata['version'], + arguments.rock_version_schema, has_update) + if rock_version is None: + return False + if metadata['version'] != rock_version: + rock_version_data = manager_yaml.get_part_metadata('version') + if rock_version_data is not None: + logging.info("Updating rock version from %s to %s", + metadata['version'], rock_version) + rock_version_data['data'] = f"version: '{rock_version}'" + has_version_update = True + else: + logging.warning("Version is not defined in metadata") + + if has_version_update: + with open('version_file', 'w', encoding="utf8") as version_file: + version_file.write(f"{rock_version}") + + return has_version_update diff --git a/updatesnap/updatesnapyaml.py b/updatesnap/updatesnapyaml.py index 755c72d..6f6c8b4 100755 --- a/updatesnap/updatesnapyaml.py +++ b/updatesnap/updatesnapyaml.py @@ -7,7 +7,7 @@ import logging from SnapModule.snapmodule import Snapcraft, Github from SnapModule.manageYAML import ManageYAML -from SnapVersionModule.snap_version_module import is_version_update +from SnapVersionModule.snap_version_module import is_version_update, is_rock_version_update UPDATE_BRANCH = 'update_versions' @@ -74,6 +74,8 @@ def main(): help='Access token for accesing Github projects.') parser.add_argument('--version-schema', action='store', default='None', help='Version schema of snapping repository') + parser.add_argument('--rock-version-schema', action='store', default='None', + help='Version schema of rock repository') parser.add_argument('--verbose', action='store_true', default=False) parser.add_argument('project', default='.', help='The project URI') arguments = parser.parse_args(sys.argv[1:]) @@ -124,7 +126,8 @@ def main(): has_update = True logging.basicConfig(level=logging.INFO) - if (is_version_update(snap, manager_yaml, arguments, has_update) or has_update): + if (is_version_update(snap, manager_yaml, arguments, has_update) or + is_rock_version_update(snap, manager_yaml, arguments, has_update) or has_update): with open('output_file', 'w', encoding="utf8") as output_file: output_file.write(manager_yaml.get_yaml()) else: