diff --git a/tests/test_config_search_paths.py b/tests/test_config_search_paths.py new file mode 100644 index 0000000..872b060 --- /dev/null +++ b/tests/test_config_search_paths.py @@ -0,0 +1,28 @@ +import sys +import tempfile +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) + +from config_search_paths import build + + +class ConfigSearchPathsTest(unittest.TestCase): + def test_extracts_default_config_paths(self): + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + package = root / "mx_exporter" + package.mkdir() + (package / "__init__.py").write_text( + "default_config_files = ['/opt/maca/etc/default-counters.csv', '/opt/mxn100/etc/default-counters.csv']\n", + encoding="utf-8", + ) + + report = build(root) + + self.assertEqual(report["path_count"], 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/config_search_paths.py b/tools/config_search_paths.py new file mode 100644 index 0000000..a253b61 --- /dev/null +++ b/tools/config_search_paths.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +"""Export the default counter-config search paths used by mx-exporter.""" + +from __future__ import annotations + +import argparse +import ast +import json +from pathlib import Path + + +def build(repo_root: Path) -> dict[str, object]: + init_path = repo_root / "mx_exporter" / "__init__.py" + module = ast.parse(init_path.read_text(encoding="utf-8"), filename=str(init_path)) + for node in ast.walk(module): + if isinstance(node, ast.Assign): + for target in node.targets: + if isinstance(target, ast.Name) and target.id == "default_config_files": + values = [ast.unparse(value) for value in node.value.elts] + return {"path_count": len(values), "paths": values} + raise ValueError("default_config_files not found") + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--repo-root", type=Path, default=Path(".")) + parser.add_argument("--output", type=Path) + args = parser.parse_args() + + text = json.dumps(build(args.repo_root), indent=2, ensure_ascii=False) + if args.output: + args.output.write_text(text + "\n", encoding="utf-8") + else: + print(text) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())