aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/python_version.py
blob: 6c8f25c69b96d44b4780c4ab8fd6f2c94773c5e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Parse and return python version information from the versions file.

The version file is read from the environment variable VERSIONS_CONFIG,
and defaults to config/versions.json otherwise.
"""

import json
import os
from dataclasses import dataclass
from pathlib import Path

VERSIONS_FILE = Path(os.getenv("VERSIONS_CONFIG", "config/versions.json"))


@dataclass(frozen=True)
class Version:
    """A python image available for eval."""

    image_tag: str
    version_name: str
    display_name: str
    is_main: bool


_ALL_VERSIONS = None
_MAIN_VERSION = None


def get_all_versions() -> tuple[list[Version], Version]:
    """
    Get a list of all available versions for this evaluation.

    Returns a tuple of all versions, and the main version.
    """
    # Return a cached result
    global _ALL_VERSIONS, _MAIN_VERSION
    if _ALL_VERSIONS is not None:
        return _ALL_VERSIONS, _MAIN_VERSION

    versions = []
    main_version: Version | None = None

    for version_json in json.loads(VERSIONS_FILE.read_text("utf-8")):
        version = Version(**version_json)
        if version.is_main:
            main_version = version
        versions.append(version)

    if main_version is None:
        raise Exception("Exactly one version must be configured as the main version.")

    _ALL_VERSIONS, _MAIN_VERSION = versions, main_version
    return versions, main_version