From 8ab5e01073f13bca5f2250a63f453ad05b0d96fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20B=C3=A4ck?= Date: Fri, 19 Jan 2024 08:49:43 +0100 Subject: [PATCH] generate_manifest.py: Use versions module (#393) When the versions module was introduced in commit e4762e9, the code in generate_manifest.py for listing the latest version of each type went unnoticed. With a slight change to versions.py to allow discovering schema file and not just definitions files we were able to use that module and delete some code. --- generate_manifest.py | 37 +++++++++++-------------------------- versions.py | 23 +++++++++++++---------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/generate_manifest.py b/generate_manifest.py index 44a0e6c9..182ea873 100755 --- a/generate_manifest.py +++ b/generate_manifest.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Copyright 2023 Axis Communications AB and others. +# Copyright 2023-2024 Axis Communications AB and others. # For a full list of individual contributors, please see the commit history. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,17 +15,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import re -import subprocess import sys from functools import cached_property -from typing import Dict +from pathlib import Path from typing import Optional import semver from ruamel import yaml from semver import Version +import versions + # List of tuples with the edition display names, their Git tags, and # their release dates. _EDITIONS = [ @@ -117,34 +117,19 @@ def is_in_edition(self, edition_tag: str, event_name: str, event_version: str): ) -def _get_latest_schemas(tag: str) -> Dict[str, str]: - """Given a tag, returns a mapping of the event types available in that - tag and the latest version of each such type. - """ - schema_file_regexp = re.compile(r"^schemas/([^/]+)/([^/]+).json$") - latest = {} - for schema_file in subprocess.check_output( - ["git", "ls-tree", "-r", "--name-only", tag, "--", "schemas"] - ).splitlines(): - match = schema_file_regexp.search(schema_file.decode("utf-8")) - if not match: - continue - event_type = match.group(1) - event_version = semver.VersionInfo.parse(match.group(2)) - if event_type not in latest or latest[event_type].compare(event_version) < 0: - latest[event_type] = event_version - return { - event_type: str(event_version) for event_type, event_version in latest.items() - } - - def _main(): manifest = [ { "name": name, "tag": tag, "release_date": date, - "events": _get_latest_schemas(tag), + "events": { + # YAML module can't serialize a semver.version.Version. + k: str(v) + for k, v in versions.latest_in_gitref( + tag, Path("."), Path("schemas") + ).items() + }, } for name, tag, date in sorted(_EDITIONS, key=lambda edition: edition[2]) ] diff --git a/versions.py b/versions.py index f250e01d..36e63f2c 100644 --- a/versions.py +++ b/versions.py @@ -13,7 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""The versions module contains functions for discovering definition files.""" +"""The versions module contains functions for discovering definition +files and schema files. +""" import os import subprocess @@ -27,10 +29,11 @@ def latest_in_gitref( committish: str, gitdir: Path, subdir: Path ) -> Dict[str, semver.version.Version]: - """Lists the definition files found under a given subdirectory of a - git at a given point in time (described by a committish, e.g. a - SHA-1, tag, or branch reference) and returns a dict that maps each - typename (e.g. EiffelArtifactCreatedEvent) to the latest version found. + """Lists the definition or schema files found under a given + subdirectory of a git at a given point in time (described by a + committish, e.g. a SHA-1, tag, or branch reference) and returns a + dict that maps each typename (e.g. EiffelArtifactCreatedEvent) to + the latest version found. """ return _latest_versions( Path(line) @@ -42,20 +45,20 @@ def latest_in_gitref( .decode("utf-8") .splitlines() ) - if Path(line).suffix == ".yml" + if Path(line).suffix in (".json", ".yml") ) def latest_in_dir(path: Path) -> Dict[str, semver.version.Version]: - """Inspects the definition files found under a given path and returns - a dict that maps each typename (e.g. EiffelArtifactCreatedEvent) to - its latest version found. + """Inspects the definition or schema files found under + a given path and returns a dict that maps each typename + (e.g. EiffelArtifactCreatedEvent) to its latest version found. """ return _latest_versions( Path(current) / f for current, _, files in os.walk(path) for f in files - if Path(f).suffix == ".yml" + if Path(f).suffix in (".json", ".yml") )