From 90a2adcbd36012b7374865b0dc2e64e21c9443fe Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Sat, 8 Dec 2018 23:18:25 -0500 Subject: [PATCH] Add standard interfaces for called programs --- lib/exec.py | 17 +++++++++++++++++ lib/git.py | 9 +++++++++ lib/proc.py | 23 +++++++++++++++++++++++ lib/yay.py | 12 ++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 lib/exec.py create mode 100644 lib/git.py create mode 100644 lib/proc.py create mode 100644 lib/yay.py diff --git a/lib/exec.py b/lib/exec.py new file mode 100644 index 0000000..b9ba16e --- /dev/null +++ b/lib/exec.py @@ -0,0 +1,17 @@ +import os +import pwd + +def current_user(): + return pwd.getpwuid(os.getuid()).pw_name + +def sudo(cmd): + return subprocess.Popen(['sudo'] + cmd, shell=True, + stderr = subprocess.PIPE, + stdout = subprocess.PIPE, + stdin = subprocess.PIPE) + +def exec(cmd): + return subprocess.Popen(['sudo'] + cmd, shell=True, + stderr = subprocess.PIPE, + stdout = subprocess.PIPE, + stdin = subprocess.PIPE) diff --git a/lib/git.py b/lib/git.py new file mode 100644 index 0000000..0ebbce9 --- /dev/null +++ b/lib/git.py @@ -0,0 +1,9 @@ +import lib.proc as proc + +def clone(url, dest): + output = git(['clone', url, dest]) + +def git(flags): + cmd = ['git'] + flags + subproc = proc.exec(cmd) + return proc.communicate(subproc, cmd) diff --git a/lib/proc.py b/lib/proc.py new file mode 100644 index 0000000..04495be --- /dev/null +++ b/lib/proc.py @@ -0,0 +1,23 @@ +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): + #print(cmd) + return subprocess.Popen(cmd, + stderr = subprocess.PIPE, + stdout = subprocess.PIPE, + stdin = subprocess.PIPE) + +def communicate(proc, cmd): + data = proc.communicate() + + if proc.returncode != 0: + raise Exception('Command failed: ' + data[1].decode()) + + return data[0].decode() diff --git a/lib/yay.py b/lib/yay.py new file mode 100644 index 0000000..9a790e9 --- /dev/null +++ b/lib/yay.py @@ -0,0 +1,12 @@ +import lib.proc as proc + +def install(package): + output = yay(['-S', package], True) + +def install_all(packages): + output = yay(['-S'] + packages, True) + +def yay(flags, sudo = False): + cmd = ['yay', '--noconfirm'] + flags + subproc = proc.sudo(cmd) if sudo else proc.exec(cmd) + return proc.communicate(subproc, cmd)