Browse Source

updated shellexec with custom exceptions

bugfixes
harshadyeola 10 years ago
parent
commit
cbda23ba17
  1. 34
      ee/core/shellexec.py

34
ee/core/shellexec.py

@ -5,6 +5,11 @@ import sys
import subprocess import subprocess
class CommandExecutionError(Exception):
"""custom Exception for command execution"""
pass
class EEShellExec(): class EEShellExec():
"""Method to run shell commands""" """Method to run shell commands"""
def __init__(): def __init__():
@ -13,8 +18,8 @@ class EEShellExec():
def cmd_exec(self, command, errormsg='', log=True): def cmd_exec(self, command, errormsg='', log=True):
"""Run shell command from Python""" """Run shell command from Python"""
try: try:
if log: log and Log.debug(self, "Running command: {0}".format(command))
Log.debug(self, "Running command: {0}".format(command))
with subprocess.Popen([command], stdout=subprocess.PIPE, with subprocess.Popen([command], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) as proc: stderr=subprocess.PIPE, shell=True) as proc:
(cmd_stdout_bytes, cmd_stderr_bytes) = proc.communicate() (cmd_stdout_bytes, cmd_stderr_bytes) = proc.communicate()
@ -26,25 +31,15 @@ class EEShellExec():
if proc.returncode == 0: if proc.returncode == 0:
return True return True
else: else:
Log.debug(self, "Command Output: {0}, Command Error: {1}" Log.debug(self, "Command Output: {0}, \nCommand Error: {1}"
.format(cmd_stdout, cmd_stderr)) .format(cmd_stdout, cmd_stderr))
return False return False
except OSError as e: except OSError as e:
if errormsg: Log.debug(self, str(e))
Log.error(self, errormsg) raise CommandExecutionError
else:
Log.debug(self, "Unable to execute command {0}"
.format(command))
Log.debug(self, "{0}".format(e))
Log.error(self, "Error occured while executing command")
except Exception as e: except Exception as e:
if errormsg: Log.debug(self, str(e))
Log.error(self, errormsg) raise CommandExecutionError
else:
Log.debug(self, "Unable to execute command {0}"
.format(command))
Log.debug(self, "{0}".format(e))
Log.error(self, "Error occurred while executing command")
def invoke_editor(self, filepath, errormsg=''): def invoke_editor(self, filepath, errormsg=''):
""" """
@ -53,8 +48,5 @@ class EEShellExec():
try: try:
subprocess.call(['sensible-editor', filepath]) subprocess.call(['sensible-editor', filepath])
except OSError as e: except OSError as e:
if errormsg:
Log.error(self, errormsg)
else:
Log.debug(self, "{0}{1}".format(e.errno, e.strerror)) Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
Log.error(self, "Unable to edit file {0}".format(filepath)) raise CommandExecutionError

Loading…
Cancel
Save