-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
112 lines (83 loc) · 3.02 KB
/
utils.py
File metadata and controls
112 lines (83 loc) · 3.02 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
"""Shared utilities for the CI toolchain."""
import subprocess
import sys
try:
import yaml
except ImportError:
print(
"error: pyyaml is required. Install with: pip install pyyaml", file=sys.stderr
)
sys.exit(1)
def normalize_config(raw):
"""Convert platform-centric config to flat images/jobs format.
Input (new format):
platforms:
nvidia:
image: {dockerfile: ..., build_args: ...}
setup: pip install .[dev]
jobs:
gpu: {resources: ..., stages: ...}
Output (flat format consumed by run.py / build.py / agent.py):
images:
nvidia: {dockerfile: ..., build_args: ...}
jobs:
nvidia_gpu: {platform: nvidia, setup: ..., resources: ..., stages: ...}
If the config already uses the flat format (no 'platforms' key), returns as-is.
"""
if "platforms" not in raw:
return raw
config = {}
for key in ("repo", "github", "agents"):
if key in raw:
config[key] = raw[key]
config["images"] = {}
config["jobs"] = {}
for platform, pcfg in raw.get("platforms", {}).items():
# Image config
if "image" in pcfg:
config["images"][platform] = pcfg["image"]
# Platform-level defaults inherited by jobs
defaults = {}
for key in ("image_tag", "docker_args", "volumes", "setup", "env"):
if key in pcfg:
defaults[key] = pcfg[key]
# Flatten jobs: {platform}_{job_name}
for job_name, job_cfg in pcfg.get("jobs", {}).items():
full_name = f"{platform}_{job_name}"
flat = {
"platform": platform,
"short_name": job_name,
"image": defaults.get("image_tag", "latest"),
}
# Apply platform defaults
for key in ("docker_args", "volumes", "setup", "env"):
if key in defaults:
flat[key] = defaults[key]
# Job-level overrides
flat.update(job_cfg)
config["jobs"][full_name] = flat
# Warn on mismatched agent/platform keys (catches typos like 'nvdia').
agent_keys = set(config.get("agents", {}).keys())
platform_keys = set(raw.get("platforms", {}).keys())
for key in agent_keys - platform_keys:
print(
f"warning: agents.{key} has no matching platform in platforms.*",
file=sys.stderr,
)
return config
def load_config(path):
"""Load a YAML config file and normalize to flat format."""
with open(path, encoding="utf-8") as f:
raw = yaml.safe_load(f)
return normalize_config(raw)
def get_git_commit(ref="HEAD", short=True):
"""Get git commit SHA. Returns 'unknown' on failure."""
cmd = ["git", "rev-parse"]
if short:
cmd.append("--short")
cmd.append(ref)
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
return "unknown"
return result.stdout.strip()