|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Dict, List, NamedTuple |
| 4 | + |
| 5 | + |
| 6 | +class RuntimeConfig(NamedTuple): |
| 7 | + image: str # Full Docker image reference |
| 8 | + file_name: str # Name that will be mounted under /scripts/ |
| 9 | + command: List[str] # Entrypoint executed inside the container |
| 10 | + |
| 11 | + |
| 12 | +LANGUAGE_SPECS: Dict[str, dict] = { |
| 13 | + "python": { |
| 14 | + "versions": ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"], |
| 15 | + "image_tpl": "python:{version}-slim", |
| 16 | + "file_ext": "py", |
| 17 | + "interpreter": ["python"], |
| 18 | + }, |
| 19 | + "node": { |
| 20 | + "versions": ["18", "20", "22"], |
| 21 | + "image_tpl": "node:{version}-alpine", |
| 22 | + "file_ext": "js", |
| 23 | + "interpreter": ["node"], |
| 24 | + }, |
| 25 | + "ruby": { |
| 26 | + "versions": ["3.1", "3.2", "3.3"], |
| 27 | + "image_tpl": "ruby:{version}-alpine", |
| 28 | + "file_ext": "rb", |
| 29 | + "interpreter": ["ruby"], |
| 30 | + }, |
| 31 | + "bash": { |
| 32 | + "versions": ["5.1", "5.2", "5.3"], |
| 33 | + "image_tpl": "bash:{version}", |
| 34 | + "file_ext": "sh", |
| 35 | + "interpreter": ["bash"], |
| 36 | + }, |
| 37 | + "go": { |
| 38 | + "versions": ["1.20", "1.21", "1.22"], |
| 39 | + "image_tpl": "golang:{version}-alpine", |
| 40 | + "file_ext": "go", |
| 41 | + "interpreter": ["bash", "-c", "go run /scripts/main.go"], |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +def _make_runtime_configs() -> Dict[str, Dict[str, RuntimeConfig]]: |
| 46 | + registry: Dict[str, Dict[str, RuntimeConfig]] = {} |
| 47 | + |
| 48 | + for lang, spec in LANGUAGE_SPECS.items(): |
| 49 | + versions = spec["versions"] |
| 50 | + image_tpl: str = spec["image_tpl"] |
| 51 | + file_ext: str = spec["file_ext"] |
| 52 | + interpreter_cmd: List[str] = spec["interpreter"] |
| 53 | + |
| 54 | + file_name = f"main.{file_ext}" |
| 55 | + full_path = f"/scripts/{file_name}" |
| 56 | + |
| 57 | + registry[lang] = { |
| 58 | + v: RuntimeConfig( |
| 59 | + image=image_tpl.format(version=v), |
| 60 | + file_name=file_name, |
| 61 | + command=( |
| 62 | + interpreter_cmd |
| 63 | + if "{file}" in " ".join(interpreter_cmd) |
| 64 | + else interpreter_cmd + [full_path] |
| 65 | + ), |
| 66 | + ) |
| 67 | + for v in versions |
| 68 | + } |
| 69 | + |
| 70 | + return registry |
| 71 | + |
| 72 | + |
| 73 | +RUNTIME_REGISTRY: Dict[str, Dict[str, RuntimeConfig]] = _make_runtime_configs() |
| 74 | + |
| 75 | +SUPPORTED_RUNTIMES: Dict[str, List[str]] = { |
| 76 | + lang: list(versions.keys()) for lang, versions in RUNTIME_REGISTRY.items() |
| 77 | +} |
0 commit comments