SCons在任何Action失败时删除$ TARGET

时间:2015-04-09 18:32:54

标签: scons

有没有办法模仿Make .DELETE_ON_FAILURE行为?如果我有一个构建器执行一系列动作来生成目标,我希望它们能够以原子方式运行。如果较早的Action生成(不完整)文件,并且后面的操作无法修改它,我希望删除目标文件,而不是保持其未完成状态。

考虑这个SConstruct文件:

def example(target, source, env):
    raise Exception('failure')
    # more processing that never happens...

action_list = [
    Copy('$TARGET', '$SOURCE'),
    Chmod('$TARGET', 0755),
    example,
]

Command(
    action = action_list,
    target = 'foo.out',
    source = 'foo.in',
)

如果example操作失败,foo.out仍然存在,因为前两个操作成功。但是,它不完整。

有趣的是,再次运行scons会导致再次重试构建foo.out,即使它存在于文件系统中。

1 个答案:

答案 0 :(得分:3)

是的,您要找的是GetBuildFailures

扩展您的示例以包含此功能...

import atexit
import os

def delete_on_failure():
    from SCons.Script import GetBuildFailures
    for bf in GetBuildFailures():
        if os.path.isfile(bf.node.abspath):
            print 'Removing %s' % bf.node.path
            os.remove(bf.node.abspath)
atexit.register(delete_on_failure)

def example(target, source, env):
    raise Exception('failure')
    # more processing that never happens...

action_list = [
    Copy('$TARGET', '$SOURCE'),
    Chmod('$TARGET', 0755),
    example,
]

Command(
    action = action_list,
    target = 'foo.out',
    source = 'foo.in',
)

运行时产生以下内容......

>> scons --version
SCons by Steven Knight et al.:
    script: v2.3.4, 2014/09/27 12:51:43, by garyo on lubuntu
    engine: v2.3.4, 2014/09/27 12:51:43, by garyo on lubuntu
    engine path: ['/usr/lib/scons/SCons']
Copyright (c) 2001 - 2014 The SCons Foundation

>> tree 
.
├── foo.in
└── SConstruct

0 directories, 2 files

>> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
Copy("foo.out", "foo.in")
Chmod("foo.out", 0755)
example(["foo.out"], ["foo.in"])
scons: *** [foo.out] Exception : failure
Traceback (most recent call last):
  File "/usr/lib/scons/SCons/Action.py", line 1065, in execute
    result = self.execfunction(target=target, source=rsources, env=env)
  File "/path/to/SConstruct", line 13, in example
    raise Exception('failure')
Exception: failure
scons: building terminated because of errors.
Removing foo.out

>> tree
.
├── foo.in
└── SConstruct

0 directories, 2 files