~landscape/landscape-client/trunk

« back to all changes in this revision

Viewing changes to landscape/monitor/mountinfo.py

  • Committer: Thomas Hervé
  • Date: 2012-03-05 14:11:42 UTC
  • mto: This revision was merged to the branch mainline in revision 473.
  • Revision ID: thomas@canonical.com-20120305141142-zjdee4996a120vt7
Restore dbus and hal

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
    def __init__(self, interval=300, monitor_interval=60 * 60,
17
17
                 mounts_file="/proc/mounts", create_time=time.time,
18
 
                 statvfs=None, mtab_file="/etc/mtab"):
 
18
                 statvfs=None, hal_manager=None, mtab_file="/etc/mtab"):
19
19
        self.run_interval = interval
20
20
        self._monitor_interval = monitor_interval
21
21
        self._create_time = create_time
29
29
        self._mount_info = []
30
30
        self._mount_info_to_persist = None
31
31
        try:
 
32
            from landscape.hal import HALManager
 
33
        except ImportError:
 
34
            self._hal_manager = hal_manager
 
35
        else:
 
36
            self._hal_manager = hal_manager or HALManager()
 
37
        try:
32
38
            from gi.repository import GUdev
33
39
        except ImportError:
34
40
            self._gudev_client = None
116
122
            current_mount_points.add(mount_point)
117
123
 
118
124
    def _get_removable_devices(self):
119
 
        if self._gudev_client is not None:
 
125
        if self._hal_manager is not None:
 
126
            return self._get_hal_removable_devices()
 
127
        elif self._gudev_client is not None:
120
128
            return self._get_udev_removable_devices()
121
129
        else:
122
130
            return set()
130
138
                return False
131
139
        return is_removable()
132
140
 
 
141
    def _get_hal_removable_devices(self):
 
142
        block_devices = {}  # {udi: [device, ...]}
 
143
        children = {}  # {parent_udi: [child_udi, ...]}
 
144
        removable = set()
 
145
 
 
146
        # We walk the list of devices building up a dictionary of all removable
 
147
        # devices, and a mapping of {UDI => [block devices]}
 
148
        # We differentiate between devices that we definitely know are
 
149
        # removable and devices that _may_ be removable, depending on their
 
150
        # parent device, e.g. /dev/sdb1 isn't flagged as removable, but
 
151
        # /dev/sdb may well be removable.
 
152
 
 
153
        # Unfortunately, HAL doesn't guarantee the order of the devices
 
154
        # returned from get_devices(), so we may not know that a parent device
 
155
        # is removable when we find it's first child.
 
156
        devices = self._hal_manager.get_devices()
 
157
        for device in devices:
 
158
            block_device = device.properties.get("block.device")
 
159
            if block_device:
 
160
                if device.properties.get("storage.removable"):
 
161
                    removable.add(device.udi)
 
162
 
 
163
                try:
 
164
                    block_devices[device.udi].append(block_device)
 
165
                except KeyError:
 
166
                    block_devices[device.udi] = [block_device]
 
167
 
 
168
                parent_udi = device.properties.get("info.parent")
 
169
                if parent_udi is not None:
 
170
                    try:
 
171
                        children[parent_udi].append(device.udi)
 
172
                    except KeyError:
 
173
                        children[parent_udi] = [device.udi]
 
174
 
 
175
        # Propagate the removable flag from each node all the way to
 
176
        # its leaf children.
 
177
        updated = True
 
178
        while updated:
 
179
            updated = False
 
180
            for parent_udi in children:
 
181
                if parent_udi in removable:
 
182
                    for child_udi in children[parent_udi]:
 
183
                        if child_udi not in removable:
 
184
                            removable.add(child_udi)
 
185
                            updated = True
 
186
 
 
187
        # We've now seen _all_ devices, and have the definitive list of
 
188
        # removable UDIs, so we can now find all the removable devices in the
 
189
        # system.
 
190
        removable_devices = set()
 
191
        for udi in removable:
 
192
            removable_devices.update(block_devices[udi])
 
193
 
 
194
        return removable_devices
 
195
 
133
196
    def _get_mount_info(self):
134
197
        """Generator yields local mount points worth recording data for."""
135
198
        removable_devices = self._get_removable_devices()