Skip to content

Commit beef96f

Browse files
committed
Move the mike version sorting CLI tool
The `mike` version sorting CLI tool is now in `src/frequenz/repo/config/cli/version/mike/sort.py` instead of in `src/frequenz/repo/config/mkdocs/sort_versions.py`. It can be invoked with `python -m frequenz.repo.config.cli.version.mike.sort`. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent a91b4a3 commit beef96f

File tree

3 files changed

+88
-160
lines changed

3 files changed

+88
-160
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ jobs:
362362
if: steps.mike-version.outputs.version
363363
run: |
364364
git checkout gh-pages
365-
python -m frequenz.repo.config.mkdocs.sort_versions versions.json
365+
python -m frequenz.repo.config.cli.version.mike.sort versions.json
366366
git commit -a -m "Sort versions.json"
367367
368368
- name: Publish site
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Sort `mike`'s `version.json` file with a custom order."""
5+
6+
7+
import dataclasses
8+
import json
9+
import sys
10+
from typing import TextIO
11+
12+
from ....mkdocs.mike import MikeVersionInfo, sort_mike_versions
13+
14+
15+
def _sort(stream_in: TextIO, stream_out: TextIO) -> None:
16+
"""Sort the versions in the given in stream to the given out stream.
17+
18+
Args:
19+
stream_in: The stream to read the versions from.
20+
stream_out: The stream to write the sorted versions to.
21+
"""
22+
versions = json.load(stream_in)
23+
sorted_versions = sort_mike_versions([MikeVersionInfo(**v) for v in versions])
24+
json.dump(sorted_versions, stream_out, separators=(",", ":"))
25+
26+
27+
def _load_and_sort_versions_from(stream: TextIO) -> list[MikeVersionInfo]:
28+
"""Load the versions from the given stream.
29+
30+
Args:
31+
stream: The stream to read the versions from.
32+
33+
Returns:
34+
The loaded versions.
35+
"""
36+
versions = [MikeVersionInfo(**v) for v in json.load(stream)]
37+
return sort_mike_versions(versions)
38+
39+
40+
def _dump_versions_to(versions: list[MikeVersionInfo], stream: TextIO) -> None:
41+
"""Dump the versions to the given stream.
42+
43+
Args:
44+
versions: The versions to dump.
45+
stream: The stream to write the versions to.
46+
"""
47+
json.dump([dataclasses.asdict(v) for v in versions], stream, separators=(",", ":"))
48+
49+
50+
def main() -> None:
51+
"""Sort `mike`'s `version.json` file with a custom order.
52+
53+
The versions are sorted using `sort_versions()`.
54+
55+
If no arguments are given, then the contents are read from stdin and the sorted
56+
versions are printed to stdout.
57+
58+
If one argument is given, then the contents of the file are replaced with the sorted
59+
versions.
60+
61+
If more than one argument is given, then an error is printed to stderr and the
62+
program exits with a non-zero exit code.
63+
"""
64+
match len(sys.argv):
65+
case 1:
66+
_dump_versions_to(_load_and_sort_versions_from(sys.stdin), sys.stdout)
67+
case 2:
68+
with open(sys.argv[1], "r", encoding="utf8") as stream_in:
69+
versions = _load_and_sort_versions_from(stream_in)
70+
with open(sys.argv[1], "w", encoding="utf8") as stream_out:
71+
_dump_versions_to(versions, stream_out)
72+
73+
case _:
74+
print(
75+
f"""\
76+
Usage: {sys.argv[0]} [<versions.json>]
77+
78+
If <versions.json> is given, the contents will be replaced with the sorted versions.
79+
Otherwise, the contents are read from stdin and the sorted versions are printed to stdout.
80+
""",
81+
file=sys.stderr,
82+
)
83+
sys.exit(2)
84+
85+
86+
if __name__ == "__main__":
87+
main()

src/frequenz/repo/config/mkdocs/sort_versions.py

Lines changed: 0 additions & 159 deletions
This file was deleted.

0 commit comments

Comments
 (0)