|
2 | 2 | import json |
3 | 3 | import os |
4 | 4 | import zipfile |
| 5 | +from pathlib import Path |
5 | 6 |
|
6 | 7 | import httpx |
7 | 8 | import typer |
@@ -234,6 +235,159 @@ def command_task_list(): |
234 | 235 | list_tasks(output_format=cli_state.output_format, experiment_id=current_experiment) |
235 | 236 |
|
236 | 237 |
|
| 238 | +TASK_INIT_TEMPLATES_DIR = Path(__file__).resolve().parent.parent / "templates" / "task_init" |
| 239 | + |
| 240 | + |
| 241 | +def _render_task_yaml_template(task_name: str) -> str: |
| 242 | + template = (TASK_INIT_TEMPLATES_DIR / "task.yaml").read_text(encoding="utf-8") |
| 243 | + return template.replace("{{TASK_NAME}}", task_name) |
| 244 | + |
| 245 | + |
| 246 | +def _main_py_template() -> str: |
| 247 | + return (TASK_INIT_TEMPLATES_DIR / "main.py").read_text(encoding="utf-8") |
| 248 | + |
| 249 | + |
| 250 | +def _write_task_yaml(path: str, data: dict) -> None: |
| 251 | + yaml_text = yaml.safe_dump(data, sort_keys=False, default_flow_style=False) |
| 252 | + with open(path, "w", encoding="utf-8", newline="\n") as f: |
| 253 | + f.write(yaml_text) |
| 254 | + |
| 255 | + |
| 256 | +def _print_next_steps(include_main_py: bool) -> None: |
| 257 | + console.print("\nNext steps:") |
| 258 | + if include_main_py: |
| 259 | + console.print("- Edit [bold]main.py[/bold] with your task code") |
| 260 | + console.print("- Customize [bold]task.yaml[/bold] (resources, setup, parameters)") |
| 261 | + console.print("- Run: [bold]lab task add .[/bold]") |
| 262 | + console.print("- Docs: https://lab.cloud/for-teams/running-a-task/task-yaml-structure") |
| 263 | + |
| 264 | + |
| 265 | +def _task_init_default(task_yaml_path: str, main_py_path: str, folder_name: str) -> None: |
| 266 | + if os.path.exists(task_yaml_path): |
| 267 | + if cli_state.output_format == "json": |
| 268 | + print(json.dumps({"error": "task.yaml already exists"})) |
| 269 | + else: |
| 270 | + console.print( |
| 271 | + f"[error]Error:[/error] [bold]{task_yaml_path}[/bold] already exists. " |
| 272 | + "Refusing to overwrite. Remove it first or run `lab task init` in an empty directory." |
| 273 | + ) |
| 274 | + raise typer.Exit(1) |
| 275 | + |
| 276 | + with open(task_yaml_path, "w", encoding="utf-8", newline="\n") as f: |
| 277 | + f.write(_render_task_yaml_template(folder_name)) |
| 278 | + |
| 279 | + main_py_existed = os.path.exists(main_py_path) |
| 280 | + if not main_py_existed: |
| 281 | + with open(main_py_path, "w", encoding="utf-8", newline="\n") as f: |
| 282 | + f.write(_main_py_template()) |
| 283 | + |
| 284 | + if cli_state.output_format == "json": |
| 285 | + created = ["task.yaml"] if main_py_existed else ["task.yaml", "main.py"] |
| 286 | + skipped = ["main.py"] if main_py_existed else [] |
| 287 | + print(json.dumps({"created": created, "skipped": skipped, "path": os.path.dirname(task_yaml_path)})) |
| 288 | + return |
| 289 | + |
| 290 | + console.print("[success]✓[/success] Created [bold]task.yaml[/bold]") |
| 291 | + if main_py_existed: |
| 292 | + console.print("[warning]•[/warning] Skipped [bold]main.py[/bold] (already exists)") |
| 293 | + else: |
| 294 | + console.print("[success]✓[/success] Created [bold]main.py[/bold]") |
| 295 | + |
| 296 | + console.print(f"\nLocation: [bold]{os.path.dirname(task_yaml_path)}[/bold]") |
| 297 | + _print_next_steps(include_main_py=not main_py_existed) |
| 298 | + |
| 299 | + |
| 300 | +def _task_init_interactive(task_yaml_path: str, folder_name: str) -> None: |
| 301 | + if os.path.exists(task_yaml_path): |
| 302 | + if cli_state.output_format == "json": |
| 303 | + print(json.dumps({"error": "task.yaml already exists"})) |
| 304 | + raise typer.Exit(1) |
| 305 | + should_overwrite = typer.confirm("task.yaml already exists. Overwrite?", default=False) |
| 306 | + if not should_overwrite: |
| 307 | + console.print("[warning]Cancelled.[/warning]") |
| 308 | + raise typer.Exit(0) |
| 309 | + |
| 310 | + task_name = typer.prompt("Task name", default=folder_name).strip() or folder_name |
| 311 | + |
| 312 | + cpus = typer.prompt("CPUs", default="2").strip() |
| 313 | + memory = typer.prompt("Memory (GB)", default="4").strip() |
| 314 | + accelerators = typer.prompt("Accelerators (optional)", default="", show_default=False).strip() |
| 315 | + |
| 316 | + setup = "" |
| 317 | + run = "" |
| 318 | + |
| 319 | + if cli_state.output_format != "json" and os.isatty(0) and os.isatty(1): |
| 320 | + edited = typer.edit( |
| 321 | + "\n".join( |
| 322 | + [ |
| 323 | + "# Define the commands for your task below.", |
| 324 | + "# This YAML snippet will be parsed and merged into task.yaml.", |
| 325 | + "", |
| 326 | + "setup: |", |
| 327 | + " # Optional: install deps, download data, etc.", |
| 328 | + " ", |
| 329 | + "run: |", |
| 330 | + " # Required: the main command to execute", |
| 331 | + " ", |
| 332 | + "", |
| 333 | + ] |
| 334 | + ) |
| 335 | + ) |
| 336 | + if edited: |
| 337 | + try: |
| 338 | + edited_obj = yaml.safe_load(edited) |
| 339 | + if isinstance(edited_obj, dict): |
| 340 | + setup_val = edited_obj.get("setup") |
| 341 | + run_val = edited_obj.get("run") |
| 342 | + if isinstance(setup_val, str): |
| 343 | + setup = setup_val.rstrip() |
| 344 | + if isinstance(run_val, str): |
| 345 | + run = run_val.rstrip() |
| 346 | + except yaml.YAMLError: |
| 347 | + pass |
| 348 | + |
| 349 | + if not setup.strip(): |
| 350 | + setup = typer.prompt("Setup command (optional)", default="", show_default=False).rstrip() |
| 351 | + |
| 352 | + while not run.strip(): |
| 353 | + run = typer.prompt("Run command", default="", show_default=False).rstrip() |
| 354 | + |
| 355 | + task_yaml: dict = { |
| 356 | + "name": task_name, |
| 357 | + "resources": {"cpus": cpus, "memory": memory}, |
| 358 | + "run": run, |
| 359 | + } |
| 360 | + if accelerators: |
| 361 | + task_yaml["resources"]["accelerators"] = accelerators |
| 362 | + if setup.strip(): |
| 363 | + task_yaml["setup"] = setup |
| 364 | + |
| 365 | + _write_task_yaml(task_yaml_path, task_yaml) |
| 366 | + |
| 367 | + if cli_state.output_format == "json": |
| 368 | + print(json.dumps({"path": task_yaml_path})) |
| 369 | + return |
| 370 | + |
| 371 | + console.print(f"[success]✓[/success] Wrote [bold]{task_yaml_path}[/bold]") |
| 372 | + _print_next_steps(include_main_py=False) |
| 373 | + |
| 374 | + |
| 375 | +@app.command("init") |
| 376 | +def command_task_init( |
| 377 | + interactive: bool = typer.Option(False, "--interactive", help="Prompt for task settings instead of using defaults"), |
| 378 | +): |
| 379 | + """Initialize a task.yaml and main.py in the current directory.""" |
| 380 | + cwd = os.getcwd() |
| 381 | + task_yaml_path = os.path.join(cwd, "task.yaml") |
| 382 | + main_py_path = os.path.join(cwd, "main.py") |
| 383 | + folder_name = os.path.basename(cwd).strip() or "my-task" |
| 384 | + |
| 385 | + if interactive: |
| 386 | + _task_init_interactive(task_yaml_path, folder_name) |
| 387 | + else: |
| 388 | + _task_init_default(task_yaml_path, main_py_path, folder_name) |
| 389 | + |
| 390 | + |
237 | 391 | @app.command("add") |
238 | 392 | def command_task_add( |
239 | 393 | task_directory: str = typer.Argument(None, help="Path to the task directory containing task.yaml"), |
|
0 commit comments