]> git.ipfire.org Git - oddments/cappie.git/blame - cappie/events.py
Add shortcut to add events from Event class.
[oddments/cappie.git] / cappie / events.py
CommitLineData
53478050
MT
1#!/usr/bin/python
2###############################################################################
3# #
4# Cappie #
5# Copyright (C) 2010 Michael Tremer #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22import os
23import subprocess
24import time
25
26from errors import *
27
28class Event(object):
29 def __init__(self, interface):
30 self.cappie = interface.cappie
31 self.interface = interface
32 self.log = interface.log
33
34 def __str__(self):
35 return self.__class__.__name__
36
be750261
MT
37 def addEvent(self, event):
38 return self.cappie.queue.add(event)
39
53478050
MT
40 def run(self):
41 raise NotImplementedError
42
43
44class EventShell(Event):
45 heartbeat = 0.1
46 timeout = 10
47
48 def __init__(self, interface, script):
49 Event.__init__(self, interface)
50
51 self.script = script
52
53 def run(self):
54 args = " ".join([self.script, self.interface.dev])
55
56 start = time.time()
57 self.log.debug("Running: %s" % args)
58
59 p = subprocess.Popen(args,
60 close_fds=True,
61 shell=True,
62 stdin=open("/dev/null", "r"),
63 stdout=subprocess.PIPE,
64 stderr=subprocess.STDOUT)
65
66 while p.poll() is None:
67 time.sleep(self.heartbeat)
68 if (time.time() - start) > self.timeout:
69 try:
70 os.killpg(p.pid, 9)
71 except OSError:
72 pass
73 raise EventTimeout, "Script took too long to return"
74
75 for line in p.stdout.read().splitlines():
76 if not line: continue
77 self.log.debug(" %s" % line)
78
79 self.cappie.log.debug("Child process returned with exit code: %s" % \
80 p.returncode)
81
82 return p.returncode