Debian Codegraph @

docs/scripts/generate_examples_tests.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # Copyright (c) 2024-2026 Arista Networks, Inc.
3 # Use of this source code is governed by the Apache License 2.0
4 # that can be found in the LICENSE file.
5 """Generates examples/tests.py."""
6
7 import os
8 from contextlib import redirect_stdout
9 from pathlib import Path
10 from sys import path
11
12 from yaml import YAMLError
13
14 # Override global path to load anta from pwd instead of any installed version.
15 path.insert(0, str(Path(__file__).parents[2]))
16
17 from anta.catalog import AntaCatalog
18
19 examples_tests_path = Path(__file__).parents[2] / "examples" / "tests.yaml"
20
21
22 prev = os.environ.get("TERM", "")
23 os.environ["TERM"] = "dumb"
24 # imported after TERM is set to act upon rich console.
25 from anta.cli.get.commands import tests # noqa: E402
26
27 try:
28 with examples_tests_path.open("w") as f:
29 f.write("---\n")
30 with redirect_stdout(f):
31 # removing the style
32 tests()
33 except SystemExit:
34 pass
35
36 os.environ["TERM"] = prev
37
38 try:
39 _ = AntaCatalog.parse(examples_tests_path)
40 except (TypeError, ValueError, YAMLError, OSError) as e:
41 msg = f"Failed to parse catalog: {e}"
42 raise ValueError(msg) from None