Viewing file: HalCD.py (5.48 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
# Copyright 2007 Red Hat, Inc. # # Jeremy Katz <katzj@redhat.com> # # 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; version 2 only # # 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, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os, sys
import dbus import dbus.glib
HALSERVICE = "org.freedesktop.Hal"
class HALManager: def __init__(self): try: self.bus = dbus.SystemBus() self.halobj = self.bus.get_object(HALSERVICE, '/org/freedesktop/Hal/Manager') self.hal = dbus.Interface(self.halobj, 'org.freedesktop.Hal.Manager') except: self.bus = None self.halobj = None self.hal = None
def GetDeviceByUDI(self, udi): if not self.bus: return None o = self.bus.get_object(HALSERVICE, udi) d = dbus.Interface(o, 'org.freedesktop.Hal.Device')
return HALDevice(d, o, self.bus)
def GetBlockDeviceByUDI(self, udi): if not self.bus: return None d = self.GetDeviceByUDI(udi) return HALBlockDevice(self, d)
def GetDevicesByStringMatch(self, property, thestr): if self.hal is None: return [] ret = [] try: udis = self.hal.FindDeviceStringMatch(property, thestr) except dbus.DBusException: udis = [] for udi in udis: ret.append(self.GetDeviceByUDI(udi)) return ret
def FindDeviceByCapability(self, cap): if self.hal is None: return [] try: return self.hal.FindDeviceByCapability('storage.cdrom') except dbus.DBusException: return []
def __getattr__(self, attr): if self.hal is None: return [] try: return getattr(self.hal, attr) except dbus.DBusException: return [] class HALDevice: def __init__(self, device, object, bus): self.device = device self.object = object self.bus = bus
def GetInterface(self, intf): i = dbus.Interface(self.object, intf) return HALDevice(i, self.object, self.bus)
def __getitem__(self, item): return self.device.GetProperty(item)
def __getattr__(self, attr): return getattr(self.device, attr)
class HALBlockDevice: def __init__(self, halmgr, haldev): if not haldev["block.device"]: raise RuntimeError, "not a block device!" self.haldev = haldev
self.volumedev = None self.storagedev = None for x in halmgr.GetDevicesByStringMatch("block.device", haldev["block.device"]): if x.QueryCapability("volume"): self.volumedev = x self.volume = x.GetInterface("org.freedesktop.Hal.Device.Volume") elif x.QueryCapability("storage"): self.storagedev = x self.storage = x.GetInterface("org.freedesktop.Hal.Device.Storage") def mount(self): if not self.volume: raise RuntimeError, "No volume to mount!" return self.volume.Mount("", "", "")
def umount(self): if not self.volume: raise RuntimeError, "No volume to mount!" return self.volume.Unmount("")
def eject(self): if not self.storage: raise RuntimeError, "No device to eject!" return self.storage.Eject("")
def lock(self, reason): return self.haldev.Lock(reason)
def unlock(self): return self.haldev.Unlock()
def __getitem__(self, item): if item.startswith("volume."): return self.volumedev.GetProperty(item) elif item.startswith("storage."): return self.storagedev.GetProperty(item) return self.haldev.GetProperty(item)
if __name__ == "__main__": hal = HALManager() udis = hal.FindDeviceByCapability('storage.cdrom') for u in udis: d = hal.GetDeviceByUDI(u) print d.GetProperty('info.udi'), d['info.udi']
print "------------------------------------------"
blk = HALBlockDevice(hal, d) import pdb; pdb.set_trace()
sys.exit(0)
for x in hal.GetDevicesByStringMatch("block.device", d["block.device"]): print x, x.GetProperty("info.category") if x["block.is_volume"]: v = x.GetInterface("org.freedesktop.Hal.Device.Volume") # print "volume:", v print "interfaces: ", x["info.interfaces"] v.Mount("", "", "")
if not x["volume.is_mounted"]: continue
print "contents of mountpoint: ", print os.listdir(x["volume.mount_point"])
v.Unmount("") else: # print "not a volume" print "interfaces:" , x["info.interfaces"]
print "------------------"
|