author | terom@fixme.fi |
Mon, 15 Sep 2008 00:53:59 +0300 | |
changeset 23 | 67e71e9170e5 |
parent 21 | aa6df8f9c44a |
child 25 | 6c0a53a512d8 |
permissions | -rw-r--r-- |
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
1 |
# read a stream from a fifo |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
2 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
3 |
from twisted.internet import reactor, interfaces |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
4 |
from twisted.python import log |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
5 |
from zope.interface import implements |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
6 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
7 |
import os, fcntl, errno |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
8 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
9 |
class EOF (Exception) : pass |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
10 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
11 |
BUF_SIZE = 2048 |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
12 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
13 |
class Fifo (object) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
14 |
implements(interfaces.IReadDescriptor) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
15 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
16 |
def __init__ (self, path) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
17 |
self.path = path |
23
67e71e9170e5
rename plugin fixbot -> fixbot_nexus, add fixbot_logwatch plugin, fix some random bugs
terom@fixme.fi
parents:
21
diff
changeset
|
18 |
self.fd = None |
67e71e9170e5
rename plugin fixbot -> fixbot_nexus, add fixbot_logwatch plugin, fix some random bugs
terom@fixme.fi
parents:
21
diff
changeset
|
19 |
|
21
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
20 |
self._open() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
21 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
22 |
def _open (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
23 |
self.fd = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
24 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
25 |
reactor.addReader(self) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
26 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
27 |
log.msg("opened fifo %s as %d" % (self.path, self.fd)) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
28 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
29 |
def _close (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
30 |
if self.fd : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
31 |
reactor.removeReader(self) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
32 |
os.close(self.fd) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
33 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
34 |
log.msg("closed fifo %d at %s" % (self.fd, self.path)) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
35 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
36 |
self.fd = None |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
37 |
close = _close |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
38 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
39 |
def reopen (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
40 |
""" |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
41 |
Close and re-open the fifo. This is useful for handling EOF |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
42 |
""" |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
43 |
self._close() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
44 |
self._open() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
45 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
46 |
def _read (self, length) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
47 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
48 |
log.msg("(read %d bytes from %d:%s)" % (length, self.fd, self.path)) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
49 |
try : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
50 |
data = os.read(self.fd, length) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
51 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
52 |
except OSError, e : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
53 |
if e.errno == errno.EAGAIN : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
54 |
log.msg("\tEAGAIN") |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
55 |
return None |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
56 |
else : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
57 |
log.msg("\tERROR: %s" % e) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
58 |
raise |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
59 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
60 |
if not data : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
61 |
log.msg("\tEOF") |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
62 |
raise EOF() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
63 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
64 |
log.msg("\tDATA: %d: %r" % (len(data), data)) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
65 |
return data |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
66 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
67 |
def fileno (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
68 |
return self.fd |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
69 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
70 |
def doRead (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
71 |
while True : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
72 |
log.msg("fifo doRead loop") |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
73 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
74 |
try : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
75 |
data = self._read(BUF_SIZE) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
76 |
except EOF : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
77 |
self.handleEOF() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
78 |
return |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
79 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
80 |
if data : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
81 |
self.dataReceived(data) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
82 |
else : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
83 |
break |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
84 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
85 |
def dataReceived (self, data) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
86 |
pass |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
87 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
88 |
def handleEOF (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
89 |
pass |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
90 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
91 |
def connectionLost (self, reason) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
92 |
self.close() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
93 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
94 |
def logPrefix (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
95 |
return "FIFO:%d:%s" % (self.fd, self.path) |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
96 |
|
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
97 |
def __del__ (self) : |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
98 |
""" |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
99 |
!!! this is important |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
100 |
""" |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
101 |
self.close() |
aa6df8f9c44a
add initial code back under fixbot/, the git-convert somehow broke
terom@fixme.fi
parents:
diff
changeset
|
102 |