从python调用yeoman生成器?

时间:2015-02-03 19:43:14

标签: python django yeoman popen

我需要在我正在研究的webapp中通过python调用这个生成器。最初我通过向发电机输出“是”来做到这一点,但我需要对第一个选项说“不”。

yes | yo angular --no-insight

现在我将文件的输出发送到生成器,告诉它输入

cat input.txt | yo angular --no-insight

input.txt中

n
y
y

这很好用,但我还需要存储日志,所以我将输出重定向到out.txt

cat input.txt | yo angular --no-insight > out.txt

这是出问题的地方,第一个提示是否按照我的预期回答,然后一切都停止了。

我需要一种以编程方式运行生成器的方法,我认为使用适配器将是解决方案,但似乎绝对没有编写文档的文档。

我应该怎么做以适应我的用例?

编辑:这是我用来运行命令的popen调用

        Popen("cat input.txt | yo angular --no-insight", shell=True).wait()

运行该命令的类在其自己的线程上,因此等待几分钟不是问题。但是,我可能会在将来的更新中删除它,以便在出现任何问题时处理生成器的超时。

1 个答案:

答案 0 :(得分:1)

你能使用python的subprocess module

with open('out.txt', 'w') as file_output:
    file_output.write(subprocess.check_output('cat input.txt | yo angular --no-insight', shell=True))

你也可以创建一个Popen对象'yo angular --no-insight'并写入stdin并从stdout读取。

相关问题