如何通过在python中登录来自动化rpmbuild?

时间:2023-01-15 11:23:19

Normally you can automate answers to an interactive prompt by piping stdin:

通常,您可以通过管道标准输入自动回答交互式提示:

import subprocess as sp

cmd = 'rpmbuild --sign --buildroot {}/BUILDROOT -bb {}'.format(TMPDIR, specfile)
p = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE, universal_newline=True, shell=True)

for out in p.communicate(input='my gpg passphrase\n'):
    print(out)

For whatever reason, this is not working for me. I've tried writing to p.stdin, before executing p.communicate(), I've tried flushing the buffer, I've tried using bytes without universal_newlines=True, I've hard coded things, etc. In all scenarios, the command is executed and hangs on:

无论出于何种原因,这对我不起作用。我已经尝试写p.stdin,在执行p.communicate()之前,我已经尝试刷新缓冲区,我尝试使用没有universal_newlines = True的字节,我已经硬编码了东西等等。在所有场景中,该命令被执行并挂起:

Enter pass phrase: 

My first hunch was that stdin was not the correct file descriptor and that rpmbuild was internally calling a gpg command, and maybe my input isn't piped. But when I do p.stdin.close() I get an OSerror about subprocess trying to write to the closed descriptor.

我的第一个预感是stdin不是正确的文件描述符,并且rpmbuild在内部调用gpg命令,也许我的输入不是管道输入。但是当我执行p.stdin.close()时,我得到一个关于子进程尝试写入封闭描述符的OSerror。

What is the rpmbuild command doing to stdin that prevents me from writing to it?

什么是对stdin执行的rpmbuild命令阻止我写入它?

Is there a hack I can do? I tried echo "my passphrase" | rpmbuild .... as the command but that doesn't work.

我能做什么?我试着回应“我的密码”| rpmbuild ....作为命令,但不起作用。

I know I can do something with gpg like command and sign packages without a passphrase but I kind of want to avoid that.

我知道我可以用gpg做一些事情,比如命令和签名包没有密码,但我有点想避免这种情况。

EDIT:

After some more reading, I realize this is issue is common to commands that require password input, typically using a form of getpass.

经过一些阅读后,我意识到这个问题对需要密码输入的命令很常见,通常使用getpass的形式。

I see a solution would be to use a library like pexpect, but I want something from the standard library. I am going to keep looking, but I think maybe i can try writing to something similar /dev/tty.

我看到一个解决方案是使用像pexpect这样的库,但我想要标准库中的东西。我会继续寻找,但我想也许我可以尝试写一些类似/ dev / tty的东西。

1 个解决方案

#1


0  

rpm uses getpass(3) which reopens /dev/tty.

rpm使用getpass(3)重新打开/ dev / tty。

There are 2 approaches to automating: 1) create a pseudotty 2) (linux) find the reopened file descriptor in /proc

自动化有两种方法:1)创建伪2)(linux)在/ proc中找到重新打开的文件描述符

If scripting, expect(1) has (or had) a short example with pseudotty's that can be used.

如果是脚本,期望(1)有(或者有)一个可以使用伪节的简短示例。

#1


0  

rpm uses getpass(3) which reopens /dev/tty.

rpm使用getpass(3)重新打开/ dev / tty。

There are 2 approaches to automating: 1) create a pseudotty 2) (linux) find the reopened file descriptor in /proc

自动化有两种方法:1)创建伪2)(linux)在/ proc中找到重新打开的文件描述符

If scripting, expect(1) has (or had) a short example with pseudotty's that can be used.

如果是脚本,期望(1)有(或者有)一个可以使用伪节的简短示例。