|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +"""Shared discovery helpers for the static validation tests. |
| 3 | +
|
| 4 | +Everything here is filesystem-only so the tests run fast and need no network or |
| 5 | +AWS credentials. Discovery walks the repository from its root (the parent of the |
| 6 | +``tests`` directory) and deliberately skips directories that hold scratch or |
| 7 | +vendored copies (for example ``.claude`` worktrees and ``.git``) so those never |
| 8 | +affect CI results. |
| 9 | +""" |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import re |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 16 | + |
| 17 | +# Directories anywhere in the tree whose contents are not part of the samples we |
| 18 | +# ship and should never be validated. |
| 19 | +_EXCLUDED_DIR_NAMES = {".git", ".claude", ".kiro", "node_modules", "__pycache__", "build"} |
| 20 | + |
| 21 | +# Matches the OpenJD ``specificationVersion`` header of a standalone template. |
| 22 | +# Anchored at column 0 (no leading whitespace) on purpose: a standalone OpenJD |
| 23 | +# template file has this as a top-level key, whereas a template *embedded* inside |
| 24 | +# another document (for example an environment template nested in a |
| 25 | +# CloudFormation resource) is indented. Only standalone template files are |
| 26 | +# validated with ``openjd check``. |
| 27 | +_SPEC_VERSION_RE = re.compile( |
| 28 | + r"""^specificationVersion\s*:\s*['"]?(?P<version>[A-Za-z0-9._-]+)""", |
| 29 | + re.MULTILINE, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def _is_excluded(path: Path) -> bool: |
| 34 | + return any(part in _EXCLUDED_DIR_NAMES for part in path.parts) |
| 35 | + |
| 36 | + |
| 37 | +def _iter_yaml_files() -> list[Path]: |
| 38 | + files = [] |
| 39 | + for pattern in ("*.yaml", "*.yml"): |
| 40 | + for path in REPO_ROOT.rglob(pattern): |
| 41 | + if not _is_excluded(path.relative_to(REPO_ROOT)): |
| 42 | + files.append(path) |
| 43 | + return sorted(set(files)) |
| 44 | + |
| 45 | + |
| 46 | +def spec_version(path: Path) -> str | None: |
| 47 | + """Return the OpenJD ``specificationVersion`` of a YAML file, or ``None``. |
| 48 | +
|
| 49 | + Reads only the head of the file; the header appears at the top of every |
| 50 | + OpenJD template. |
| 51 | + """ |
| 52 | + try: |
| 53 | + head = path.read_text(encoding="utf-8", errors="replace")[:4096] |
| 54 | + except OSError: |
| 55 | + return None |
| 56 | + match = _SPEC_VERSION_RE.search(head) |
| 57 | + return match.group("version") if match else None |
| 58 | + |
| 59 | + |
| 60 | +def find_openjd_templates() -> list[Path]: |
| 61 | + """All OpenJD job and environment templates in the repository.""" |
| 62 | + return [ |
| 63 | + p |
| 64 | + for p in _iter_yaml_files() |
| 65 | + if (v := spec_version(p)) is not None |
| 66 | + and (v.startswith("jobtemplate-") or v.startswith("environment-")) |
| 67 | + ] |
| 68 | + |
| 69 | + |
| 70 | +def find_job_templates() -> list[Path]: |
| 71 | + return [p for p in find_openjd_templates() if (spec_version(p) or "").startswith("jobtemplate-")] |
| 72 | + |
| 73 | + |
| 74 | +def find_environment_templates() -> list[Path]: |
| 75 | + return [p for p in find_openjd_templates() if (spec_version(p) or "").startswith("environment-")] |
| 76 | + |
| 77 | + |
| 78 | +def find_host_configuration_scripts() -> list[Path]: |
| 79 | + """Host configuration shell / PowerShell scripts.""" |
| 80 | + base = REPO_ROOT / "host_configuration_scripts" |
| 81 | + if not base.is_dir(): |
| 82 | + return [] |
| 83 | + scripts = [] |
| 84 | + for pattern in ("*.sh", "*.ps1"): |
| 85 | + for path in base.rglob(pattern): |
| 86 | + if not _is_excluded(path.relative_to(REPO_ROOT)): |
| 87 | + scripts.append(path) |
| 88 | + return sorted(set(scripts)) |
| 89 | + |
| 90 | + |
| 91 | +def find_cloudformation_templates() -> list[Path]: |
| 92 | + """CloudFormation templates (YAML files under ``cloudformation/``). |
| 93 | +
|
| 94 | + Excludes the OpenJD job templates that happen to live under that tree (for |
| 95 | + example the ``test-job.yaml`` samples) since those are validated by the |
| 96 | + OpenJD checks instead. |
| 97 | + """ |
| 98 | + base = REPO_ROOT / "cloudformation" |
| 99 | + if not base.is_dir(): |
| 100 | + return [] |
| 101 | + templates = [] |
| 102 | + for pattern in ("*.yaml", "*.yml"): |
| 103 | + for path in base.rglob(pattern): |
| 104 | + rel = path.relative_to(REPO_ROOT) |
| 105 | + if _is_excluded(rel) or spec_version(path) is not None: |
| 106 | + continue |
| 107 | + templates.append(path) |
| 108 | + return sorted(set(templates)) |
| 109 | + |
| 110 | + |
| 111 | +def find_conda_recipe_dirs() -> list[Path]: |
| 112 | + """Directories under ``conda_recipes/`` that contain a ``deadline-cloud.yaml``.""" |
| 113 | + base = REPO_ROOT / "conda_recipes" |
| 114 | + if not base.is_dir(): |
| 115 | + return [] |
| 116 | + dirs = [] |
| 117 | + for path in base.rglob("deadline-cloud.yaml"): |
| 118 | + rel = path.relative_to(REPO_ROOT) |
| 119 | + if not _is_excluded(rel): |
| 120 | + dirs.append(path.parent) |
| 121 | + return sorted(set(dirs)) |
| 122 | + |
| 123 | + |
| 124 | +def rel(path: Path) -> str: |
| 125 | + """Repo-relative string form of a path, for readable test ids.""" |
| 126 | + try: |
| 127 | + return str(path.relative_to(REPO_ROOT)) |
| 128 | + except ValueError: |
| 129 | + return str(path) |
0 commit comments