diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 938901f..a885181 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,3 +22,4 @@ repos: rev: 5.13.2 hooks: - id: isort + args: ["--profile", "black", "--filter-files"] diff --git a/qa/tools/apex_algorithm_qa_tools/usecases.py b/qa/tools/apex_algorithm_qa_tools/usecases.py index ce7570d..eaa5a98 100644 --- a/qa/tools/apex_algorithm_qa_tools/usecases.py +++ b/qa/tools/apex_algorithm_qa_tools/usecases.py @@ -2,10 +2,12 @@ import dataclasses import json +import logging +import os from pathlib import Path from typing import List -_PROJECT_ROOT = Path(__file__).parent.parent.parent.parent +_log = logging.getLogger(__name__) @dataclasses.dataclass(kw_only=True) @@ -28,10 +30,45 @@ def from_dict(cls, data: dict) -> UseCase: ) +APEX_ALGORITHM_INVOCATIONS_ROOT = "APEX_ALGORITHM_INVOCATIONS_ROOT" + + +def get_algorithm_invocation_root() -> Path: + # TODO: find a better name for "algorithm invocations"? + # First attempt: check environment variable + if APEX_ALGORITHM_INVOCATIONS_ROOT in os.environ: + algo_root = os.environ[APEX_ALGORITHM_INVOCATIONS_ROOT] + _log.info( + f"Using algorithm invocations root {algo_root!r} (from environment variable {APEX_ALGORITHM_INVOCATIONS_ROOT})" + ) + return Path(algo_root) + + # Next attempts: try to detect project root + for project_root_candidate in [ + # Running from project root? + Path.cwd(), + # Running from qa/tools, qa/benchmarks, qa/unittests? + Path.cwd().parent.parent, + # Search from current file + Path(__file__).parent.parent.parent.parent, + ]: + if project_root_candidate.is_dir() and all( + (project_root_candidate / p).is_dir() + for p in ["algorithm_invocations", "qa/tools"] + ): + algo_root = project_root_candidate / "algorithm_invocations" + _log.info( + f"Using algorithm invocations root {algo_root!r} (assuming project root {project_root_candidate!r})" + ) + return algo_root + + raise RuntimeError("Could not determine algorithm invocations root directory.") + + def get_use_cases() -> List[UseCase]: # TODO: instead of flat list, keep original grouping/structure of "algorithm_invocations" files? use_cases = [] - for path in (_PROJECT_ROOT / "algorithm_invocations").glob("*.json"): + for path in get_algorithm_invocation_root().glob("*.json"): with open(path) as f: data = json.load(f) # TODO: support single use case files in addition to listings? diff --git a/qa/unittests/tests/test_usecases.py b/qa/unittests/tests/test_usecases.py index 3d4cbf5..a746b71 100644 --- a/qa/unittests/tests/test_usecases.py +++ b/qa/unittests/tests/test_usecases.py @@ -1,4 +1,11 @@ -from apex_algorithm_qa_tools.usecases import get_use_cases +from apex_algorithm_qa_tools.usecases import ( + get_algorithm_invocation_root, + get_use_cases, +) + + +def test_get_algorithm_invocation_root(): + assert get_algorithm_invocation_root().is_dir() def test_get_use_cases():