.EE
.TP
-.RI Delete( entry )
+.RI Delete( entry ", [" must_exist ])
Returns an Action that
deletes the specified
.IR entry ,
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
env.Command('foo.out', 'foo.in',
[Delete('${TARGET.dir}'),
MyBuildAction])
+
+Execute(Delete('file_that_must_exist', must_exist=1))
.EE
.TP
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)
"""
import os.path
+import string
import TestSCons
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")
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")
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()