Best way to manage y/n and password prompts through a Python script
I am creating a Python script to install AUR packages but can't figure out how to detect yes/no and root password prompts.
So far what I have managed is to get rid of yes/no prompts using yes
command, like this.
cmd = r'yes y | makepkg -si'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
But still 2 problems persist:
-
Detect the password prompt: The password prompt appears mainly after
makepkg
enters in fakeroot. IDK how to detect that and supply the password from the script. There is no pattern actually. Some package evoke 1 y/n prompt while some prompt more than 1. -
Enter actual password: Actually, I want to enter password through a GUI. So, basically, I want user to input password in a text box that I will create using Tkinter.
tkinter.simpledialog.askstring("Password", "Enter password:", show='*')
I am confused how to implement this. I looked up this problem already and some Stackoverflow posts demonstrate the use of pexpect
but I am having hard time understanding it. But if it can be done using the standsard libraries then it would be great.
Any help is appreacited. Thanks.
Solution 1:
Detect the password prompt: The password prompt appears mainly after makepkg enters in fakeroot. IDK how to detect that and supply the password from the script. There is no pattern actually. Some package evoke 1 y/n prompt while some prompt more than 1.
There is a pattern. An y/n prompt is shown when:
- makepkg invokes pacman to install build dependencies (for the
-s
option); - makepkg invokes pacman to install the freshly built package (for the
-i
option); - makepkg invokes pacman to remove the build dependencies (for the
-r
option). - makepkg invokes pacman to...
In all cases, if makepkg is given the --noconfirm
option, it will pass that to pacman as well.
Enter actual password: Actually, I want to enter password through a GUI. So, basically, I want user to input password in a text box that I will create using Tkinter.
The password prompt, if any, is shown by sudo. Sudo supports running external "askpass" tools through the SUDO_ASKPASS
environment variable.
Write or install a program that shows the password prompt and outputs the password to stdout (e.g. ssh-askpass or zenity would do), then set the SUDO_ASKPASS
environment variable to the program's path:
export SUDO_ASKPASS=/usr/local/bin/ssh-askpass
Finally, edit ~/.config/pacman/makepkg.conf
to make makepkg run sudo -A
for elevation, activating the "graphical askpass" feature:
PACMAN_AUTH=(sudo -A)