core.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Core variables, functions, and classes that we want in the wandb
  2. module but are also used in modules that import the wandb module.
  3. The purpose of this module is to break circular imports.
  4. """
  5. import os
  6. import tempfile
  7. import time
  8. import wandb
  9. from wandb import env
  10. # We use the hidden version if it already exists, otherwise non-hidden.
  11. if os.path.exists(os.path.join(env.get_dir(os.getcwd()), ".wandb")):
  12. __stage_dir__ = ".wandb" + os.sep
  13. elif os.path.exists(os.path.join(env.get_dir(os.getcwd()), "wandb")):
  14. __stage_dir__ = "wandb" + os.sep
  15. else:
  16. __stage_dir__ = None
  17. wandb.START_TIME = time.time()
  18. def wandb_dir(root_dir=None):
  19. if root_dir is None or root_dir == "":
  20. try:
  21. cwd = os.getcwd()
  22. except OSError:
  23. wandb.termwarn("os.getcwd() no longer exists, using system temp directory")
  24. cwd = tempfile.gettempdir()
  25. root_dir = env.get_dir(cwd)
  26. path = os.path.join(root_dir, __stage_dir__ or ("wandb" + os.sep))
  27. if not os.access(root_dir, os.W_OK):
  28. wandb.termwarn(
  29. f"Path {path} wasn't writable, using system temp directory", repeat=False
  30. )
  31. path = os.path.join(tempfile.gettempdir(), __stage_dir__ or ("wandb" + os.sep))
  32. return path
  33. def _set_stage_dir(stage_dir):
  34. # Used when initing a new project with "wandb init"
  35. global __stage_dir__
  36. __stage_dir__ = stage_dir
  37. __all__ = [
  38. "__stage_dir__",
  39. "START_TIME",
  40. "wandb_dir",
  41. "_set_stage_dir",
  42. ]