|
| 1 | +import platform |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | + |
| 5 | +def select_pip_deps(raw_deps: list[dict], accelerator: str) -> list[dict]: |
| 6 | + sys_os = platform.system().lower() # 'linux', 'darwin', 'windows' |
| 7 | + machine = platform.machine().lower() # 'x86_64', 'arm64', ... |
| 8 | + accel = (accelerator or "cpu").lower() |
| 9 | + |
| 10 | + out: list[dict] = [] |
| 11 | + for d in raw_deps: |
| 12 | + cond = d.get("condition") or {} |
| 13 | + ok = True |
| 14 | + if cond.get("os"): |
| 15 | + ok = ok and sys_os in [o.lower() for o in cond["os"]] |
| 16 | + if cond.get("platform"): |
| 17 | + ok = ok and any(p.lower() in machine for p in cond["platform"]) |
| 18 | + if cond.get("accelerator"): |
| 19 | + ok = ok and accel in [a.lower() for a in cond["accelerator"]] |
| 20 | + if ok: |
| 21 | + out.append( |
| 22 | + {"package": d["package"], "extra_pip_args": d.get("extra_pip_args")} |
| 23 | + ) |
| 24 | + return out |
| 25 | + |
| 26 | + |
| 27 | +def run_cmd( |
| 28 | + cmd: list[str], *, env: dict[str, str] | None = None, capture: bool = True |
| 29 | +) -> str: |
| 30 | + if capture: |
| 31 | + proc = subprocess.run(cmd, env=env, text=True, capture_output=True) |
| 32 | + if proc.returncode != 0: |
| 33 | + raise RuntimeError( |
| 34 | + f"Command failed: {' '.join(cmd)}\n" |
| 35 | + f"STDOUT:\n{proc.stdout}\n\nSTDERR:\n{proc.stderr}" |
| 36 | + ) |
| 37 | + return proc.stdout |
| 38 | + else: |
| 39 | + proc = subprocess.run(cmd, env=env, text=True) |
| 40 | + if proc.returncode != 0: |
| 41 | + raise RuntimeError(f"Command failed: {' '.join(cmd)}") |
| 42 | + return "" |
| 43 | + |
| 44 | + |
| 45 | +def which(exe: str): |
| 46 | + for path in os.environ.get("PATH", "").split(os.pathsep): |
| 47 | + cand = os.path.join(path, exe) |
| 48 | + if os.path.isfile(cand) and os.access(cand, os.X_OK): |
| 49 | + return cand |
| 50 | + return None |
0 commit comments