1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
from Plugins.Plugin import PluginDescriptor
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
from Components.Harddisk import harddiskmanager
hotplugNotifier = [ ]
class Hotplug(Protocol):
def connectionMade(self):
self.received = ""
def dataReceived(self, data):
self.received += data
def connectionLost(self, reason):
data = self.received.split('\0')[:-1]
v = {}
for x in data:
i = x.find('=')
var, val = x[:i], x[i+1:]
v[var] = val
print "hotplug:", v
action = v.get("ACTION")
device = v.get("DEVPATH")
physdevpath = v.get("PHYSDEVPATH")
media_state = v.get("X_E2_MEDIA_STATUS")
dev = device.split('/')[-1]
if action is not None and action == "add":
harddiskmanager.addHotplugPartition(dev, physdevpath)
elif action is not None and action == "remove":
harddiskmanager.removeHotplugPartition(dev)
elif media_state is not None:
if media_state == '1':
harddiskmanager.removeHotplugPartition(dev)
harddiskmanager.addHotplugPartition(dev, physdevpath)
elif media_state == '0':
harddiskmanager.removeHotplugPartition(dev)
for callback in hotplugNotifier:
try:
callback(dev, action or media_state)
except AttributeError:
hotplugNotifier.remove(callback)
def autostart(reason, **kwargs):
if reason == 0:
print "starting hotplug handler"
factory = Factory()
factory.protocol = Hotplug
try:
import os
os.remove("/tmp/hotplug.socket")
except OSError:
pass
reactor.listenUNIX("/tmp/hotplug.socket", factory)
def Plugins(**kwargs):
return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)
|