forked from eiffel-community/eiffel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.py
75 lines (66 loc) · 2.53 KB
/
versions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright 2024 Axis Communications AB.
# For a full list of individual contributors, please see the commit history.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The versions module contains functions for discovering definition
files and schema files.
"""
import os
import subprocess
from pathlib import Path
from typing import Dict
from typing import Iterable
import semver
def latest_in_gitref(
committish: str, gitdir: Path, subdir: Path
) -> Dict[str, semver.version.Version]:
"""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)
for line in (
subprocess.check_output(
["git", "ls-tree", "-r", "--name-only", committish, "--", subdir],
cwd=gitdir,
)
.decode("utf-8")
.splitlines()
)
if Path(line).suffix in (".json", ".yml")
)
def latest_in_dir(path: Path) -> Dict[str, semver.version.Version]:
"""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 in (".json", ".yml")
)
def _latest_versions(paths: Iterable[Path]) -> Dict[str, semver.version.Version]:
"""Given a list of foo/<typename>/<version>.<ext> pathnames, returns
a dict mapping typenames to the most recent version of that type.
"""
result = {}
for path in paths:
type = path.parent.name
this_version = semver.VersionInfo.parse(Path(path.name).stem)
if type not in result or result[type] < this_version:
result[type] = this_version
return result