Source code for simtools.model_repository.dependency_versions

"""Read, validate, and export the simtools dependency version catalog."""

import json
import os
import re
import sys
import tomllib
from copy import deepcopy
from functools import cache
from pathlib import Path

import yaml

from simtools import version as versioning
from simtools.model_repository.compatibility import (
    LEGACY_MODEL_KEY,
    MODEL_KEY,
    RANGE_KEY,
    get_contract,
)

DEPENDENCY_VERSIONS_FILENAME = "dependency_versions.yml"
PYPROJECT_FILENAME = "pyproject.toml"
SHA256_PATTERN = re.compile(r"^sha256:[0-9a-f]{64}$")
ARCHIVE_SHA256_PATTERN = re.compile(r"^[0-9a-f]{64}$")
SIMTOOLS_TESTS_REPOSITORY_PATTERN = re.compile(r"^[^/]+/[^/]+$")
CORSIKA_TAG_PATTERN = re.compile(r"^v\d+\.\d+$")
SOURCE_REF_PATTERN = re.compile(
    r"^(?![-/.])(?!HEAD$)(?!.*[\x00-\x20\x7f~^:?*\[\\])(?!.*\.\.)(?!.*//)"
    r"(?!.*@\{)(?!.*(?:/\.|\.lock(?:/|$)))(?!.*[./]$).+$"
)
IMAGE_TAG_PATTERN = re.compile(r"^\w[\w.-]{0,127}$", re.ASCII)
CORSIKA_INTERACTION_TABLES_LABEL = "CORSIKA interaction tables"
SOURCE_SNAPSHOT_REGISTRY = "ghcr.io/gammasim"
IMAGE_REGISTRY = "ghcr.io/gammasim"
IMAGE_REGISTRY_PATTERN = re.compile(r"^[a-z0-9.-]+(/[a-z0-9._-]+)*$")
IMAGE_TAG_SUFFIX_PATTERN = re.compile(r"^(-\w[A-Za-z0-9_.]*)?$", re.ASCII)
SIM_TELARRAY_KEY_SCHEMA_VERSION = "0.10.0"


@cache
def _dependency_schemas():
    """Load supported catalog formats and their features from the schema documents."""
    path = Path(__file__).parents[1] / "schemas" / "dependency_versions.schema.yml"
    documents = yaml.safe_load_all(path.read_text(encoding="utf-8"))
    return {document["schema_version"]: document for document in documents}


def _readable_ref_schemas():
    """Return formats whose model declaration uses source refs."""
    return {
        version
        for version, schema in _dependency_schemas().items()
        if "default-ref" in schema["properties"][MODEL_KEY]["properties"]
    }


def _requires_model_range(schema_version):
    """Check whether the version's schema requires a model compatibility range."""
    model_schema = _dependency_schemas()[schema_version]["properties"][MODEL_KEY]
    return RANGE_KEY in model_schema.get("required", [])


def _uses_image_catalog(schema_version):
    """Return whether the schema of this version names images through an ``images`` section."""
    schema = _dependency_schemas().get(schema_version)
    return schema is not None and "images" in schema["required"]


def _source_snapshot_image(package, digest, revisions):
    """Return an immutable private source-snapshot image reference."""
    if not digest or not all(revisions):
        return ""
    return f"{SOURCE_SNAPSHOT_REGISTRY}/{package}@{digest}"


def _image_settings(catalog):
    """Return the image registry and tag suffix used for component images."""
    if _uses_image_catalog(catalog["schema_version"]):
        return catalog["images"]["registry"], catalog["images"].get("tag-suffix", "")
    return IMAGE_REGISTRY, ""


def _corsika_tag(component):
    """Return the external CORSIKA source tag from either catalog schema."""
    return component.get("tag", component.get("source-ref"))


def _corsika_build_id(component):
    """Return the CORSIKA build identifier from either catalog schema."""
    if "build-id" in component:
        build_id = component["build-id"]
        if not isinstance(build_id, str) or re.fullmatch(r"\d+", build_id, re.ASCII) is None:
            raise ValueError(f"CORSIKA build ID must contain only digits: {build_id!r}")
        return build_id
    if "version" in component:
        return component["version"]
    tag = _corsika_tag(component)
    if CORSIKA_TAG_PATTERN.fullmatch(tag or "") is None:
        raise ValueError(
            "CORSIKA tag must have the form v<major>.<minor> to derive its legacy build ID."
        )
    return tag.removeprefix("v").replace(".", "")


def _corsika_reference(component):
    """Return the production-combination reference for a CORSIKA record."""
    return _corsika_build_id(component) if "version" in component else _corsika_tag(component)


