|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Generate a cluster_annotate.yml from a config_annotate.yml. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python bin/generate_cluster_from_config.py \ |
| 7 | + --config config/config_annotate.yml \ |
| 8 | + --out config/cluster_annotate.yml \ |
| 9 | + --account cpu-s1-pgl-0 --partition cpu-s1-pgl-0 |
| 10 | +
|
| 11 | +Only top-level sections that contain ncpus/threads/memory/time are copied. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +from pathlib import Path |
| 18 | +from typing import Any, Dict |
| 19 | + |
| 20 | +import yaml |
| 21 | + |
| 22 | + |
| 23 | +def build_arg_parser() -> argparse.ArgumentParser: |
| 24 | + p = argparse.ArgumentParser(description="Generate cluster_annotate.yml from config_annotate.yml") |
| 25 | + p.add_argument("--config", required=True, help="Path to config_annotate.yml") |
| 26 | + p.add_argument("--out", required=True, help="Output path for cluster_annotate.yml") |
| 27 | + p.add_argument("--account", default="placeholder", help="SLURM account (default: placeholder)") |
| 28 | + p.add_argument("--partition", default="placeholder", help="SLURM partition (default: placeholder)") |
| 29 | + p.add_argument("--time", default="14-00:00:00", help="Walltime (default: 14-00:00:00)") |
| 30 | + p.add_argument("--default-memory", default="4g", help="Default memory for __default__ (default: 4g)") |
| 31 | + p.add_argument("--default-ncpus", type=int, default=1, help="Default CPUs for __default__ (default: 1)") |
| 32 | + p.add_argument( |
| 33 | + "--output-pattern", |
| 34 | + default="results/logs/{rule}_{wildcards}.out", |
| 35 | + help="Output log pattern for __default__", |
| 36 | + ) |
| 37 | + p.add_argument( |
| 38 | + "--error-pattern", |
| 39 | + default="results/logs/{rule}_{wildcards}.err", |
| 40 | + help="Error log pattern for __default__", |
| 41 | + ) |
| 42 | + return p |
| 43 | + |
| 44 | + |
| 45 | +def load_config(path: Path) -> Dict[str, Any]: |
| 46 | + with path.open() as fh: |
| 47 | + return yaml.safe_load(fh) or {} |
| 48 | + |
| 49 | + |
| 50 | +def should_copy_section(section: Dict[str, Any]) -> bool: |
| 51 | + return any(key in section for key in ("ncpus", "threads", "memory", "time")) |
| 52 | + |
| 53 | + |
| 54 | +def main() -> None: |
| 55 | + args = build_arg_parser().parse_args() |
| 56 | + cfg_path = Path(args.config) |
| 57 | + out_path = Path(args.out) |
| 58 | + |
| 59 | + cfg = load_config(cfg_path) |
| 60 | + |
| 61 | + cluster: Dict[str, Any] = { |
| 62 | + "__default__": { |
| 63 | + "account": args.account, |
| 64 | + "partition": args.partition, |
| 65 | + "memory": args.default_memory, |
| 66 | + "ncpus": args.default_ncpus, |
| 67 | + "nodes": 1, |
| 68 | + "time": args.time, |
| 69 | + "name": "{rule}.{wildcards}", |
| 70 | + "output": args.output_pattern, |
| 71 | + "error": args.error_pattern, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + for name, section in cfg.items(): |
| 76 | + if isinstance(section, dict) and should_copy_section(section): |
| 77 | + cluster[name] = {} |
| 78 | + for key in ("ncpus", "threads", "memory", "time"): |
| 79 | + if key in section: |
| 80 | + cluster[name][key] = section[key] |
| 81 | + |
| 82 | + out_path.parent.mkdir(parents=True, exist_ok=True) |
| 83 | + with out_path.open("w") as fh: |
| 84 | + yaml.safe_dump(cluster, fh, sort_keys=False, default_flow_style=False) |
| 85 | + |
| 86 | + print(f"Wrote cluster config to {out_path}") |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments