2017-10-31 09:20:35 +01:00
|
|
|
import subprocess
|
|
|
|
|
2017-12-13 04:21:26 +01:00
|
|
|
from platypush.message.response import Response
|
2017-12-13 03:37:28 +01:00
|
|
|
|
2017-10-31 09:20:35 +01:00
|
|
|
from .. import Plugin
|
|
|
|
|
|
|
|
class ShellPlugin(Plugin):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Plugin to run custom shell commands.
|
|
|
|
"""
|
|
|
|
|
2017-11-04 12:28:15 +01:00
|
|
|
def exec(self, cmd):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Execute a command.
|
|
|
|
|
|
|
|
:param cmd: Command to execute
|
|
|
|
:type cmd: str
|
|
|
|
|
|
|
|
:returns: A response object where the ``output`` field will contain the command output as a string, and the ``errors`` field will contain whatever was sent to stderr.
|
|
|
|
"""
|
|
|
|
|
2017-10-31 09:20:35 +01:00
|
|
|
output = None
|
2017-12-20 20:25:08 +01:00
|
|
|
errors = []
|
2017-10-31 09:20:35 +01:00
|
|
|
|
|
|
|
try:
|
2017-12-13 03:37:28 +01:00
|
|
|
output = subprocess.check_output(
|
|
|
|
cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
|
2017-10-31 09:20:35 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
2017-12-20 20:25:08 +01:00
|
|
|
errors = [e.output.decode('utf-8')]
|
2017-10-31 09:20:35 +01:00
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
return Response(output=output, errors=errors)
|
2017-10-31 09:20:35 +01:00
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|