From 7872a839d8d9743e6779bb33f1acd4f50622d71b Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Thu, 22 Oct 2020 18:02:52 +0200 Subject: [PATCH] Add command execution using subprocess --- src/util/command.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/util/command.py diff --git a/src/util/command.py b/src/util/command.py new file mode 100644 index 0000000..2aa260b --- /dev/null +++ b/src/util/command.py @@ -0,0 +1,27 @@ +import subprocess + + +def execute(command) -> None: + """Execute bash `command`, return the returncode (int)""" + command = command.split(' ') + subprocess.call(command) + + +def get_output(command) -> str: + """Get standard output of `command`""" + command = command.split(' ') + return subprocess.run( + command, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE + ).stdout.decode('utf-8') + + +def get_return_code(command) -> int: + """get return code of `command` (int)""" + command = command.split(' ') + return subprocess.run( + command, + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE + ).returncode