[Serve] [runtime env] Replace os.rename with shutil.move in remove_dir_from_filepaths() (#22018)

Currently, the `remove_dir_from_filepaths()` function uses `os.rename()` when shifting directories and files. This change replaces [`os.rename()`](https://docs.python.org/3/library/os.html#os.rename) with [`shutil.move()`](https://docs.python.org/3/library/shutil.html#shutil.move) to support these operations even when the directory's parent and the temporary directory are located on separate file systems.
This commit is contained in:
shrekris-anyscale 2022-02-01 12:33:53 -08:00 committed by simon-mo
parent d3b62f3958
commit 6f240c9c34

View file

@ -542,14 +542,17 @@ def remove_dir_from_filepaths(base_dir: str, rdir: str):
# Move rdir to a temporary directory, so its contents can be moved to
# base_dir without any name conflicts
with TemporaryDirectory() as tmp_dir:
os.rename(os.path.join(base_dir, rdir), os.path.join(tmp_dir, rdir))
# shutil.move() is used instead of os.rename() in case rdir and tmp_dir
# are located on separate file systems
shutil.move(os.path.join(base_dir, rdir), os.path.join(tmp_dir, rdir))
# Shift children out of rdir and into base_dir
rdir_children = os.listdir(os.path.join(tmp_dir, rdir))
for child in rdir_children:
os.rename(
os.path.join(tmp_dir, rdir, child),
os.path.join(base_dir, child))
shutil.move(
os.path.join(tmp_dir, rdir, child), os.path.join(base_dir, child)
)
def unzip_package(package_path: str,