From 1d86d604b288df9e89f46e60e5d89865bbeaa384 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 23 Nov 2015 16:20:39 +0100 Subject: [PATCH] More reliable file copy behaviour - handle directory symlinks better, too --- makepanda/makepandacore.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/makepanda/makepandacore.py b/makepanda/makepandacore.py index f67ca9754d..c766dc946b 100644 --- a/makepanda/makepandacore.py +++ b/makepanda/makepandacore.py @@ -2504,8 +2504,12 @@ def CopyFile(dstfile, srcfile): if NeedsBuild([dstfile], [srcfile]): if os.path.islink(srcfile): # Preserve symlinks - if os.path.exists(dstfile): + if os.path.isfile(dstfile) or os.path.islink(dstfile): + print("Removing file %s" % (dstfile)) os.unlink(dstfile) + elif os.path.isdir(dstfile): + print("Removing directory %s" % (dstfile)) + shutil.rmtree(dstfile) os.symlink(os.readlink(srcfile), dstfile) else: WriteBinaryFile(dstfile, ReadBinaryFile(srcfile)) @@ -2536,24 +2540,37 @@ def CopyAllJavaSources(dir, skip=[]): JustBuilt([dstfile], [srcfile]) def CopyTree(dstdir, srcdir, omitVCS=True): - if (os.path.isdir(dstdir)): - for entry in os.listdir(srcdir): + if os.path.isdir(dstdir): + source_entries = os.listdir(srcdir) + for entry in source_entries: srcpth = os.path.join(srcdir, entry) dstpth = os.path.join(dstdir, entry) - if (os.path.isfile(srcpth)): + + if os.path.islink(srcpth) or os.path.isfile(srcpth): if not omitVCS or entry not in VCS_FILES: CopyFile(dstpth, srcpth) else: if not omitVCS or entry not in VCS_DIRS: CopyTree(dstpth, srcpth) + + # Delete files in dstdir that are not in srcdir. + for entry in os.listdir(dstdir): + if entry not in source_entries: + path = os.path.join(dstdir, entry) + if os.path.islink(path) or os.path.isfile(path): + os.remove(path) + elif os.path.isdir(path): + shutil.rmtree(path) else: if GetHost() == 'windows': srcdir = srcdir.replace('/', '\\') dstdir = dstdir.replace('/', '\\') cmd = 'xcopy /I/Y/E/Q "' + srcdir + '" "' + dstdir + '"' + oscmd(cmd) else: - cmd = 'cp -R -f ' + srcdir + ' ' + dstdir - oscmd(cmd) + if subprocess.call(['cp', '-R', '-f', srcdir, dstdir]) != 0: + exit("Copy failed.") + if omitVCS: DeleteVCS(dstdir)