gpt4 book ai didi

python - 如何以及在何处通过基于 macOS Python 的应用程序上的 native GUI 最好地检索 sudo 密码 - (同时维护交互式输出流(stdout))

转载 作者:行者123 更新时间:2023-12-05 06:36:20 25 4
gpt4 key购买 nike

好的,情况是这样的:我正在使用 Python 和 wx (wxphoenix) 构建一个 macOS GUI 应用程序。用户可以使用 GUI(例如:script1)启动文件删除过程(包含在 script2 中)。为了成功运行 script2 需要以 sudo 权限运行。

script2 将遍历一长串文件并将其删除。但我需要它在每一轮之后与 script1 中包含的 GUI 进行通信,以便 script1 可以更新进度条。

在绝对最基本的形式中,我的当前工作设置如下所示:

脚本 1:

import io
from threading import Thread
import subprocess

import wx

# a whole lot of wx GUI stuff

def get_password():
"""Retrieve user password via a GUI"""

# A wx solution using wx.PasswordEntryDialog()
# Store password in a variable

return variable

class run_script_with_sudo(Thread):
"""Launch a script with administrator privileges"""

def __init__(self, path_to_script, wx_pubsub_sendmessage):
"""Set variables to self"""
self.path = path_to_script
self.sender = wx_pubsub_sendmessage
self.password = get_password()
Thread.__init__(self)
self.start()

def run(self):
"""Run thread"""

prepare_script = subprocess.Popen(["echo", password], stdout=subprocess.PIPE)
prepare_script.wait()
launch_script = subprocess.Popen(['sudo', '-S', '/usr/local/bin/python3.6', '-u', self.path], stdin=prepare_script.stdout, stdout=subprocess.PIPE)
for line in io.TextIOWrapper(launch_script.stdout, encoding="utf-8"):
print("Received line: ", line.rstrip())
# Tell progressbar to add another step:
wx.CallAfter(self.sender, "update", msg="")

脚本 2:

import time

# This is a test setup, just a very simple loop that produces an output.

for i in range(25):
time.sleep(1)
print(i)

上述设置的工作原理是 script1 实时接收 script2 的输出并对其进行操作。 (所以在给定的示例中:script1 每秒向进度条添加一个步骤,直到它达到 25 个步骤)。

我想要实现的目标 = 不将密码存储在变量中并使用 macOS 它的 native GUI 来检索密码。

但是当我改变时:

prepare_script = subprocess.Popen(["echo", password], stdout=subprocess.PIPE)
prepare_script.wait()
launch_script = subprocess.Popen(['sudo', '-S', '/usr/local/bin/python3.6', '-u', self.path], stdin=prepare_script.stdout, stdout=subprocess.PIPE)
for line in io.TextIOWrapper(launch_script.stdout, encoding="utf-8"):
print("Received line: ", line.rstrip())
# Tell progressbar to add another step:
wx.CallAfter(self.sender, "update", msg="")

进入:

command = r"""/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 -u """ + self.path + """ with prompt "Sart Deletion Process " with administrator privileges'"""
command_list = shlex.split(command)

launch_script = subprocess.Popen(command_list, stdout=subprocess.PIPE)
for line in io.TextIOWrapper(launch_script.stdout, encoding="utf-8"):
print("Received line: ", line.rstrip())
# Tell progressbar to add another step:
wx.CallAfter(self.sender, "update", msg="")

它停止工作因为 osascript 显然 runs in a non-interactive shell .这意味着 script2 在完全完成之前不会发送任何输出,从而导致 script1 中的进度条停止。

我的问题因此变成了:我怎样才能确保使用 macOS native GUI 来询问 sudo 密码,从而避免必须将其存储在变量中,同时仍然保持捕获的可能性来自交互式/实时流中特权脚本的标准输出。

希望这是有道理的。

非常感谢任何见解!

最佳答案

My question thus becomes: How can I make sure to use macOS native GUI to ask for the sudo password, thus preventing having to store it in a variable, while still maintaining the possibility to catch the stdout from the privileged script in an interactive / real-time stream.

我自己找到了一个解决方案,使用命名管道 (os.mkfifo())。

这样,您可以让 2 个 python 脚本相互通信,同时其中 1 个通过 osascript 以特权权限启动(意思是:您获得一个 native GUI 窗口,要求用户输入 sudo 密码)。

工作解决方案:

mainscript.py

import os
from pathlib import Path
import shlex
import subprocess
import sys
from threading import Thread
import time

class LaunchDeletionProcess(Thread):

def __init__(self):

Thread.__init__(self)

def run(self):

launch_command = r"""/usr/bin/osascript -e 'do shell script "/usr/local/bin/python3.6 -u /path/to/priviliged_script.py" with prompt "Sart Deletion Process " with administrator privileges'"""
split_command = shlex.split(launch_command)

print("Thread 1 started")
testprogram = subprocess.Popen(split_command)
testprogram.wait()
print("Thread1 Finished")

class ReadStatus(Thread):

def __init__(self):

Thread.__init__(self)

def run(self):

while not os.path.exists(os.path.expanduser("~/p1")):
time.sleep(0.1)

print("Thread 2 started")

self.wfPath = os.path.expanduser("~/p1")

rp = open(self.wfPath, 'r')
response = rp.read()

self.try_pipe(response)

def try_pipe(self, response):
rp = open(self.wfPath, 'r')
response = rp.read()
print("Receiving response: ", response)
rp.close()
if response == str(self.nr_of_steps-1):
print("Got to end")
os.remove(os.path.expanduser("~/p1"))
else:
time.sleep(1)
self.try_pipe(response)

if __name__ == "__main__":

thread1 = LaunchDeletionProcess()
thread2 = ReadStatus()
thread1.start()
thread2.start()

priviliged_script.py

import os
import time
import random

wfPath = os.path.expanduser("~/p1")

try:

os.mkfifo(wfPath)

except OSError:

print("error")
pass

result = 10

nr = 0

while nr < result:

random_nr = random.random()

wp = open(wfPath, 'w')
print("writing new number: ", random_nr)
wp.write("Number: " + str(random_nr))
wp.close()

time.sleep(1)
nr += 1

wp = open(wfPath, 'w')
wp.write("end")
wp.close()

关于python - 如何以及在何处通过基于 macOS Python 的应用程序上的 native GUI 最好地检索 sudo 密码 - (同时维护交互式输出流(stdout)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com