From: stevenknight Date: Thu, 19 Aug 2004 14:12:31 +0000 (+0000) Subject: Delete() should not blow up if what it's deleting doesn't exist. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3c9b65ba18170e20c5d30802e8a1a29225bdcf10;p=scons.git Delete() should not blow up if what it's deleting doesn't exist. git-svn-id: http://scons.tigris.org/svn/scons/trunk@1040 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/doc/man/scons.1 b/doc/man/scons.1 index 0c07aecc..54a76abc 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -7617,7 +7617,7 @@ env.Command('bar.out', 'bar.in', .EE .TP -.RI Delete( entry ) +.RI Delete( entry ", [" must_exist ]) Returns an Action that deletes the specified .IR entry , @@ -7625,6 +7625,15 @@ which may be a file or a directory tree. If a directory is specified, the entire directory tree will be removed. +If the +.I must_exist +flag is set, +then a Python error will be thrown +if the specified entry does not exist; +the default is +.BR must_exist=0 , +that is, the Action will silently do nothing +if the entry does not exist. Examples: .ES @@ -7633,6 +7642,8 @@ Execute(Delete('/tmp/buildroot')) env.Command('foo.out', 'foo.in', [Delete('${TARGET.dir}'), MyBuildAction]) + +Execute(Delete('file_that_must_exist', must_exist=1)) .EE .TP diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 3bf91ce9..a3040a93 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -12,7 +12,9 @@ RELEASE 0.97 - XXX From Steven Knight - - XXX + - Add a must_exist flag to Delete() to let the user control whether + it's an error if the specified entry doesn't exist. The default + behavior is now to silently do nothing if it doesn't exist. diff --git a/src/engine/SCons/Defaults.py b/src/engine/SCons/Defaults.py index 8100869a..c75ac252 100644 --- a/src/engine/SCons/Defaults.py +++ b/src/engine/SCons/Defaults.py @@ -181,14 +181,18 @@ def copy_func(dest, src): Copy = ActionFactory(copy_func, lambda dest, src: 'Copy("%s", "%s")' % (dest, src)) -def delete_func(entry): +def delete_func(entry, must_exist=0): + if not must_exist and not os.path.exists(entry): + return None if os.path.isfile(entry): return os.unlink(entry) else: return shutil.rmtree(entry, 1) -Delete = ActionFactory(delete_func, - lambda entry: 'Delete("%s")' % entry) +def delete_strfunc(entry, must_exist=0): + return 'Delete("%s")' % entry + +Delete = ActionFactory(delete_func, delete_strfunc) Mkdir = ActionFactory(os.makedirs, lambda dir: 'Mkdir("%s")' % dir) diff --git a/test/Delete.py b/test/Delete.py index 4f69c5f4..c57a810f 100644 --- a/test/Delete.py +++ b/test/Delete.py @@ -29,6 +29,7 @@ Verify that the Delete() Action works. """ import os.path +import string import TestSCons @@ -52,6 +53,14 @@ env.Command('f8.out', 'f8.in', [Delete("$FILE"), Delete("$DIR"), Cat]) env.Command('f9.out', 'f9.in', [Cat, Delete("Delete-$SOURCE"), Delete("$TARGET-Delete")]) +env.Command('f10-nonexistent.out', 'f10.in', [Delete("$TARGET"), + Cat]) +env.Command('d11-nonexistent.out', 'd11.in', [Delete("$TARGET"), + Mkdir("$TARGET")]) +env.Command('f12-nonexistent.out', 'f12.in', [Delete("$TARGET", must_exist=0), + Cat]) +env.Command('d13-nonexistent.out', 'd13.in', [Delete("$TARGET", must_exist=0), + Mkdir("$TARGET")]) """) test.write('f1', "f1\n") @@ -68,12 +77,24 @@ test.write('f8.in', "f8.in\n") test.write('f9.in', "f9.in\n") test.write('Delete-f9.in', "Delete-f9.in\n") test.write('f9.out-Delete', "f9.out-Delete\n") +test.write('f10.in', "f10.in\n") +test.subdir('d11.in') +test.write('f12.in', "f12.in\n") +test.subdir('d13.in') expect = test.wrap_stdout(read_str = """\ Delete("f1") Delete("d2") """, build_str = """\ +Delete("d11-nonexistent.out") +Mkdir("d11-nonexistent.out") +Delete("d13-nonexistent.out") +Mkdir("d13-nonexistent.out") +Delete("f10-nonexistent.out") +cat(["f10-nonexistent.out"], ["f10.in"]) +Delete("f12-nonexistent.out") +cat(["f12-nonexistent.out"], ["f12.in"]) cat(["f3.out"], ["f3.in"]) Delete("f4") Delete("d5") @@ -117,5 +138,29 @@ test.must_match('f8.out', "f8.in\n") test.must_match('f9.out', "f9.in\n") test.must_not_exist('Delete-f9.in') test.must_not_exist('f9.out-Delete') +test.must_exist('f10-nonexistent.out') +test.must_exist('d11-nonexistent.out') +test.must_exist('f12-nonexistent.out') +test.must_exist('d13-nonexistent.out') + +test.write("SConstruct", """\ +def cat(env, source, target): + target = str(target[0]) + source = map(str, source) + f = open(target, "wb") + for src in source: + f.write(open(src, "rb").read()) + f.close() +Cat = Action(cat) +env = Environment() +env.Command('f14-nonexistent.out', 'f14.in', [Delete("$TARGET", must_exist=1), + Cat]) +""") + +test.write('f14.in', "f14.in\n") + +test.run(status=2, stderr=None) + +test.fail_test(string.find(test.stderr(), "No such file or directory") == -1) test.pass_test()