Python Fabric:如何回答键盘输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2246256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:10:08  来源:igfitidea点击:

Python Fabric: How to answer to keyboard input?

pythonautomationfabric

提问by dzen

I would like to automate the response for some question prompted by some programs, like mysql prompting for a password, or apt asking for a 'yes' or ... when I want to rebuild my haystack index with a ./manage.py rebuild_index.

我想自动响应某些程序提示的某些问题,例如 mysql 提示输入密码,或 apt 要求输入“是”或 ... .

For MySQL, I can use the --password= switch, and I'm sure that apt has a 'quiet' like option. But how can I pass the response to other programs ?

对于 MySQL,我可以使用 --password= 开关,并且我确信 apt 有一个类似“安静”的选项。但是如何将响应传递给其他程序?

采纳答案by Esteban Küber

Why can't you just use pipes?

为什么你不能只使用管道

For example, for an automated auto accept, just use yes, that just outputs a neverending stream of y.

例如,对于自动自动接受,只需使用yes,它只会输出一个永无止境的y.

yes | rm *.txt


(source: wikimedia.org)


(来源:wikimedia.org

回答by buckley

If you are looking for a user to confirm an operation, use the confrim method.

如果您正在寻找用户来确认操作,请使用 confrim 方法。

if fabric.contrib.console.confirm("You tests failed do you want to continue?"):
  #continue processing

Or if you are looking for a way to get input from the user, use the prompt method.

或者,如果您正在寻找一种从用户那里获取输入的方法,请使用提示方法。

password = fabric.operations.prompt("What is your password?")

回答by dzen

Those both methods are valid and works.

这两种方法都是有效的。

I choose the first one, because I didn't want to have any interaction with my deployment system.

我选择第一个,因为我不想与我的部署系统有任何交互。

So here is the solution I used:

所以这是我使用的解决方案:

% yes | ./manage.py rebuild_index

% yes | ./manage.py rebuild_index

WARNING: This will irreparably remove EVERYTHING from your search index. Your choices after this are to restore from backups or rebuild via the rebuild_indexcommand. Are you sure you wish to continue? [y/N] Removing all documents from your index because you said so. All documents removed. Indexing 27 Items.

WARNING: This will irreparably remove EVERYTHING from your search index. Your choices after this are to restore from backups or rebuild via the rebuild_indexcommand. Are you sure you wish to continue? [y/N] Removing all documents from your index because you said so. All documents removed. Indexing 27 Items.

回答by akv

The development version of Fabric (1.0a) now supports interaction with remote programs. http://docs.fabfile.org/1.0a/usage/interactivity.html

Fabric (1.0a) 的开发版本现在支持与远程程序的交互。 http://docs.fabfile.org/1.0a/usage/interactivity.html

回答by luoluo

Late answer, but hope this would help peoples having similar problem.

迟到的答案,但希望这会帮助有类似问题的人。

Different point:

不同点:

  1. Answer two or more different inputto console.
  2. parallel modeSupport.
  3. Any type of input yes/no/y/nincluded.
  1. 向控制台回答两个或更多不同的输入
  2. 并行模式支持。
  3. yes/no/y/n包括任何类型的输入。

Problem

问题

[hostxxx] out: Type 'c' if you want to use the Commercial Edition.
[hostxxx] out: Type 'o' if you want to use the Open Source Edition.
[hostxxx] out: Type '3' to view the GNU General Public License version 3.
[hostxxx] out: Type 'L' to view the Lesser GNU General Public License version 2.1.
[hostxxx] out: Type 'yes' to accept this license offer.
[hostxxx] out: Type 'no' to decline this license offer.

Solution:

解决方案:

Use printfinstead of yesto add more flexibility, meanwhile this works like a charm on parallelmode.

使用printf而不是yes增加更多的灵活性,同时这就像一个魅力parallel模式。

@parallel
def demo_multi_input():
    run('printf "o\nyes\n"|./configure --prefix=/home/work/bin/qt')

回答by Ziwen Lv

Use this code:

使用此代码:

run("echo yes|./manage.py rebuild_index")