【问题标题】:Calling yeoman generator from python?从 python 调用 yeoman 生成器?
【发布时间】:2015-02-03 19:43:13
【问题描述】:

我需要在我一直在开发的 web 应用程序中通过 python 调用这个生成器。最初我通过管道向生成器发送“是”来做到这一点,但我需要对第一个选项说“不”。

yes | yo angular --no-insight

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

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

输入.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()

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

【问题讨论】:

  • 如果从命令行运行<input.txt yo angular --no-insight >out.txt 会发生什么? (它有效吗?)如何在 Python 中运行命令? (显示代码)
  • 同样的事情发生,第一个提示被回答为否,然后没有其他输入被传递。该帖子已更新,包括我的公开电话。
  • 您是否也尝试过重定向stderr?在 bash 中:&> out.txt

标签: python django yeoman popen


【解决方案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”,然后写入标准输入并从标准输出读取。

【讨论】:

  • 要将子进程的输出重定向到文件,请改用check_call(cmd, stdout=file_output)
  • 感谢您的评论,使用 file_output.write() 解决方案给出了相同的结果,我输入的第一行被传入,然后没有进一步的输入通过。我忘了提到我已经在使用子流程了。我将更多地使用 Popens 标准输入和标准输出重定向来使其正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-18
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多