Simplify symlinking and don't print warnings (#5615)

This commit is contained in:
Edward Oakes
2019-09-02 13:31:10 -07:00
committed by Richard Liaw
parent d37c09aac0
commit dfd2a45f69
2 changed files with 15 additions and 30 deletions
+2 -2
View File
@@ -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)
+13 -28
View File
@@ -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