[docs] def corsika_source_tag_for_build_id(build_id, catalog=None): """Return the CORSIKA source tag matching a legacy build identifier.""" if catalog is None: catalog = load_dependency_catalog() matches = [ _corsika_tag(component) for component in catalog.get("corsika", []) if str(_corsika_build_id(component)) == str(build_id) ] if len(matches) > 1: raise ValueError(f"Multiple CORSIKA source tags match build ID {build_id!r}: {matches}") return matches[0] if matches else None
def _simtel_tag(component): """Return a sim_telarray tag from either catalog schema.""" return component.get("tag", component.get("source-ref", component.get("version"))) def _safe_build_id(source_ref, build_id=None): """Return an OCI-safe identifier while preserving readable ref fragments.""" if build_id is None: build_id = re.sub(r"[^\w.-]+", "-", source_ref or "", flags=re.ASCII).strip("-") if not isinstance(build_id, str) or IMAGE_TAG_PATTERN.fullmatch(build_id) is None: raise ValueError( "sim_telarray build-id must be a valid OCI image tag when its source ref " f"is not safe for image names: {build_id!r}" ) return build_id def _simtel_build_id(component): """Return the safe image and artifact identifier for a sim_telarray record.""" source_ref = _simtel_tag(component) build_id = component.get("build-id") if build_id is None and IMAGE_TAG_PATTERN.fullmatch(source_ref or ""): build_id = source_ref if build_id is None: raise ValueError( "sim_telarray source refs that are not valid OCI image tags require build-id: " f"{source_ref!r}" ) return _safe_build_id(source_ref, build_id)
[docs] def model_repository_revision(catalog): """Return the configured simulation-model repository ref or revision.""" model = catalog.get("model-repository") or catalog.get("model-database", {}) return model.get("git-revision") or model.get( "default-ref", model.get("default-tag", model.get("default-version")) )
def _tests_ref(test_resources): """Return the human-readable simtools-tests Git ref.""" return test_resources.get("ref") def _tests_resource_version(test_resources): """Return the versioned simtools-tests resource directory name.""" return test_resources.get("resource-version") def _tests_tag(test_resources): """Return the simtools-tests tag from pre-0.5 catalogs.""" return test_resources.get("tag", test_resources.get("version", "")) def _dependency_tag(component, new_key, *old_keys): """Read a renamed dependency field while retaining old catalogs.""" for key in (new_key, *old_keys): if key in component: return component[key] return None
[docs] def find_dependency_versions(start_path=None): """Find the dependency catalog matching the running simtools installation. Parameters ---------- start_path : str or Path, optional Explicit search directory, taking precedence over bundled catalogs. By default, bundled catalogs take precedence over the working directory. SIMTOOLS_DEPENDENCY_VERSIONS overrides either lookup order. Returns ------- pathlib.Path Path to the matching ``dependency_versions.yml`` file. Raises ------ FileNotFoundError If an explicit environment override is missing or no catalog can be found. """ configured_path = os.getenv("SIMTOOLS_DEPENDENCY_VERSIONS") if configured_path: catalog = Path(configured_path) if not catalog.is_file(): raise FileNotFoundError(f"Configured dependency catalog does not exist: {catalog}") return catalog bundled = [ Path(__file__).resolve().parents[3] / DEPENDENCY_VERSIONS_FILENAME, Path(sys.prefix) / "simtools" / DEPENDENCY_VERSIONS_FILENAME, ] start = Path(start_path or Path.cwd()).resolve() searched = [parent / DEPENDENCY_VERSIONS_FILENAME for parent in (start, *start.parents)] candidates = bundled + searched if start_path is None else searched + bundled for candidate in candidates: if candidate.is_file(): return candidate raise FileNotFoundError(f"Could not find {DEPENDENCY_VERSIONS_FILENAME}.")
[docs] def find_pyproject(start_path=None): """Find the nearest ``pyproject.toml`` file.""" configured_path = os.getenv("SIMTOOLS_PYPROJECT") candidates = [Path(configured_path)] if configured_path else [] start = Path(start_path or Path.cwd()).resolve() candidates.extend(parent / PYPROJECT_FILENAME for parent in (start, *start.parents)) candidates.append(Path(__file__).resolve().parents[3] / PYPROJECT_FILENAME) for candidate in candidates: if candidate.is_file(): return candidate raise FileNotFoundError(f"Could not find {PYPROJECT_FILENAME}.")
@cache def _load_dependency_catalog_from_file(catalog_file, validate): """Load and optionally validate one catalog file for the process lifetime.""" catalog = yaml.safe_load(Path(catalog_file).read_text(encoding="utf-8")) if not isinstance(catalog, dict): raise ValueError(f"Dependency catalog must contain a mapping: {catalog_file}") return validate_dependency_catalog(catalog) if validate else catalog
[docs] def load_dependency_catalog(catalog_path=None, validate=True): """Load the dependency version catalog from YAML. Parameters ---------- catalog_path : str or Path, optional Explicit catalog file. The repository is searched when omitted. validate : bool, optional Validate the catalog structure when True. Returns ------- dict Dependency version catalog. """ catalog_file = (Path(catalog_path) if catalog_path else find_dependency_versions()).resolve() return deepcopy(_load_dependency_catalog_from_file(str(catalog_file), validate))
[docs] def validate_dependency_catalog(catalog): """Validate required dependency catalog values. Parameters ---------- catalog : dict Dependency version catalog. Returns ------- dict The validated catalog. Raises ------ ValueError If a required value is missing or invalid. """ if LEGACY_MODEL_KEY in catalog: if MODEL_KEY in catalog: raise ValueError("Catalog declares both legacy and current model dependency keys.") catalog = dict(catalog) catalog[MODEL_KEY] = catalog.pop(LEGACY_MODEL_KEY) schema_version = catalog.get("schema_version") missing = sorted(_required_catalog_keys(schema_version) - catalog.keys()) if missing: raise ValueError(f"Missing dependency catalog keys: {', '.join(missing)}") if schema_version not in set(_dependency_schemas()): raise ValueError(f"Unsupported dependency catalog schema version: {schema_version}") catalog = _normalize_sim_telarray_key(catalog, schema_version) _validate_optional_digest(catalog["base-image"].get("runtime-digest"), "runtime base image") _validate_optional_digest(catalog["base-image"].get("build-digest"), "build base image") if _uses_image_catalog(schema_version): _validate_images(catalog["images"]) _validate_image_components(catalog) else: _validate_archive_checksums(catalog["archives"]) _validate_components(catalog, schema_version) _validate_production_combinations(catalog) return catalog
def _version_tuple(schema_version): """Return a schema version as a tuple of integers for numeric comparison.""" return tuple(map(int, schema_version.split("."))) def _with_sim_telarray_key(mapping): """Return a copy of a mapping with the sim-telarray key renamed to sim_telarray.""" return { ("sim_telarray" if key == "sim-telarray" else key): value for key, value in mapping.items() } def _normalize_sim_telarray_key(catalog, schema_version): """Rename the sim-telarray key of catalogs older than schema 0.10.0 to sim_telarray. Must run after the catalog passed the required-key check of its own schema version. Catalogs of schema 0.10.0 and later are returned unchanged. """ if _version_tuple(schema_version) >= _version_tuple(SIM_TELARRAY_KEY_SCHEMA_VERSION): return catalog catalog = _with_sim_telarray_key(catalog) if "production-combinations" in catalog: catalog["production-combinations"] = [ _with_sim_telarray_key(combination) for combination in catalog["production-combinations"] ] return catalog def _required_catalog_keys(schema_version): """Return the top-level keys required by the schema of a catalog version. An unknown version falls back to the newest schema, so that an incomplete catalog reports its missing keys before the unsupported version is reported. """ schemas = _dependency_schemas() if schema_version not in schemas: schema_version = max(schemas, key=_version_tuple) return set(schemas[schema_version]["required"]) def _validate_images(images): """Validate the registry and tag suffix that name catalog-managed images.""" if not isinstance(images, dict): raise ValueError("Image catalog section must be a mapping: images") unknown = sorted(set(images) - {"registry", "tag-suffix"}) if unknown: raise ValueError(f"Unknown image catalog keys: {', '.join(unknown)}") registry = images.get("registry") if not isinstance(registry, str) or IMAGE_REGISTRY_PATTERN.fullmatch(registry) is None: raise ValueError(f"Invalid image registry: {registry!r}") suffix = images.get("tag-suffix", "") if not isinstance(suffix, str) or IMAGE_TAG_SUFFIX_PATTERN.fullmatch(suffix) is None: raise ValueError(f"Invalid image tag suffix: {suffix!r}") def _validate_image_components(catalog): """Validate source refs, image identifiers, and digests of an image-based catalog.""" _validate_source_ref( catalog["corsika-interaction-tables"]["ref"], CORSIKA_INTERACTION_TABLES_LABEL ) for component in catalog["corsika"]: _validate_source_ref(component["source-ref"], "CORSIKA source") _corsika_build_id(component) for variant, digest in component.get("image-digests", {}).items(): _validate_digest(digest, f"CORSIKA {component['source-ref']} {variant}") for component in catalog["sim_telarray"]: _validate_source_ref(component["source-ref"], "sim_telarray") _simtel_build_id(component) _validate_optional_digest(component.get("image-digest"), "sim_telarray image") _validate_model_and_test_components(catalog, catalog["schema_version"]) def _validate_components(catalog, schema_version): """Validate CORSIKA and sim_telarray component records.""" readable_refs = schema_version in _readable_ref_schemas() ref_validator = _validate_source_ref if readable_refs else _validate_release_tag interaction_tables = catalog["corsika-interaction-tables"] interaction_ref = ( interaction_tables["ref"] if readable_refs else interaction_tables.get("tag", interaction_tables.get("version")) ) ref_validator(interaction_ref, CORSIKA_INTERACTION_TABLES_LABEL) require_revisions = schema_version in {"0.4.0", "0.5.0"} if readable_refs: _validate_source_url(interaction_tables.get("source-url"), CORSIKA_INTERACTION_TABLES_LABEL) revision_validator = ( _validate_revision if require_revisions else _validate_optional_revision ) revision_validator(interaction_tables.get("revision"), CORSIKA_INTERACTION_TABLES_LABEL) _validate_corsika_components( catalog["corsika"], require_revisions, ref_validator, readable_refs ) _validate_simtel_components( catalog["sim_telarray"], require_revisions, ref_validator, readable_refs ) _validate_model_and_test_components(catalog, schema_version) _validate_archive_versions(catalog["archives"]) def _validate_corsika_components( components, require_revisions=False, ref_validator=None, current_schema=False ): """Validate CORSIKA tags, legacy IDs, revisions, and image digests.""" ref_validator = ref_validator or _validate_release_tag for component in components: source_tag = _corsika_tag(component) ref_validator(source_tag, "CORSIKA source") config_ref = ( component["config-ref"] if current_schema else _dependency_tag(component, "config-tag", "config-version") ) opt_patch_ref = ( component["opt-patch-ref"] if current_schema else _dependency_tag(component, "opt-patch-tag", "opt-patch-version") ) ref_validator(config_ref, "CORSIKA configuration") ref_validator(opt_patch_ref, "CORSIKA optimization patch") try: _corsika_build_id(component) except ValueError as exc: raise ValueError(f"Invalid CORSIKA build ID mapping for {source_tag!r}: {exc}") from exc revision_validator = ( _validate_revision if require_revisions else _validate_optional_revision ) revision_validator(component.get("source-revision"), "CORSIKA source") revision_validator(component.get("config-revision"), "CORSIKA configuration") revision_validator(component.get("opt-patch-revision"), "CORSIKA optimization patch") _validate_optional_digest( component.get("source-snapshot-digest"), "CORSIKA source snapshot" ) if component.get("source-snapshot-digest") and not all( component.get(key) for key in ("source-revision", "config-revision", "opt-patch-revision") ): raise ValueError("CORSIKA source snapshot requires all source revisions") for variant, digest in component.get("image-digests", {}).items(): _validate_optional_digest(digest, f"CORSIKA {source_tag} {variant}") def _validate_simtel_components( components, require_revisions=False, ref_validator=None, current_schema=False ): """Validate sim_telarray component revisions and image digests.""" ref_validator = ref_validator or _validate_release_tag for component in components: ref_validator(_simtel_tag(component), "sim_telarray") _simtel_build_id(component) hessio_ref = ( component["hessio-ref"] if current_schema else _dependency_tag(component, "hessio-tag", "hessio-version") ) stdtools_ref = ( component["stdtools-ref"] if current_schema else _dependency_tag(component, "stdtools-tag", "stdtools-version") ) ref_validator(hessio_ref, "hessio") ref_validator(stdtools_ref, "stdtools") _safe_build_id(hessio_ref) _safe_build_id(stdtools_ref) revision_validator = ( _validate_revision if require_revisions else _validate_optional_revision ) for key in ("revision", "hessio-revision", "stdtools-revision"): revision_validator(component.get(key), key) _validate_optional_digest( component.get("source-snapshot-digest"), "sim_telarray source snapshot" ) if component.get("source-snapshot-digest") and not all( component.get(key) for key in ("revision", "hessio-revision", "stdtools-revision") ): raise ValueError("sim_telarray source snapshot requires all source revisions") _validate_optional_digest(component.get("image-digest"), "sim_telarray image") def _validate_model_and_test_components(catalog, schema_version): """Validate model-repository and simtools-tests catalog values.""" model = catalog["model-repository"] readable_refs = schema_version in _readable_ref_schemas() model_version = ( model["default-ref"] if readable_refs else model.get("default-tag", model.get("default-version")) ) if readable_refs: valid_model_version = _is_valid_source_ref(model_version) elif schema_version in {"0.2.0", "0.3.0", "0.4.0"}: valid_model_version = versioning.is_valid_release_tag(model_version) else: valid_model_version = isinstance(model_version, str) and versioning.is_valid_model_version( model_version ) if not valid_model_version: message = "Invalid simulation-model repository revision." if readable_refs: message = "Invalid simulation-model source ref." elif schema_version in {"0.2.0", "0.3.0", "0.4.0"}: message = "Invalid simulation-model release tags." raise ValueError(message) _validate_model_constraint(catalog, schema_version) if model.get("repository-url") is not None: _validate_source_url(model["repository-url"], "simulation-model repository") if model.get("git-revision") is not None: _validate_revision(model["git-revision"], "simulation-model repository") if schema_version != "0.1.0": _validate_simtools_tests( catalog["simtools-tests"], schema_version == "0.5.0", readable_refs ) def _validate_model_constraint(catalog, schema_version): """Validate the range declaration without changing historical catalog semantics.""" model = catalog[MODEL_KEY] if _requires_model_range(schema_version) and RANGE_KEY not in model: raise ValueError("Missing simulation-model compatible-versions.") if RANGE_KEY in model: get_contract(catalog) def _validate_simtools_tests(test_resources, require_revision=False, readable_refs=False): """Validate the simtools-tests repository configuration.""" repository = test_resources.get("repository") if not isinstance(repository, str) or not SIMTOOLS_TESTS_REPOSITORY_PATTERN.fullmatch( repository ): raise ValueError("simtools-tests repository must use the owner/name format.") _validate_source_url(test_resources.get("source-url"), "simtools-tests") source_ref = _tests_ref(test_resources) if readable_refs else _tests_tag(test_resources) if readable_refs: _validate_source_ref(source_ref, "simtools-tests") revision_validator = _validate_revision if require_revision else _validate_optional_revision revision_validator(test_resources.get("revision"), "simtools-tests") resource_version = _tests_resource_version(test_resources) if not versioning.is_valid_release_tag(resource_version): raise ValueError( "simtools-tests resource version must be a release tag starting with 'v'." ) elif not versioning.is_valid_release_tag(source_ref): raise ValueError("simtools-tests tag must be a release tag starting with 'v'.") def _validate_production_combinations(catalog): """Validate every production combination against catalogued components.""" corsika_versions = {_corsika_reference(component) for component in catalog["corsika"]} simtel_versions = {_simtel_tag(component) for component in catalog["sim_telarray"]} cpu_variants = set(catalog["cpu-variants"]) for combination in catalog["production-combinations"]: if combination["corsika"] not in corsika_versions: raise ValueError("Unknown CORSIKA production combination.") if combination["sim_telarray"] not in simtel_versions: raise ValueError("Unknown sim_telarray production combination.") invalid_variants = set(combination.get("cpu-variants", cpu_variants)) - cpu_variants if invalid_variants: raise ValueError("Unknown CPU variant in production combination.") def _validate_digest(value, label): """Validate an OCI SHA-256 digest.""" if not isinstance(value, str) or not SHA256_PATTERN.fullmatch(value): raise ValueError(f"Invalid SHA-256 digest for {label}: {value}") def _validate_revision(value, label): """Validate a Git commit revision.""" if not versioning.is_valid_revision(value): raise ValueError(f"Invalid Git revision for {label}: {value}") def _validate_optional_digest(value, label): """Validate an OCI SHA-256 digest when one is declared.""" if value is not None: _validate_digest(value, label) def _validate_archive_checksums(archives): """Validate every optional archive SHA-256 checksum.""" for archive_name, archive in archives.items(): value = archive.get("sha256") if value is not None and ( not isinstance(value, str) or not ARCHIVE_SHA256_PATTERN.fullmatch(value) ): raise ValueError(f"Invalid SHA-256 checksum for {archive_name}: {value}") def _validate_optional_revision(value, label): """Validate a Git commit revision when one is declared.""" if value is not None: _validate_revision(value, label) def _validate_release_tag(value, label): """Validate a catalog-managed release tag.""" if not versioning.is_valid_release_tag(value): raise ValueError(f"Invalid release tag for {label}: {value!r}") def _is_valid_source_ref(value): """Return whether a Git branch or tag name is safe to use as a source ref.""" return isinstance(value, str) and SOURCE_REF_PATTERN.fullmatch(value) is not None def _validate_source_ref(value, label): """Validate a human-readable Git tag or branch name.""" if not _is_valid_source_ref(value): raise ValueError(f"Invalid source ref for {label}: {value!r}") def _validate_source_url(value, label): """Validate a source URL that may be cloned by a build workflow.""" if not isinstance(value, str) or not value.startswith("https://"): raise ValueError(f"{label} source URL must use HTTPS.") def _validate_archive_versions(archives): """Validate package versions recorded for archived dependencies.""" for archive_name, archive in archives.items(): if not versioning.is_valid_package_version(archive.get("version")): raise ValueError( f"Invalid package version for {archive_name}: {archive.get('version')!r}" )
[docs] def validate_env_template(template_path): """Validate non-secret runtime defaults against the dependency catalog. Parameters ---------- template_path : str or Path Environment template to validate. Raises ------ ValueError If catalog-managed versions are duplicated in the template. """ values = {} for line in Path(template_path).read_text(encoding="utf-8").splitlines(): stripped = line.strip() if not stripped or stripped.startswith("#") or "=" not in stripped: continue key, value = stripped.split("=", maxsplit=1) values[key] = value version_keys = {"SIMTOOLS_TESTS_RESOURCE_VERSION"} configured_versions = sorted(version_keys & values.keys()) if configured_versions: raise ValueError( ".env_template must not define catalog-managed versions: " + ", ".join(configured_versions) )
[docs] def build_workflow_matrices(catalog): """Build GitHub Actions matrices from the dependency catalog. Image catalogs export only the production matrix. Catalogs that pin component sources also export the source-build matrices. """ production_matrix = _production_matrix(catalog) if _uses_image_catalog(catalog["schema_version"]): return {"production_matrix": production_matrix} return {**_source_build_matrices(catalog), "production_matrix": production_matrix}
def _production_matrix(catalog): """Build one production image matrix entry per combination and CPU variant.""" variants = catalog["cpu-variants"] corsika_components = {_corsika_reference(item): item for item in catalog["corsika"]} simtel_components = {_simtel_tag(item): item for item in catalog["sim_telarray"]} images = _image_settings(catalog) return [ _production_matrix_entry( corsika_components, simtel_components, combination, variant, images ) for combination in catalog["production-combinations"] for variant in combination.get("cpu-variants", variants) ] def _source_build_matrices(catalog): """Build the source-build matrices of catalogs that pin component sources.""" variants = catalog["cpu-variants"] platform_matrix = [ {"platform": "linux/amd64", "arch": "amd64", "runner": "ubuntu-24.04"}, {"platform": "linux/arm64/v8", "arch": "arm64", "runner": "ubuntu-24.04-arm"}, ] corsika_matrix = [ { "corsika_tag": _corsika_tag(corsika), "corsika_build_id": _corsika_build_id(corsika), "corsika_source_url": corsika["source-url"], "corsika_source_revision": corsika.get("source-revision", ""), "corsika_config_tag": _dependency_tag( corsika, "config-ref", "config-tag", "config-version" ), "corsika_config_source_url": corsika["config-source-url"], "corsika_config_revision": corsika.get("config-revision", ""), "corsika_opt_patch_tag": _dependency_tag( corsika, "opt-patch-ref", "opt-patch-tag", "opt-patch-version" ), "corsika_opt_patch_source_url": corsika["opt-patch-source-url"], "corsika_opt_patch_revision": corsika.get("opt-patch-revision", ""), "corsika_source_snapshot": _source_snapshot_image( "corsika7-build-inputs", corsika.get("source-snapshot-digest", ""), ( corsika.get("source-revision", ""), corsika.get("config-revision", ""), corsika.get("opt-patch-revision", ""), ), ), "avx_flag": variant, } for corsika in catalog["corsika"] for variant in variants ] simtel_matrix = [ { "simtel_tag": _simtel_tag(component), "simtel_build_id": _simtel_build_id(component), "simtel_source_url": component["source-url"], "simtel_revision": component.get("revision", ""), "hessio_tag": _dependency_tag(component, "hessio-ref", "hessio-tag", "hessio-version"), "hessio_build_id": _safe_build_id( _dependency_tag(component, "hessio-ref", "hessio-tag", "hessio-version") ), "hessio_source_url": component["hessio-source-url"], "hessio_revision": component.get("hessio-revision", ""), "stdtools_tag": _dependency_tag( component, "stdtools-ref", "stdtools-tag", "stdtools-version" ), "stdtools_build_id": _safe_build_id( _dependency_tag(component, "stdtools-ref", "stdtools-tag", "stdtools-version") ), "stdtools_source_url": component["stdtools-source-url"], "stdtools_revision": component.get("stdtools-revision", ""), "simtel_source_snapshot": _source_snapshot_image( "simtel-array-build-inputs", component.get("source-snapshot-digest", ""), ( component.get("revision", ""), component.get("hessio-revision", ""), component.get("stdtools-revision", ""), ), ), } for component in catalog["sim_telarray"] ] corsika_source_matrix = [ { "corsika_tag": _corsika_tag(component), "corsika_source_url": component["source-url"], "corsika_source_revision": component.get("source-revision", ""), "corsika_config_tag": _dependency_tag( component, "config-ref", "config-tag", "config-version" ), "corsika_config_source_url": component["config-source-url"], "corsika_config_revision": component.get("config-revision", ""), "corsika_opt_patch_tag": _dependency_tag( component, "opt-patch-ref", "opt-patch-tag", "opt-patch-version" ), "corsika_opt_patch_source_url": component["opt-patch-source-url"], "corsika_opt_patch_revision": component.get("opt-patch-revision", ""), "corsika_source_snapshot": _source_snapshot_image( "corsika7-build-inputs", component.get("source-snapshot-digest", ""), ( component.get("source-revision", ""), component.get("config-revision", ""), component.get("opt-patch-revision", ""), ), ), } for component in catalog["corsika"] ] return { "corsika_matrix": corsika_matrix, "corsika_build_matrix": [ {**item, **platform} for item in corsika_matrix for platform in platform_matrix if item["avx_flag"] == "generic" or platform["arch"] == "amd64" ], "corsika_source_matrix": [ {"corsika_build_id": _corsika_build_id(component), **source} for component, source in zip(catalog["corsika"], corsika_source_matrix) ], "simtel_matrix": simtel_matrix, "simtel_build_matrix": [ {**item, **platform} for item in simtel_matrix for platform in platform_matrix ], } def _production_matrix_entry(corsika_components, simtel_components, combination, variant, images): """Build one production image matrix entry. ``images`` is the (registry, tag suffix) pair. Build IDs and tags in the entry do not include the suffix; only image references do. """ registry, suffix = images corsika = corsika_components[combination["corsika"]] simtel = simtel_components[combination["sim_telarray"]] production_corsika = _corsika_tag(corsika) or f"v{_corsika_build_id(corsika)}" return { "corsika_tag": production_corsika, "corsika_build_id": _corsika_build_id(corsika), "corsika_image": _image_reference( f"{registry}/corsika7", f"v{_corsika_build_id(corsika)}-{variant}{suffix}", corsika.get("image-digests", {}).get(variant), ), "simtel_tag": _simtel_tag(simtel), "simtel_build_id": _simtel_build_id(simtel), "simtel_image": _image_reference( f"{registry}/sim_telarray", f"{_simtel_build_id(simtel)}{suffix}", simtel.get("image-digest"), ), "avx_flag": variant, } def _image_reference(name, tag, digest=None): """Return a digest reference when declared, otherwise a version tag.""" return f"{name}@{digest}" if digest else f"{name}:{tag}"
[docs] def dependency_catalog_summary(catalog): """Return stable scalar build values used by Docker workflows. Image catalogs omit the values that only source builds use. """ base = catalog["base-image"] default_corsika = catalog["corsika"][0] default_simtel = catalog["sim_telarray"][0] test_resources = catalog.get("simtools-tests", {}) registry, suffix = _image_settings(catalog) summary = { "python_version": catalog["python"], "apptainer_version": catalog["apptainer"], "base_image": _image_reference( base["name"], base["runtime-version"], base.get("runtime-digest") ), "almalinux_version": base["runtime-version"].removesuffix("-minimal"), "corsika_tables_ref": _dependency_tag( catalog["corsika-interaction-tables"], "ref", "tag", "version" ), "corsika_tables_revision": catalog["corsika-interaction-tables"].get("revision", ""), "model_repository": catalog["model-repository"].get("repository-url", ""), "model_repository_revision": model_repository_revision(catalog), "simtools_tests_repository": test_resources.get("repository", ""), "simtools_tests_url": test_resources.get("source-url", ""), "simtools_tests_ref": _tests_ref(test_resources) or _tests_tag(test_resources), "simtools_tests_resource_version": _tests_resource_version(test_resources), "simtools_tests_revision": test_resources.get("revision", ""), "dev_corsika_image": _image_reference( f"{registry}/corsika7", f"v{_corsika_build_id(default_corsika)}-generic{suffix}", default_corsika.get("image-digests", {}).get("generic"), ), "dev_simtel_image": _image_reference( f"{registry}/sim_telarray", f"{_simtel_build_id(default_simtel)}{suffix}", default_simtel.get("image-digest"), ), } if _uses_image_catalog(catalog["schema_version"]): return summary return {**summary, **_source_build_summary(catalog)}
def _source_build_summary(catalog): """Return summary values that only source builds use.""" base = catalog["base-image"] default_corsika = catalog["corsika"][0] default_simtel = catalog["sim_telarray"][0] return { "build_base_image": _image_reference( base["name"], base["build-version"], base.get("build-digest") ), "autoconf_version": catalog["archives"]["autoconf"]["version"], "autoconf_sha256": catalog["archives"]["autoconf"].get("sha256", ""), "default_corsika_source_snapshot": _source_snapshot_image( "corsika7-build-inputs", default_corsika.get("source-snapshot-digest", ""), ( default_corsika.get("source-revision", ""), default_corsika.get("config-revision", ""), default_corsika.get("opt-patch-revision", ""), ), ), "gsl_version": catalog["archives"]["gsl"]["version"], "gsl_sha256": catalog["archives"]["gsl"].get("sha256", ""), "default_simtel_source_snapshot": _source_snapshot_image( "simtel-array-build-inputs", default_simtel.get("source-snapshot-digest", ""), ( default_simtel.get("revision", ""), default_simtel.get("hessio-revision", ""), default_simtel.get("stdtools-revision", ""), ), ), }
[docs] def dependency_catalog_environment(catalog): """Return catalog-managed runtime values as environment assignments. Parameters ---------- catalog : dict Validated dependency version catalog. Returns ------- dict Environment variable names and values for model repository and test-resource configuration. Local paths and credentials are intentionally omitted. """ environment = { "SIMTOOLS_SIMULATION_MODELS_GIT_REVISION": model_repository_revision(catalog), } if "simtools-tests" in catalog: if catalog["schema_version"] in _readable_ref_schemas(): test_environment = { "SIMTOOLS_TESTS_REF": _tests_ref(catalog["simtools-tests"]), "SIMTOOLS_TESTS_RESOURCE_VERSION": _tests_resource_version( catalog["simtools-tests"] ), } else: if catalog["schema_version"] in {"0.3.0", "0.4.0"}: tag_key = "SIMTOOLS_TESTS_TAG" else: tag_key = "SIMTOOLS_TESTS_VERSION" test_environment = {tag_key: _tests_tag(catalog["simtools-tests"])} environment.update( { **test_environment, "SIMTOOLS_TESTS_REPOSITORY": catalog["simtools-tests"]["repository"], "SIMTOOLS_TESTS_URL": catalog["simtools-tests"]["source-url"], } ) revision = catalog["simtools-tests"].get("revision") if revision: environment["SIMTOOLS_TESTS_REVISION"] = revision return environment
[docs] def project_requirements(pyproject_path, extras): """Return project requirements, optionally including named extras.""" with Path(pyproject_path).open("rb") as file: project = tomllib.load(file)["project"] requirements = list(project["dependencies"]) optional = project.get("optional-dependencies", {}) for extra in extras: try: requirements.extend(optional[extra]) except KeyError as exc: available = ", ".join(sorted(optional)) or "none" raise ValueError( f"Unknown optional-dependency group: {extra}. Available groups: {available}" ) from exc return requirements
[docs] def export_dependency_configuration(pyproject_path=None, output_format="catalog", extras=None): """Return dependency configuration in a selected export format. Parameters ---------- pyproject_path : str or Path, optional Explicit project file, required for ``python-requirements`` output. The repository is searched when omitted for that output format. output_format : str, optional One of ``catalog``, ``env``, ``github-output``, ``python-requirements``, or ``summary``. extras : list of str, optional Optional dependency groups included in ``python-requirements`` output. Returns ------- str Serialized dependency configuration, including a trailing newline. """ project_file = Path(pyproject_path) if pyproject_path else None catalog_file = ( project_file.with_name(DEPENDENCY_VERSIONS_FILENAME) if project_file else find_dependency_versions() ) catalog = load_dependency_catalog(catalog_file) env_template = catalog_file.parent / ".env_template" if env_template.is_file(): validate_env_template(env_template) extras = extras or [] if output_format == "python-requirements": project_file = project_file or find_pyproject() return "\n".join(project_requirements(project_file, extras)) + "\n" if output_format == "catalog": return json.dumps(catalog, indent=2, sort_keys=True) + "\n" if output_format == "summary": return json.dumps(dependency_catalog_summary(catalog), sort_keys=True) + "\n" if output_format == "env": return "".join( f"{key}={value}\n" for key, value in dependency_catalog_environment(catalog).items() ) if output_format == "github-output": output = {**dependency_catalog_summary(catalog), **build_workflow_matrices(catalog)} return "".join(f"{key}={_github_output_value(value)}\n" for key, value in output.items()) raise ValueError(f"Unsupported dependency export format: {output_format}")
def _github_output_value(value): """Serialize a GitHub Actions output value.""" if isinstance(value, list): return json.dumps(value, separators=(",", ":")) return value