Python 3.x 如何从python脚本安装home brew?
环境 Python 3.7 MacOSCatalina10.15 问题 这让我发疯了 我需要从python脚本安装home brew。 我尝试了不同的代码和命令 如果我以管理员身份运行它,它会说“不要以root身份运行它!” 如果我以非管理员身份运行,它会显示“需要macOS上的sudo访问权限” 这是我的Python脚本:Python 3.x 如何从python脚本安装home brew?,python-3.x,macos,homebrew,Python 3.x,Macos,Homebrew,环境 Python 3.7 MacOSCatalina10.15 问题 这让我发疯了 我需要从python脚本安装home brew。 我尝试了不同的代码和命令 如果我以管理员身份运行它,它会说“不要以root身份运行它!” 如果我以非管理员身份运行,它会显示“需要macOS上的sudo访问权限” 这是我的Python脚本: def InstallHomeBrew(p_command): proc = subprocess.Popen(p_command, shell=True, std
def InstallHomeBrew(p_command):
proc = subprocess.Popen(p_command, shell=True, stdout=True)
(output, err) = proc.communicate()
returncode_brew = proc.wait()
print(f"returncode_brew : {returncode_brew}")
if returncode_brew == 0:
print("Homebrew installation was fine")
print(f"Here is the succesfull command: {p_command}")
return True
else:
print("Homebrew installation was not fine!")
return False
# === Get actual username ====================
proc = subprocess.Popen("dscacheutil -q user | grep -A 3 -B 2 -e uid:\ 5'[0-9][0-9]'", shell=True,
stdout=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.STDOUT, close_fds=True)
returncode_get_username = proc.wait()
username = proc.stdout.readlines()
# username = username.decode('utf-8')
for line in username:
if isinstance(line, bytes):
line = line.decode()
if line.find("name") != -1:
mac_account = line.replace('name: ', '').replace('\n', '')
# ====================================================================
# Here below the list of different commands my function will try for installing homebrew
command_install_homebrew_1 = f'/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"'
command_install_homebrew_2 = f'sudo -u {mac_account} /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"'
command_install_homebrew_3=f"""/usr/bin/osascript -e 'do shell script "{os.path.join(os.path.dirname(sys.argv[0]),'PhoneBot_Install.sh')} args 2>&1 etc" with administrator privileges'"""
command_install_homebrew_4 = f'sudo -S /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"'
command_install_homebrew_5=f"""/usr/bin/osascript -e 'do shell script "{os.path.join(os.path.dirname(sys.argv[0]),'PhoneBot_Install_as_Admin.sh')} args 2>&1 etc" with administrator privileges'"""
command_install_homebrew_6 = f'sudo /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"'
result_1 = InstallHomeBrew(command_install_homebrew_1)
if not result_1:
result_2 = InstallHomeBrew(command_install_homebrew_2)
if not result_2:
result_3 = InstallHomeBrew(command_install_homebrew_3)
if not result_3:
result_4 = InstallHomeBrew(command_install_homebrew_4)
if not result_4:
result_5 = InstallHomeBrew(command_install_homebrew_5)
if not result_5:
result_6 = InstallHomeBrew(command_install_homebrew_6)
这是bash脚本的源代码:
#Install.sh
sudo -S /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
#Install_as_Admin.sh
sudo /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
这是输出:
Need sudo access on macOS (e.g. the user gauthierbtz to be an Administrator)!
returncode_brew : 1
Homebrew installation was not fine!
Need sudo access on macOS (e.g. the user gauthierbtz to be an Administrator)!
returncode_brew : 1
Homebrew installation was not fine!
Don't run this as root! (1)
returncode_brew : 1
Homebrew installation was not fine!
Password:mypassword123
Don't run this as root!
returncode_brew : 1
Homebrew installation was not fine!
Don't run this as root! (1)
returncode_brew : 1
Homebrew installation was not fine!
Don't run this as root!
returncode_brew : 1
Homebrew installation was not fine!
Process finished with exit code 0
那么如何从python脚本安装自制软件呢?