Delete() should not blow up if what it's deleting doesn't exist.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 19 Aug 2004 14:12:31 +0000 (14:12 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 19 Aug 2004 14:12:31 +0000 (14:12 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@1040 fdb21ef1-2011-0410-befe-b5e4ea1792b1

doc/man/scons.1
src/CHANGES.txt
src/engine/SCons/Defaults.py
test/Delete.py

index 0c07aecc12f6951cf5d3a98afbfe33a3386d446c..54a76abc0d586238da267950aaa6f4143a7fe8b8 100644 (file)
@@ -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
index 3bf91ce937f2337b126b30f7866746424da61bd2..a3040a9385064ffb70eabbbcc59f0a8cc2035e9b 100644 (file)
@@ -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.
 
 
 
index 8100869a737cf96297036abc300556b30296e95e..c75ac25222178bb4b6d09cf2746f039f0c42336d 100644 (file)
@@ -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)
index 4f69c5f43219b2a5d13ad43dfe07b783971c9bb8..c57a810f3cc867f822b199592ebbe61165312331 100644 (file)
@@ -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()