aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/python_version.py
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2023-03-15 04:49:06 +0400
committerGravatar Hassan Abouelela <[email protected]>2023-03-15 04:49:06 +0400
commit47a9e0d72d5225f9c503775530d4e5f0ff63fe6d (patch)
treedbbe21c54d5ae66215b576ba998fd5dbf2e6f679 /scripts/python_version.py
parentUpdate Sentry SDK to support Falcon 3 (diff)
Add Multi-version Capability
Adds support for having multiple evaluation python versions installed in the docker container. A utility to automatically generate correct dockerfile instructions and nsjail mounts based on the available versions is also included. Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'scripts/python_version.py')
-rw-r--r--scripts/python_version.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/scripts/python_version.py b/scripts/python_version.py
new file mode 100644
index 0000000..6c8f25c
--- /dev/null
+++ b/scripts/python_version.py
@@ -0,0 +1,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