From 22f11cfc3f0558c6b093decf01b3e140063576cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Thu, 26 May 2016 17:24:29 +0300 Subject: [PATCH] util fixes --- opt/ahenk/base/util/util.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/opt/ahenk/base/util/util.py b/opt/ahenk/base/util/util.py index 850b1bc..971f997 100644 --- a/opt/ahenk/base/util/util.py +++ b/opt/ahenk/base/util/util.py @@ -115,9 +115,18 @@ class Util: raise @staticmethod - def execute(command): - p = subprocess.Popen(command, shell=True) - return p.wait() + def execute(command, stdin=None, env=None, cwd=None, shell=True): + + try: + process = subprocess.Popen(command, stdin=stdin, env=env, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=shell) + + result_code = process.wait() + p_out = ', '.join([str(x) for x in process.stdout.readlines()]) + p_err = ', '.join([str(x) for x in process.stderr.readlines()]) + + return result_code, p_out, p_err + except Exception as e: + return 1, 'Could not execute command: {0}. Error Message: {1}'.format(command, str(e)), '' @staticmethod def execute_script(script_path, parameters=None): @@ -173,4 +182,6 @@ class Util: gid = st.st_uid return grp.getgrgid(gid)[0] except: - raise \ No newline at end of file + raise + +