aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2023-03-17 19:29:50 +0400
committerGravatar Hassan Abouelela <[email protected]>2023-03-17 19:29:50 +0400
commit59dce27d31567046d2205743ede41ab664a10011 (patch)
tree5d8ee81cebf4cce5cb19b2571b6c312aeddcc814
parentMake Python Binary Mount Dynamic (diff)
Refactor Python Version Parsing
Remove the pointless function and allow the file-level constants to be imported by external callers. Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r--scripts/python_version.py37
-rw-r--r--scripts/set_versions.py10
-rw-r--r--snekbox/api/resources/eval.py14
3 files changed, 24 insertions, 37 deletions
diff --git a/scripts/python_version.py b/scripts/python_version.py
index 6c8f25c..0a6b518 100644
--- a/scripts/python_version.py
+++ b/scripts/python_version.py
@@ -23,32 +23,21 @@ class Version:
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
-
+ALL_VERSIONS: list[Version] = []
+"""A list of all versions available for eval."""
+MAIN_VERSION: Version = None
+"""The default eval version, and the version used by the server."""
+VERSION_DISPLAY_NAMES: list[str] = []
+"""The display names for all available eval versions."""
+
+if MAIN_VERSION is None:
+ # Set the constants' values the first time the file is imported
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)
+ MAIN_VERSION = version
+ ALL_VERSIONS.append(version)
+ VERSION_DISPLAY_NAMES.append(version.display_name)
- if main_version is None:
+ 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
diff --git a/scripts/set_versions.py b/scripts/set_versions.py
index fb33dc1..3bd2ab4 100644
--- a/scripts/set_versions.py
+++ b/scripts/set_versions.py
@@ -3,18 +3,16 @@
from pathlib import Path
from textwrap import dedent
-from scripts import python_version
+from scripts.python_version import ALL_VERSIONS, MAIN_VERSION
DOCKERFILE_TEMPLATE = Path("scripts/in.Dockerfile").read_text("utf-8")
DOCKERFILE = Path("Dockerfile")
-versions, main_version = python_version.get_all_versions()
-
# Download and copy multiple python images into one layer
python_build = ""
previous_layer = "first"
-for version in versions:
+for version in ALL_VERSIONS:
if version.is_main:
# Main is handled separately later
continue
@@ -31,14 +29,14 @@ for version in versions:
# Main version is installed twice, once at the very beginning to make sure
# its files aren't overwritten, and once at the end which actually makes use of the version
-python_build = f"FROM python:{main_version.image_tag} as base-first\n" + python_build
+python_build = f"FROM python:{MAIN_VERSION.image_tag} as base-first\n" + python_build
# Write new dockerfile
DOCKERFILE.write_text(
"# THIS FILE IS AUTOGENERATED, DO NOT MODIFY! #\n"
+ DOCKERFILE_TEMPLATE.replace("{python_install_commands}", python_build)
.replace("{final_base}", previous_layer)
- .replace("{main_version_tag}", main_version.image_tag),
+ .replace("{main_version_tag}", MAIN_VERSION.image_tag),
"utf-8",
)
diff --git a/snekbox/api/resources/eval.py b/snekbox/api/resources/eval.py
index 30f95ab..a4a9d5e 100644
--- a/snekbox/api/resources/eval.py
+++ b/snekbox/api/resources/eval.py
@@ -9,13 +9,11 @@ from snekbox.nsjail import NsJail
__all__ = ("EvalResource",)
-from scripts.python_version import get_all_versions
+from scripts.python_version import ALL_VERSIONS, MAIN_VERSION, VERSION_DISPLAY_NAMES
from snekbox.snekio import FileAttachment, ParsingError
log = logging.getLogger(__name__)
-_VERSION_DISPLAY_NAMES = [version.display_name for version in get_all_versions()[0]]
-
class EvalResource:
"""
@@ -34,7 +32,7 @@ class EvalResource:
"args": {"type": "array", "items": {"type": "string"}},
"version": {
"type": "string",
- "oneOf": [{"const": name} for name in _VERSION_DISPLAY_NAMES],
+ "oneOf": [{"const": name} for name in VERSION_DISPLAY_NAMES],
},
"files": {
"type": "array",
@@ -81,7 +79,7 @@ class EvalResource:
Success.
"""
resp.media = {
- "versions": _VERSION_DISPLAY_NAMES,
+ "versions": VERSION_DISPLAY_NAMES,
}
@validate(REQ_SCHEMA)
@@ -130,6 +128,7 @@ class EvalResource:
>>> {
... "stdout": "10000 loops, best of 5: 23.8 usec per loop",
... "returncode": 0,
+ ... "version": "Python 3.11",
... "files": [
... {
... "path": "output.png",
@@ -155,9 +154,9 @@ class EvalResource:
body["args"].append(body["input"])
# Parse a version from the request body, or use the default version
- all_versions, selected_version = get_all_versions()
+ selected_version = MAIN_VERSION
if "version" in body:
- for version in all_versions:
+ for version in ALL_VERSIONS:
if version.display_name == body["version"]:
selected_version = version
break
@@ -177,5 +176,6 @@ class EvalResource:
resp.media = {
"stdout": result.stdout,
"returncode": result.returncode,
+ "version": selected_version.display_name,
"files": [f.as_dict for f in result.files],
}