我怎样才能生成多行生成命令?
-
19-08-2019 - |
题
在SCons的,我的命令发生器创建离谱长命令行。 ID 希望能够分割多行这些命令用于 可读性在生成日志。
e.g。我有一个像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作品蟒壳这样做 - 我得到一个可读命令串和副壳过程解释所有的行作为一个命令
。>>> 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脚本,因为这将创建的字符串 逸出疯狂。
这是可能的?
更新:结果 cournape,结果 感谢您对$ 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调试),我终于想通了什么,我需要做的。你想在你的函数传递到“生成器”类为“行动”不是“发电机”。这将允许你去实际执行使用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.
在其它关键部分是要记住,从一个“发电机”到“动作”切换意味着你正在构建不再目标已在要传递到子处理外壳的实际字符串的隐式相关性。通过添加字符串到环境中,您可以重新创建这种依赖性。
e.g,我想亲自溶液如下:
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的具有一种机制来改变印刷的命令。他们每个Action的注册,而且很多默认的可供选择:
env = Environment()
env['CCCOMSTR'] = "CC $SOURCE"
env['CXXCOMSTR'] = "CXX $SOURCE"
env['LINKCOM'] = "LINK $SOURCE"
将打印,假定只有C和CXX来源:
CC foo.c
CC bla.c
CXX yo.cc
LINK yo.o bla.o foo.o
不隶属于 StackOverflow