From dfd2a45f69d064f6854087360469a285905820bc Mon Sep 17 00:00:00 2001 From: Edward Oakes Date: Mon, 2 Sep 2019 13:31:10 -0700 Subject: [PATCH] Simplify symlinking and don't print warnings (#5615) --- python/ray/node.py | 4 ++-- python/ray/utils.py | 41 +++++++++++++---------------------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/python/ray/node.py b/python/ray/node.py index d22819b22..e016ffb75 100644 --- a/python/ray/node.py +++ b/python/ray/node.py @@ -19,7 +19,7 @@ import ray import ray.ray_constants as ray_constants import ray.services from ray.resource_spec import ResourceSpec -from ray.utils import try_to_create_directory, try_to_symlink_directory +from ray.utils import try_to_create_directory, try_to_symlink # Logger for this module. It should be configured at the entry point # into the program using Ray. Ray configures it by default automatically @@ -176,7 +176,7 @@ class Node(object): # Send a warning message if the session exists. try_to_create_directory(self._session_dir) - try_to_symlink_directory(self._session_dir, session_symlink) + try_to_symlink(session_symlink, self._session_dir) # Create a directory to be used for socket files. self._sockets_dir = os.path.join(self._session_dir, "sockets") try_to_create_directory(self._sockets_dir, warn_if_exist=False) diff --git a/python/ray/utils.py b/python/ray/utils.py index c6016c43e..1914b2615 100644 --- a/python/ray/utils.py +++ b/python/ray/utils.py @@ -616,47 +616,32 @@ def try_to_create_directory(directory_path, warn_if_exist=True): try_make_directory_shared(directory_path) -def try_to_symlink_directory(directory_path, symlink_path): - """Attempt to create a symlink to an existing directory. +def try_to_symlink(symlink_path, target_path): + """Attempt to create a symlink. - If the directory doesn't exist, the symlink path exists, or the symlink - failed to be created, a warning will be logged and the symlink will not - be created. If a symlink exists in the path, it will be attempted to be + If the symlink path exists and isn't a symlink, the symlink will not be + created. If a symlink exists in the path, it will be attempted to be removed and replaced. Args: - directory_path: The path of the existing directory. - symlink_path: The path of the symlink to create. + symlink_path: The path at which to create the symlink. + target_path: The path the symlink should point to. """ - logger = logging.getLogger("ray") - directory_path = os.path.expanduser(directory_path) symlink_path = os.path.expanduser(symlink_path) - if not os.path.exists(directory_path): - logger.warning("Attempted to create symlink to directory '{}', but " - "the directory doesn't exist.".format(directory_path)) - return - elif not os.path.isdir(directory_path): - logger.warning("Attempted to create symlink to directory '{}', but " - "the '{}' isn't a directory.".format( - directory_path, directory_path)) - return + target_path = os.path.expanduser(target_path) if os.path.exists(symlink_path): if os.path.islink(symlink_path): + # Try to remove existing symlink. try: os.remove(symlink_path) - except OSError as e: - logger.warning("Failed to remove existing symlink '{}': {}" - .format(symlink_path, e)) + except OSError: return else: - logger.warning("Attempted to create symlink '{}' to directory '{}'" - ", but the symlink path exists and isn't a symlink." - .format(symlink_path, directory_path)) + # There's an existing non-symlink file, don't overwrite it. return try: - os.symlink(directory_path, symlink_path) - except OSError as e: - logger.warning("Failed to create symlink '{}' to directory '{}': {}" - .format(symlink_path, directory_path, e)) + os.symlink(target_path, symlink_path) + except OSError: + return