Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate_manifest.py: Use versions module #393

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions generate_manifest.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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