From 314c902a642b5cdc03eb9f4f6141574ecb22180e Mon Sep 17 00:00:00 2001 From: Kunal Bhalla Date: Fri, 24 May 2024 15:39:22 -0700 Subject: [PATCH] Allow over-writing existing targets Depending on the conda environment, the same file may be written to multiple times (eg. if a specific lib.so file exists, multiple conda packages may have it and at pack time conda-pack will pull from the cache'd original copy of the package and copy it in; when multiple packages have the same lib.so file they'll over-write each other). shutil.copy2 doesn't handle overwriting existing symlinks cleanly: it overwrites files silently but fails on creating a symlink (probably worth a separate PR to cpython later). Allow for this by explicitly deleting existing targets. --- conda_pack/formats.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/conda_pack/formats.py b/conda_pack/formats.py index 39d9cfc..cc5c8cb 100644 --- a/conda_pack/formats.py +++ b/conda_pack/formats.py @@ -506,6 +506,10 @@ def _add(self, source, target): self.copy_func = partial(shutil.copy2, follow_symlinks=False) if os.path.isfile(source) or os.path.islink(source): + if os.path.islink(target_abspath) or os.path.isfile(target_abspath): + os.remove(target_abspath) + elif os.path.isdir(target_abspath): + shutil.rmtree(target_abspath) self.copy_func(source, target_abspath) else: os.mkdir(target_abspath)