class_docstr: A reference to data persisted as a directory in local or remote storage.
  
  Access the checkpoint contents locally using ``checkpoint.to_directory()``
  or ``checkpoint.as_directory``.
  
  Example creating a checkpoint using ``Checkpoint.from_directory``:
  
      >>> from ray.train import Checkpoint
      >>> checkpoint = Checkpoint.from_directory("/tmp/example_checkpoint_dir")
      >>> checkpoint.filesystem  # doctest: +ELLIPSIS
      <pyarrow._fs.LocalFileSystem object...
      >>> checkpoint.path
      '/tmp/example_checkpoint_dir'
  
  Example creating a checkpoint from a remote URI:
  
      >>> checkpoint = Checkpoint("s3://bucket/path/to/checkpoint")
      >>> checkpoint.filesystem  # doctest: +ELLIPSIS
      <pyarrow._s3fs.S3FileSystem object...
      >>> checkpoint.path
      'bucket/path/to/checkpoint'
  
  Example creating a checkpoint with a custom filesystem:
  
      >>> checkpoint = Checkpoint(
      ...     path="bucket/path/to/checkpoint",
      ...     filesystem=pyarrow.fs.S3FileSystem(),
      ... )
      >>> checkpoint.filesystem  # doctest: +ELLIPSIS
      <pyarrow._s3fs.S3FileSystem object...
      >>> checkpoint.path
      'bucket/path/to/checkpoint'
  
  Attributes:
      path: A path on the filesystem containing the checkpoint contents.
      filesystem: PyArrow FileSystem that can be used to access data at the `path`.
class_docstr_tok: ["A","reference","to","data","persisted","as","a","directory","in","local","or","remote","storage",".","Access","the","checkpoint","contents","locally","using","``","checkpoint.to_directory","(",")","``","or","``","checkpoint.as_directory","``",".","Example","creating","a","checkpoint","using","``","Checkpoint.from_directory","``",":",">",">",">","from","ray.train","import","Checkpoint",">",">",">","checkpoint","=","Checkpoint.from_directory","(","``","\/tmp\/example_checkpoint_dir","''",")",">",">",">","checkpoint.filesystem","#","doctest",":","+ELLIPSIS","<","pyarrow._fs.LocalFileSystem","object","...",">",">",">","checkpoint.path","'\/tmp\/example_checkpoint_dir'","Example","creating","a","checkpoint","from","a","remote","URI",":",">",">",">","checkpoint","=","Checkpoint","(","``","s3",":","\/\/bucket\/path\/to\/checkpoint","''",")",">",">",">","checkpoint.filesystem","#","doctest",":","+ELLIPSIS","<","pyarrow._s3fs.S3FileSystem","object","...",">",">",">","checkpoint.path","'bucket\/path\/to\/checkpoint'","Example","creating","a","checkpoint","with","a","custom","filesystem",":",">",">",">","checkpoint","=","Checkpoint","(","...","path=","''","bucket\/path\/to\/checkpoint","''",",","...","filesystem=pyarrow.fs.S3FileSystem","(",")",",","...",")",">",">",">","checkpoint.filesystem","#","doctest",":","+ELLIPSIS","<","pyarrow._s3fs.S3FileSystem","object","...",">",">",">","checkpoint.path","'bucket\/path\/to\/checkpoint'","Attributes",":","path",":","A","path","on","the","filesystem","containing","the","checkpoint","contents",".","filesystem",":","PyArrow","FileSystem","that","can","be","used","to","access","data","at","the","`","path","`","."]
docstr: Write checkpoint data to a local directory.
  
  *If multiple processes on the same node call this method simultaneously,*
  only a single process will perform the download, while the others
  wait for the download to finish. Once the download finishes, all processes
  receive the same local directory to read from.
  
  Args:
      path: Target directory to download data to. If not specified,
          this method will use a temporary directory.
  
  Returns:
      str: Directory containing checkpoint data.
docstr_tok: ["Write","checkpoint","data","to","a","local","directory",".","*","If","multiple","processes","on","the","same","node","call","this","method","simultaneously",",","*","only","a","single","process","will","perform","the","download",",","while","the","others","wait","for","the","download","to","finish",".","Once","the","download","finishes",",","all","processes","receive","the","same","local","directory","to","read","from",".","Args",":","path",":","Target","directory","to","download","data","to",".","If","not","specified",",","this","method","will","use","a","temporary","directory",".","Returns",":","str",":","Directory","containing","checkpoint","data","."]
code: def to_directory(
      self, path: Optional[Union[str, os.PathLike]] = None
  ) -> str:
      """Write checkpoint data to a local directory.
  
      *If multiple processes on the same node call this method simultaneously,*
      only a single process will perform the download, while the others
      wait for the download to finish. Once the download finishes, all processes
      receive the same local directory to read from.
  
      Args:
          path: Target directory to download data to. If not specified,
              this method will use a temporary directory.
  
      Returns:
          str: Directory containing checkpoint data.
      """
      user_provided_path = path is not None
      local_path = (
          path
          if user_provided_path
          else self._get_temporary_checkpoint_dir()
      )
      local_path = os.path.normpath(os.path.expanduser(str(local_path)))
      os.makedirs(local_path, exist_ok=True)
  
      try:
          # Timeout 0 means there will be only one attempt to acquire
          # the file lock. If it cannot be acquired, throw a TimeoutError
          with TempFileLock(local_path, timeout=0):
              _download_from_fs_path(
                  fs=self.filesystem,
                  fs_path=self.path,
                  local_path=local_path,
              )
      except TimeoutError:
          # if the directory is already locked, then wait but do not do anything.
          with TempFileLock(local_path, timeout=-1):
              pass
          if not os.path.exists(local_path):
              raise RuntimeError(
                  f"Checkpoint directory {local_path} does not exist, "
                  "even though it should have been created by "
                  "another process. Please raise an issue on GitHub: "
                  "https://github.com/ray-project/ray/issues"
              )
  
      return local_path
  
imports: import contextlib
  import glob
  import json
  import logging
  import os
  import platform
  import shutil
  import tempfile
  import traceback
  import uuid
  from typing import Any, Dict, Iterator, List, Optional, Union
  import pyarrow.fs
  from ray.air._internal.filelock import TempFileLock
  from ray.train._internal.storage import _download_from_fs_path, _exists_at_fs_path
  from ray.util.annotations import PublicAPI
label_desc: Use image node_id 7 for calling the Checkpoint obj's underlying member method code with example usage: obj.to_directory(path) and returns: local_path