2018-11-13 21:55:59 +01:00
|
|
|
"""WordOps Shell Functions"""
|
|
|
|
|
import subprocess
|
|
|
|
|
|
2019-09-04 20:36:15 +02:00
|
|
|
from wo.core.logging import Log
|
|
|
|
|
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
class CommandExecutionError(Exception):
|
|
|
|
|
"""custom Exception for command execution"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WOShellExec():
|
|
|
|
|
"""Method to run shell commands"""
|
|
|
|
|
def __init__():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def cmd_exec(self, command, errormsg='', log=True):
|
|
|
|
|
"""Run shell command from Python"""
|
|
|
|
|
try:
|
|
|
|
|
log and Log.debug(self, "Running command: {0}".format(command))
|
|
|
|
|
|
|
|
|
|
with subprocess.Popen([command], stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.PIPE, shell=True) as proc:
|
|
|
|
|
(cmd_stdout_bytes, cmd_stderr_bytes) = proc.communicate()
|
|
|
|
|
(cmd_stdout, cmd_stderr) = (cmd_stdout_bytes.decode('utf-8',
|
2019-10-12 11:07:25 +02:00
|
|
|
"replace"),
|
2018-11-13 21:55:59 +01:00
|
|
|
cmd_stderr_bytes.decode('utf-8',
|
2019-10-12 11:07:25 +02:00
|
|
|
"replace"))
|
2018-11-13 21:55:59 +01:00
|
|
|
|
2019-10-30 13:05:08 +01:00
|
|
|
Log.debug(self, "Command Output: {0}, \nCommand Error: {1}"
|
|
|
|
|
.format(cmd_stdout, cmd_stderr))
|
|
|
|
|
return bool(proc.returncode == 0)
|
2018-11-13 21:55:59 +01:00
|
|
|
except OSError as e:
|
2019-07-31 20:26:52 +02:00
|
|
|
Log.debug(self, str(e))
|
|
|
|
|
raise CommandExecutionError
|
2018-11-13 21:55:59 +01:00
|
|
|
except Exception as e:
|
2019-07-31 20:26:52 +02:00
|
|
|
Log.debug(self, str(e))
|
|
|
|
|
raise CommandExecutionError
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
def invoke_editor(self, filepath, errormsg=''):
|
|
|
|
|
"""
|
|
|
|
|
Open files using sensible editor
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
subprocess.call(['sensible-editor', filepath])
|
|
|
|
|
except OSError as e:
|
2019-09-02 18:56:34 +02:00
|
|
|
Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
|
|
|
|
|
raise CommandExecutionError
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
def cmd_exec_stdout(self, command, errormsg='', log=True):
|
|
|
|
|
"""Run shell command from Python"""
|
|
|
|
|
try:
|
2019-09-03 21:14:00 +02:00
|
|
|
log and Log.debug(self, "Running command: {0}".format(command))
|
|
|
|
|
with subprocess.Popen([command], stdout=subprocess.PIPE,
|
2018-11-13 21:55:59 +01:00
|
|
|
stderr=subprocess.PIPE, shell=True) as proc:
|
|
|
|
|
(cmd_stdout_bytes, cmd_stderr_bytes) = proc.communicate()
|
|
|
|
|
(cmd_stdout, cmd_stderr) = (cmd_stdout_bytes.decode('utf-8',
|
2019-10-12 11:07:25 +02:00
|
|
|
"replace"),
|
2018-11-13 21:55:59 +01:00
|
|
|
cmd_stderr_bytes.decode('utf-8',
|
2019-10-12 11:07:25 +02:00
|
|
|
"replace"))
|
2018-11-13 21:55:59 +01:00
|
|
|
|
|
|
|
|
if proc.returncode == 0:
|
|
|
|
|
Log.debug(self, "Command Output: {0}, \nCommand Error: {1}"
|
|
|
|
|
.format(cmd_stdout, cmd_stderr))
|
|
|
|
|
return cmd_stdout
|
|
|
|
|
else:
|
|
|
|
|
Log.debug(self, "Command Output: {0}, \nCommand Error: {1}"
|
|
|
|
|
.format(cmd_stdout, cmd_stderr))
|
|
|
|
|
return cmd_stdout
|
|
|
|
|
except OSError as e:
|
2019-09-02 18:56:34 +02:00
|
|
|
Log.debug(self, str(e))
|
|
|
|
|
raise CommandExecutionError
|
2018-11-13 21:55:59 +01:00
|
|
|
except Exception as e:
|
2019-09-02 18:56:34 +02:00
|
|
|
Log.debug(self, str(e))
|
|
|
|
|
raise CommandExecutionError
|