]> git.ipfire.org Git - oddments/cappie.git/blobdiff - cappie/events.py
Splitted into daemon and python module.
[oddments/cappie.git] / cappie / events.py
diff --git a/cappie/events.py b/cappie/events.py
new file mode 100644 (file)
index 0000000..d403b52
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/python
+###############################################################################
+#                                                                             #
+# Cappie                                                                      #
+# Copyright (C) 2010 Michael Tremer                                           #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+import os
+import subprocess
+import time
+
+from errors import *
+
+class Event(object):
+       def __init__(self, interface):
+               self.cappie = interface.cappie
+               self.interface = interface
+               self.log = interface.log
+
+       def __str__(self):
+               return self.__class__.__name__
+
+       def run(self):
+               raise NotImplementedError
+
+
+class EventShell(Event):
+       heartbeat = 0.1
+       timeout = 10
+
+       def __init__(self, interface, script):
+               Event.__init__(self, interface)
+
+               self.script = script
+
+       def run(self):
+               args = " ".join([self.script, self.interface.dev])
+
+               start = time.time()
+               self.log.debug("Running: %s" % args)
+
+               p = subprocess.Popen(args,
+                       close_fds=True,
+                       shell=True,
+                       stdin=open("/dev/null", "r"),
+                       stdout=subprocess.PIPE,
+                       stderr=subprocess.STDOUT)
+
+               while p.poll() is None:
+                       time.sleep(self.heartbeat)
+                       if (time.time() - start) > self.timeout:
+                               try:
+                                       os.killpg(p.pid, 9)
+                               except OSError:
+                                       pass
+                               raise EventTimeout, "Script took too long to return"
+
+               for line in p.stdout.read().splitlines():
+                       if not line: continue
+                       self.log.debug("  %s" % line)
+
+               self.cappie.log.debug("Child process returned with exit code: %s" % \
+                       p.returncode)
+
+               return p.returncode