我如何生成多行构建命令?

在SCons中,我的命令生成器创建了非常长的命令行. ID
希望能够将这些命令分为多行
生成日志中的可读性.

例如我有一个像这样的SConscipt:

import os

# create dependency
def my_cmd_generator(source, target, env, for_signature):
    return r'''echo its a small world after all \
        its a small world after all'''

my_cmd_builder = Builder(generator=my_cmd_generator, suffix = '.foo')

env = Environment()
env.Append( BUILDERS = {'MyCmd' : my_cmd_builder } )

my_cmd = env.MyCmd('foo.foo',os.popen('which bash').read().strip())
AlwaysBuild(my_cmd)

当它执行时,我得到:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
echo its a small world after all \
its a small world after all
its a small world after all
sh: line 1: its: command not found
scons: *** [foo.foo] Error 127
scons: building terminated because of errors.

使用os.system和os.popen在python shell中执行此操作-我得到了可读的命令字符串,并且子shell进程将所有行解释为一个命令.

>>> import os
>>> cmd = r'''echo its a small world after all \
... its a small world after all'''
>>> print cmd
echo its a small world after all \
its a small world after all
>>> os.system( cmd)
its a small world after all its a small world after all
0

当我在SCons中执行此操作时,它一次执行每一行,即
不是我想要的

我也想避免将命令构建到shell脚本中,
然后执行外壳程序脚本,因为这将创建字符串
逃避疯狂.

这可能吗?

更新:
库纳佩
感谢您提供有关$CCCOMSTR的线索.不幸的是,我没有开箱即用SCons支持的任何语言,因此我正在创建自己的命令生成器.使用生成器,我如何获得SCons:

echo its a small world after all its a small world after all' 

但是打印

echo its a small world after all \
    its a small world after all

解决方法:

多亏了cournape关于“操作与生成器”(以及Eclipse pydev调试器)的技巧,我终于弄清楚了我需要做的事情.您想将函数作为“操作”而不是“生成器”传递给“ Builder”类.这将允许您实际直接执行os.system或os.popen调用.这是更新的代码:

import os

def my_action(source, target, env):
    cmd = r'''echo its a small world after all \
        its a small world after all'''
    print cmd
    return os.system(cmd)

my_cmd_builder = Builder(
    action=my_action,  # <-- CRUCIAL PIECE OF SOLUTION
    suffix = '.foo')

env = Environment()
env.Append( BUILDERS = {'MyCmd' : my_cmd_builder } )

my_cmd = env.MyCmd('foo.foo',os.popen('which bash').read().strip())

此SConstruct文件将产生以下输出:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
my_action(["foo.foo"], ["/bin/bash"])
echo its a small world after all \
        its a small world after all
its a small world after all its a small world after all
scons: done building targets.

另一个至关重要的方面是要记住,从“生成器”切换到“动作”意味着您要构建的目标不再对要传递给子流程外壳的实际字符串具有隐式依赖性.您可以通过将字符串添加到环境中来重新创建此依赖性.

例如,我个人想要的解决方案如下所示:

import os

cmd = r'''echo its a small world after all \
        its a small world after all'''

def my_action(source, target, env):
    print cmd
    return os.system(cmd)

my_cmd_builder = Builder(
    action=my_action,
    suffix = '.foo')

env = Environment()
env['_MY_CMD'] = cmd  # <-- CREATE IMPLICIT DEPENDENCY ON CMD STRING
env.Append( BUILDERS = {'MyCmd' : my_cmd_builder } )

my_cmd = env.MyCmd('foo.foo',os.popen('which bash').read().strip())
上一篇:【转载】scons基础用法介绍举例


下一篇:如何让SCons不包含zip文件中的基础目录?