Die Bash-Shell von Linux hat einen eingebauten Befehl namens expect. Dieser wird verwendet, um Befehlszeilenarbeit zu automatisieren, indem interaktive Shells wie Passworteingabe-Prompts automatisch eingegeben werden. Als Python-Wrapper für dieses expect gibt es das pexpect-Paket.
Installieren Sie pexpect mit dem Folgenden:
pip install pexpect
Als Beispiel für die Verwendung von pexpect zeige ich die Schritte bis zum Push zu Github und zur automatischen Eingabe des Passwort-Prompts mit pexpect.
Zunächst, um einfache Befehle wie git add . in Python auszuführen, tun Sie Folgendes:
Das Folgende ist ein Beispiel mit Python 3.6.7. Wenn die Python-Version unterschiedlich ist, können Inkompatibilitäten um shell=True Fehler verursachen.
Um mehrere Python-Versionen zu installieren und zwischen ihnen zu wechseln, wird empfohlen, ein Versionsverwaltungssystem wie pyenv zu verwenden. pyenv wird in anderen Artikeln erklärt. Bitte verwenden Sie die Suchfunktion in diesem Blog.
# Python 3.6.7
import pexpect
import subprocess
import os
cmd = subprocess.run("git add .", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if ( cmd.stdout ) : print( cmd.stdout.decode('utf-8') )
if ( cmd.stderr ) : print( cmd.stderr.decode('utf-8') )
Fahren Sie fort mit git commit.
cmd = subprocess.run("git status", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if ( cmd.stdout ) : print( cmd.stdout.decode('utf-8') )
if ( cmd.stderr ) : print( cmd.stderr.decode('utf-8') )
cmd = subprocess.run("git commit -m .", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if ( cmd.stdout ) : print( cmd.stdout.decode('utf-8') )
if ( cmd.stderr ) : print( cmd.stderr.decode('utf-8') )
Das Folgende ist ein spezifisches Beispiel für die Verwendung von pexpect. Es gibt automatisch die Benutzernamen- und Passwort-Prompts ein. Sie müssen Umgebungsvariablen für Benutzernamen und Passwort jeweils setzen.
child = pexpect.spawn('git push -u origin gh-pages')
child.expect('Username .*')
child.sendline(os.environ['GITHUB_USERNAME'])
child.expect('Password .*')
child.sendline(os.environ['GITHUB_PASSWORD'])
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
print( cmd_show_data.decode() )
Das gesamte Skript sieht so aus:
# Python 3.6.7
import pexpect
import subprocess
import os
cmd = subprocess.run("git add .", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if ( cmd.stdout ) : print( cmd.stdout.decode('utf-8') )
if ( cmd.stderr ) : print( cmd.stderr.decode('utf-8') )
cmd = subprocess.run("git status", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if ( cmd.stdout ) : print( cmd.stdout.decode('utf-8') )
if ( cmd.stderr ) : print( cmd.stderr.decode('utf-8') )
cmd = subprocess.run("git commit -m .", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if ( cmd.stdout ) : print( cmd.stdout.decode('utf-8') )
if ( cmd.stderr ) : print( cmd.stderr.decode('utf-8') )
child = pexpect.spawn('git push -u origin gh-pages')
child.expect('Username .*')
child.sendline(os.environ['GITHUB_USERNAME'])
child.expect('Password .*')
child.sendline(os.environ['GITHUB_PASSWORD'])
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
print( cmd_show_data.decode() )