summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Driver/LocalExec.py
diff options
context:
space:
mode:
authorpikusa <pikusa@man.poznan.pl>2013-04-03 13:18:17 (GMT)
committer pikusa <pikusa@man.poznan.pl>2013-04-03 13:18:17 (GMT)
commit2f2a3a129c91de540e66c3bfbe30b0df1942cd4b (patch)
tree2d313cdf0068af368d4de6067d676be16f6a6464 /Monitoring/MonitoringService/Driver/LocalExec.py
parentff8aa232b071a9b54dff833714a870fd0aec0b30 (diff)
downloadnovi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.zip
novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.tar.gz
novi-public-2f2a3a129c91de540e66c3bfbe30b0df1942cd4b.tar.bz2
project commit and dir tree change
Diffstat (limited to 'Monitoring/MonitoringService/Driver/LocalExec.py')
-rw-r--r--Monitoring/MonitoringService/Driver/LocalExec.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/Monitoring/MonitoringService/Driver/LocalExec.py b/Monitoring/MonitoringService/Driver/LocalExec.py
new file mode 100644
index 0000000..2cf4f8f
--- /dev/null
+++ b/Monitoring/MonitoringService/Driver/LocalExec.py
@@ -0,0 +1,56 @@
+'''
+Created on Feb 4, 2013
+
+@author: steger, jozsef
+@organization: ELTE
+@contact: steger@complex.elte.hu
+'''
+
+#TODO: nested command execution is not working properly: e.g.: echo `hostname`
+
+from Driver import Driver
+from subprocess import Popen, PIPE
+
+class LocalExec(Driver):
+ '''
+ @summary: implements a driver to execute local commands
+ @ivar command: the default command
+ @type command: str
+ @ivar p: the process api
+ @type p: subprocess.Popen or None
+ '''
+
+ def __init__(self, command = "echo -n helloworld"):
+ '''
+ @summary: save a default command
+ @param command: the default command
+ @type command: str
+ '''
+ self.command = command
+ self.p = None
+
+ def __del__(self):
+ '''
+ @summary: an implicit deletion of the driver triggers a kill signal on a running process
+ '''
+ if self.p:
+ self.p.kill()
+ self.p = None
+
+ def execute(self, command = None):
+ '''
+ @summary: executes a local command
+ @param command: the command to run, if None, the default command is issued
+ @type command: str or None
+ @return: the standard output of the command run
+ @rtype: str
+ '''
+ if command is None:
+ command = self.command
+ self.p = Popen(args = command.split(' '), stdout = PIPE, stderr = PIPE)
+ stout, sterr = self.p.communicate()
+ self.p = None
+ self.logger.debug("executed '%s'" % (command))
+ if len(sterr):
+ self.logger.warning("execution '%s' failed: %s" % (command, sterr))
+ return stout