From 83001323a2f70372d0d41398dc22b9e21fb5e860 Mon Sep 17 00:00:00 2001 From: Gary Oberbrunner Date: Fri, 18 Feb 2011 03:01:29 +0000 Subject: Fix #2708 by making Delete able to delete broken symlinks and dir symlinks. Thanks to David Garcia Garzon for the patch. --- src/engine/SCons/Defaults.py | 11 ++++++----- test/Delete.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/engine/SCons/Defaults.py b/src/engine/SCons/Defaults.py index bbc2571..a99bcc7 100644 --- a/src/engine/SCons/Defaults.py +++ b/src/engine/SCons/Defaults.py @@ -199,14 +199,15 @@ def delete_func(dest, must_exist=0): dest = [dest] for entry in dest: entry = str(entry) - if not must_exist and not os.path.exists(entry): + # os.path.exists returns False with broken links that exist + entry_exists = os.path.exists(entry) or os.path.islink(entry) + if not entry_exists and not must_exist: continue - if not os.path.exists(entry) or os.path.isfile(entry): - os.unlink(entry) - continue - else: + # os.path.isdir returns True when entry is a link to a dir + if os.path.isdir(entry) and not os.path.islink(entry): shutil.rmtree(entry, 1) continue + os.unlink(entry) def delete_strfunc(dest, must_exist=0): return 'Delete(%s)' % get_paths_str(dest) diff --git a/test/Delete.py b/test/Delete.py index f3dfcb6..0740a7c 100644 --- a/test/Delete.py +++ b/test/Delete.py @@ -37,6 +37,10 @@ test = TestSCons.TestSCons() test.write('SConstruct', """ Execute(Delete('f1')) Execute(Delete('d2')) +Execute(Delete('symlinks/filelink')) +Execute(Delete('symlinks/brokenlink')) +Execute(Delete('symlinks/dirlink')) + def cat(env, source, target): target = str(target[0]) f = open(target, "wb") @@ -90,10 +94,20 @@ test.subdir('d13.in') test.write('f14', "f14\n") test.subdir('d15') test.write('f16.in', "f16.in\n") +test.subdir('symlinks') +test.subdir('symlinks/dirtarget') +test.write('symlinks/dirtarget/dircontent', 'dircontent content\n') +test.write('symlinks/filetarget', 'filetarget content\n') +test.symlink('filetarget', 'symlinks/filelink') +test.symlink('dirtarget', 'symlinks/dirlink') +test.symlink('brokentarget', 'symlinks/brokenlink') expect = test.wrap_stdout(read_str = """\ Delete("f1") Delete("d2") +Delete("symlinks/filelink") +Delete("symlinks/brokenlink") +Delete("symlinks/dirlink") """, build_str = """\ Delete("d11-nonexistent.out") @@ -135,6 +149,13 @@ test.must_exist('f9.out-Delete') test.must_exist('f14') test.must_exist('d15') test.must_not_exist('f16.out') +test.must_exist('symlinks') +test.must_exist('symlinks/dirtarget') +test.must_exist('symlinks/dirtarget/dircontent') +test.must_exist('symlinks/filetarget') +test.must_exist('symlinks/filelink') +test.must_exist('symlinks/brokenlink') +test.must_exist('symlinks/dirlink') test.run() @@ -159,6 +180,13 @@ test.must_exist('d13-nonexistent.out') test.must_not_exist('f14') test.must_not_exist('d15') test.must_match('f16.out', "f16.in\n") +test.must_exist('symlinks') +test.must_exist('symlinks/dirtarget') +test.must_exist('symlinks/dirtarget/dircontent') +test.must_exist('symlinks/filetarget') +test.must_not_exist('symlinks/filelink') +test.must_not_exist('symlinks/brokenlink') +test.must_not_exist('symlinks/dirlink') test.write("SConstruct", """\ def cat(env, source, target): -- cgit v0.12