|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +__all__: list[str] = [] |
| 4 | + |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from typing import Annotated |
| 8 | + |
| 9 | +import typer |
| 10 | +import yaml |
| 11 | + |
| 12 | +from ..._submission.pipeline import submit_cwl |
| 13 | +from ..._submission.simple import detect_sandbox_files, generate_cwl |
| 14 | +from . import app |
| 15 | + |
| 16 | + |
| 17 | +@app.async_command( |
| 18 | + help="""Submit a simple command to the grid. |
| 19 | +
|
| 20 | +Runs COMMAND on a worker node. Local files referenced in the command |
| 21 | +are automatically detected and shipped as input sandboxes. |
| 22 | +
|
| 23 | +Use --sandbox for additional files not mentioned in the command. |
| 24 | +
|
| 25 | +Examples: |
| 26 | + dirac job submit cmd "python my_script.py" |
| 27 | + dirac job submit cmd "python my_script.py" --sandbox config.json |
| 28 | +""", |
| 29 | +) |
| 30 | +async def cmd( |
| 31 | + command: Annotated[str, typer.Argument(help="Shell command to run on the grid")], |
| 32 | + sandbox: Annotated[ |
| 33 | + list[Path], |
| 34 | + typer.Option("--sandbox", help="Additional local files to ship"), |
| 35 | + ] = [], |
| 36 | + yes: Annotated[ |
| 37 | + bool, typer.Option("-y", "--yes", help="Skip confirmation prompt") |
| 38 | + ] = False, |
| 39 | +): |
| 40 | + """Submit a simple command to the grid.""" |
| 41 | + # Auto-detect files from command |
| 42 | + auto_files = detect_sandbox_files(command) |
| 43 | + all_sandbox = list(set(auto_files + sandbox)) |
| 44 | + |
| 45 | + # Generate CWL |
| 46 | + cwl = generate_cwl(command=command, sandbox_files=all_sandbox) |
| 47 | + |
| 48 | + # Write CWL to temp file (pipeline expects a Path) |
| 49 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".cwl", delete=False) as f: |
| 50 | + yaml.dump(cwl, f) |
| 51 | + cwl_path = Path(f.name) |
| 52 | + |
| 53 | + try: |
| 54 | + # Build sandbox inputs if files exist |
| 55 | + input_files: list[Path] = [] |
| 56 | + if all_sandbox: |
| 57 | + sandbox_input = { |
| 58 | + "sandbox_files": [ |
| 59 | + {"class": "File", "path": str(p)} for p in all_sandbox |
| 60 | + ] |
| 61 | + } |
| 62 | + with tempfile.NamedTemporaryFile( |
| 63 | + mode="w", suffix=".yaml", delete=False |
| 64 | + ) as inp_f: |
| 65 | + yaml.dump(sandbox_input, inp_f) |
| 66 | + input_files = [Path(inp_f.name)] |
| 67 | + |
| 68 | + results = await submit_cwl( |
| 69 | + workflow=cwl_path, |
| 70 | + input_files=input_files, |
| 71 | + cli_args=[], |
| 72 | + range_spec=None, |
| 73 | + yes=yes, |
| 74 | + ) |
| 75 | + job_ids = [str(r.job_id) for r in results] |
| 76 | + print(f"Submitted {len(results)} job(s): {', '.join(job_ids)}") |
| 77 | + finally: |
| 78 | + cwl_path.unlink(missing_ok=True) |
0 commit comments