如何使用Python自动安装程序?

如何使用Python自动安装程序?,python,installation,stdout,stdin,silent-installer,Python,Installation,Stdout,Stdin,Silent Installer,所以我们有我们的软件产品,我目前正在研究一种自动化安装的方法。具体做法是: 我有一个config.cfg文件,其中包含有关Apache、Tomcat和MySQL的用户和密码以及端口号的所有信息 我使用ConfigParser来获得这些信息 安装程序脚本是迭代的:您可以像sudofoo.run那样执行它,它会提示命令行,以便进行安装的人员可以输入上述输入。例如: Apache user: **[foo]** Apache password: **[bar]** 输入该信息后,安装程序将实际

所以我们有我们的软件产品,我目前正在研究一种自动化安装的方法。具体做法是:

  • 我有一个
    config.cfg
    文件,其中包含有关
    Apache
    Tomcat
    MySQL的
    用户和密码以及端口号的所有信息
  • 我使用
    ConfigParser
    来获得这些信息
安装程序脚本是迭代的:您可以像
sudofoo.run
那样执行它,它会提示命令行,以便进行安装的人员可以输入上述输入。例如:

Apache user: **[foo]** 
Apache password: **[bar]**
输入该信息后,安装程序将实际安装该产品。但是,我想自动化整个过程,包括使用
Python
输入这些信息

到目前为止,我得到的是:

  1 from subprocess import Popen, call, PIPE, STDOUT
  2 import errno
  3 import os
  4 fname = "/home/ec2-user/foo.run"
  5 os.chmod(fname, 0777) # make it executable
  6 cmd = "sudo %s" % fname
  7 print cmd
  9 p = Popen(cmd, stdin=PIPE, stdout=PIPE,
 10           stderr=STDOUT, shell=True)
 11 # now is actually the missing part ... reading from the stdout and writing
 12 # to stdin ... how could I accomplish that? Is there a easier way of doing it?
 13 p.wait()
因此,我在设计
stdin
/
stdout
部分如何工作时遇到了一些问题。。。 我需要一个
while
循环来读取它,直到
stdout
为空,然后我将注入
ConfigParser
中包含的数据。
有人能举例说明如何做到这一点吗?还有其他更简单的方法吗?我愿意接受任何建议。。。对我来说,第一步可能是从python执行手动安装程序,使用
python的
p.stdin.write()
将信息提供给安装程序。

使用而不是子进程来运行程序。它的工作是从python运行交互式命令行程序,参考资料中有一些示例可供使用。有一个需要手动输入的安装程序,然后不得不做各种复杂的工作来自动提供手动输入,这似乎有些奇怪。我工作的地方也有基于python的产品。它们会自动安装并使用shell脚本来配置apache、mysql等。您应该使用pexpect,或者更好的做法是重新考虑
foo。运行
安装程序直接从配置文件获取这些参数。