code: def safe_open(
      path: StrPath, mode: str = "r", *args: Any, **kwargs: Any
  ) -> Generator[IO, None, None]:
      """Open a file, ensuring any changes only apply atomically after close.
  
      This context manager ensures that even unsuccessful writes will not leave a "dirty"
      file or overwrite good data, and that all temp data is cleaned up.
  
      The semantics and behavior are intended to be nearly identical to the built-in
      open() function. Differences:
          - It creates any parent directories that don't exist, rather than raising.
          - In 'x' mode, it checks at the beginning AND end of the write and fails if the
              file exists either time.
      """
      path = Path(path).resolve()
      path.parent.mkdir(parents=True, exist_ok=True)
  
      if "x" in mode and path.exists():
          raise FileExistsError(f"{path!s} already exists")
  
      if "r" in mode and "+" not in mode:
          # This is read-only, so we can just open the original file.
          # TODO (hugh): create a reflink and read from that.
          with path.open(mode, *args, **kwargs) as f:
              yield f
          return
  
      with tempfile.TemporaryDirectory(dir=path.parent) as tmp_dir:
          tmp_path = Path(tmp_dir) / path.name
  
          if ("r" in mode or "a" in mode) and path.exists():
              # We need to copy the original file in order to support reads and appends.
              # TODO (hugh): use reflinks to avoid the copy on platforms that support it.
              shutil.copy2(path, tmp_path)
  
          with tmp_path.open(mode, *args, **kwargs) as f:
              yield f
              f.flush()
              os.fsync(f.fileno())
  
          if "x" in mode:
              # Ensure that if another process has beaten us to writing the file we raise
              # rather than overwrite. os.link() atomically creates a hard link to the
              # target file and will raise FileExistsError if the target already exists.
              os.link(tmp_path, path)
              os.unlink(tmp_path)
          else:
              tmp_path.replace(path)
  
imports: import contextlib
  import ctypes
  import errno
  import logging
  import os
  import platform
  import re
  import shutil
  import tempfile
  import threading
  from pathlib import Path
  from typing import IO, Any, BinaryIO, Generator, Optional
  from wandb.sdk.lib.paths import StrPath
label_desc: Use image node_id 6 for calling a global function with example usage: safe_open(path, mode) without return types