beta_leet.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import annotations
  2. import os
  3. import pathlib
  4. import subprocess
  5. import sys
  6. import click
  7. from typing_extensions import Never
  8. from wandb.analytics import get_sentry
  9. from wandb.env import error_reporting_enabled, is_debug
  10. from wandb.sdk import wandb_setup
  11. from wandb.util import get_core_path
  12. from .beta_sync import _find_wandb_files
  13. def _fatal(message: str) -> Never:
  14. """Print an error message and exit with code 1."""
  15. click.echo(f"Error: {message}", err=True)
  16. sys.exit(1)
  17. def _wandb_file_path(path: str | None) -> str:
  18. """Returns absolute path to the .wandb file to display with LEET.
  19. If `path` is not provided, looks for the latest W&B run.
  20. Prints an error and exits if a valid path is not found.
  21. """
  22. if not path:
  23. wandb_dir = wandb_setup.singleton().settings.wandb_dir
  24. wandb_run_path = (pathlib.Path(wandb_dir) / "latest-run").resolve()
  25. else:
  26. wandb_run_path = pathlib.Path(path).resolve()
  27. wandb_files = list(_find_wandb_files(wandb_run_path, skip_synced=False))
  28. if len(wandb_files) == 0:
  29. _fatal(f"Could not find a .wandb file in {wandb_run_path}.")
  30. elif len(wandb_files) > 1:
  31. _fatal(f"Found multiple .wandb files in {wandb_run_path}.")
  32. return wandb_files[0]
  33. def launch(path: str | None) -> Never:
  34. get_sentry().configure_scope(process_context="leet")
  35. wandb_file = _wandb_file_path(path)
  36. try:
  37. core_path = get_core_path()
  38. args = [core_path, "leet"]
  39. if not error_reporting_enabled():
  40. args.append("--no-observability")
  41. if is_debug(default="False"):
  42. args.extend(["--log-level", "-4"])
  43. args.append(wandb_file)
  44. result = subprocess.run(
  45. args,
  46. env=os.environ,
  47. close_fds=True,
  48. )
  49. sys.exit(result.returncode)
  50. except Exception as e:
  51. get_sentry().reraise(e)