Skip to content

Commit

Permalink
Issue #4 harden finding of "algorithm_invocations" folder in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jun 19, 2024
1 parent 68e04d8 commit 1aff9ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ repos:
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
41 changes: 39 additions & 2 deletions qa/tools/apex_algorithm_qa_tools/usecases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?
Expand Down
9 changes: 8 additions & 1 deletion qa/unittests/tests/test_usecases.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down

0 comments on commit 1aff9ce

Please sign in to comment.