Skip to content

Commit

Permalink
generate_manifest.py: Use versions module (#393)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magnusbaeck committed Jan 19, 2024
1 parent da53778 commit 8ab5e01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
37 changes: 11 additions & 26 deletions 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");
Expand All @@ -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 = [
Expand Down Expand Up @@ -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])
]
Expand Down
23 changes: 13 additions & 10 deletions versions.py
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
)


Expand Down

0 comments on commit 8ab5e01

Please sign in to comment.