1 from Plugins.Plugin import PluginDescriptor
2 from twisted.internet.protocol import Protocol, Factory
3 from twisted.internet import reactor
4 from Components.Harddisk import harddiskmanager
8 class Hotplug(Protocol):
9 def connectionMade(self):
12 def dataReceived(self, data):
15 def connectionLost(self, reason):
16 data = self.received.split('\0')[:-1]
22 var, val = x[:i], x[i+1:]
27 action = v.get("ACTION")
28 device = v.get("DEVPATH")
29 physdevpath = v.get("PHYSDEVPATH")
30 media_state = v.get("X_E2_MEDIA_STATUS")
32 dev = device.split('/')[-1]
34 if action is not None and action == "add":
35 harddiskmanager.addHotplugPartition(dev, physdevpath)
36 elif action is not None and action == "remove":
37 harddiskmanager.removeHotplugPartition(dev)
38 elif media_state is not None:
39 if media_state == '1':
40 harddiskmanager.removeHotplugPartition(dev)
41 harddiskmanager.addHotplugPartition(dev, physdevpath)
42 elif media_state == '0':
43 harddiskmanager.removeHotplugPartition(dev)
45 for callback in hotplugNotifier:
47 callback(dev, action or media_state)
48 except AttributeError:
49 hotplugNotifier.remove(callback)
51 def autostart(reason, **kwargs):
53 print "starting hotplug handler"
55 factory.protocol = Hotplug
59 os.remove("/tmp/hotplug.socket")
63 reactor.listenUNIX("/tmp/hotplug.socket", factory)
65 def Plugins(**kwargs):
66 return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)