1
0
mirror of https://github.com/Foltik/dotfiles synced 2025-01-07 08:42:47 -05:00

Change exec to take sudo as a parameter

This commit is contained in:
Jack Foltz 2018-12-09 00:45:33 -05:00
parent e1647db4b8
commit 1d42aab37e
Signed by: foltik
GPG Key ID: D1F0331758D1F29A
2 changed files with 6 additions and 13 deletions

View File

@ -11,5 +11,5 @@ def install_all(packages):
def pacman(flags, sudo = False):
cmd = ['pacman', '--noconfirm'] + flags
subproc = proc.sudo(cmd) if sudo else proc.exec(cmd)
subproc = proc.exec(cmd, sudo)
return proc.communicate(subproc, cmd)

View File

@ -1,23 +1,16 @@
import subprocess
def sudo(cmd):
#print(['sudo'] + cmd)
return subprocess.Popen(['sudo'] + cmd,
stderr = subprocess.PIPE,
stdout = subprocess.PIPE,
stdin = subprocess.PIPE)
def exec(cmd):
def exec(cmd, sudo = False):
#print(cmd)
return subprocess.Popen(cmd,
return subprocess.Popen(['sudo'] + cmd if sudo else cmd,
stderr = subprocess.PIPE,
stdout = subprocess.PIPE,
stdin = subprocess.PIPE)
def communicate(proc, cmd):
def communicate(proc, cmd, success_retvals = [0]):
data = proc.communicate()
if proc.returncode != 0:
raise Exception('Command failed: ' + data[1].decode())
if proc.returncode not in success_retvals:
raise Exception('Command failed: "' + ' '.join(cmd) + '"' + data[1].decode())
return data[0].decode()