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
+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