使用子进程来执行终端命令序列

时间:2017-05-22 01:45:24

标签: python terminal subprocess

我正在尝试制作我的第一个程序,即使用GUI输入自动执行git push / clone进程。

    """GUI GIT Program"""
#Import Statements
from tkinter import *
from tkinter import simpledialog
from tkinter import messagebox
import subprocess
from time import sleep

# set up the GUI
root = Tk()
w = Label(root, text="Git Handler")
w.pack()

# Welcome the User
messagebox.showinfo("Welcome","This is a program to automate your Git stuff!")

# solicit input
user_name = simpledialog.askstring("Username:","What is your username?")
password = simpledialog.askstring("Password","What is your password?",show="*")
message_for_push = simpledialog.askstring("Push Message","What's your push message?")

# do stuff with the data
# call(["git","push"])
# sleep (2)
# call([user_name])
# sleep (2)
# call([password])
commands = '''
git push'''
user_name
password


process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = process.communicate(commands.encode('utf-8'))
print(out.decode('utf-8'))

我遇到的问题是执行git push命令,但是下一步输入用户名不是,以及所有后续命令......任何想法?

1 个答案:

答案 0 :(得分:0)

我很久以前遇到过类似的问题,并且可以使用下面的代码片段来解决这个问题。

git-clone () {
    print "Bitbucket checkout enter bitbucket user/pass."
    echo -n "Bitbucket username:"
    read bit_user
    #echo "Bitbucket pass:"
    read -s -p "Bitbucket Password:" bit_pass
    su -c "cd /home/latlongo; git clone https://$bit_user:$bit_pass@bitbucket.org/xyz.git -b your_branch" -m $1
}