cleanup_test_state.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. This script is used to clean up state after running test scripts, including
  3. on external services. For instance, this script can be used to remove the runs
  4. from WandB that have been saved during unit testing or when running examples.
  5. """
  6. import sys
  7. def clear_wandb_project():
  8. import wandb
  9. # This is hardcoded in the `ray/air/examples/upload_to_wandb.py` example
  10. wandb_projects = [
  11. "ray_air_example",
  12. "ray_air_example_xgboost",
  13. "ray_air_example_torch",
  14. ]
  15. for wandb_project in wandb_projects:
  16. api = wandb.Api()
  17. for run in api.runs(wandb_project):
  18. run.delete()
  19. def clear_comet_ml_project():
  20. import comet_ml
  21. # This is hardcoded in the `ray/air/examples/upload_to_comet_ml.py` example
  22. comet_ml_project = "ray-air-example"
  23. api = comet_ml.API()
  24. workspace = api.get_default_workspace()
  25. experiments = api.get_experiments(
  26. workspace=workspace, project_name=comet_ml_project
  27. )
  28. api.delete_experiments([experiment.key for experiment in experiments])
  29. SERVICES = {"wandb": clear_wandb_project, "comet_ml": clear_comet_ml_project}
  30. if __name__ == "__main__":
  31. if len(sys.argv) < 2:
  32. print(f"Usage: python {sys.argv[0]} <service1> [service2] ...")
  33. sys.exit(0)
  34. services = sys.argv[1:]
  35. if any(service not in SERVICES for service in services):
  36. raise RuntimeError(
  37. f"All services must be included in {list(SERVICES.keys())}. "
  38. f"Got: {services}"
  39. )
  40. for service in services:
  41. try:
  42. SERVICES[service]()
  43. except Exception as e:
  44. print(f"Could not cleanup service test state for {service}: {e}")