/usr/share/pyshared/smart/media.py is in python-smartpm 1.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | #
# Copyright (c) 2005 Canonical
# Copyright (c) 2004 Conectiva, Inc.
#
# Written by Gustavo Niemeyer <niemeyer@conectiva.com>
#
# This file is part of Smart Package Manager.
#
# Smart Package Manager 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; either version 2 of the License, or (at
# your option) any later version.
#
# Smart Package Manager 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 Smart Package Manager; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
from smart.util.filetools import compareFiles
from smart import *
import commands
import stat
import os
try:
import dbus
except ImportError:
dbus = None
class MediaSet(object):
def __init__(self):
self._medias = []
self._processcache = {}
self.discover()
def discover(self):
self.restoreState()
del self._medias[:]
self._processcache.clear()
mountpoints = {}
for lst in hooks.call("discover-medias"):
for media in lst:
mountpoint = media.getMountPoint()
if mountpoint not in mountpoints:
mountpoints[mountpoint] = media
self._medias.append(media)
self._medias.sort()
def resetState(self):
for media in self._medias:
media.resetState()
def restoreState(self):
for media in self._medias:
media.restoreState()
def mountAll(self):
for media in self._medias:
media.mount()
def umountAll(self):
for media in self._medias:
media.umount()
def findMountPoint(self, path, subpath=False):
path = os.path.normpath(path)
for media in self._medias:
mountpoint = media.getMountPoint()
if (mountpoint == path or
subpath and path.startswith(mountpoint+"/")):
return media
return None
def findDevice(self, path, subpath=False):
path = os.path.normpath(path)
for media in self._medias:
device = media.getDevice()
if device and \
(device == path or subpath and path.startswith(device+"/")):
return media
return None
def findFile(self, path, comparepath=None):
if path.startswith("localmedia:"):
path = path[11:]
while path[:2] == "//":
path = path[1:]
for media in self._medias:
if media.isMounted():
filepath = media.joinPath(path)
if (os.path.isfile(filepath) and
not comparepath or compareFiles(filepath, comparepath)):
return media
return None
def processFilePath(self, filepath):
dirname = os.path.dirname(filepath)
if dirname in self._processcache:
media = self._processcache.get(dirname)
if media:
filepath = media.convertDevicePath(filepath)
else:
media = self.findMountPoint(filepath, subpath=True)
if not media:
media = self.findDevice(filepath, subpath=True)
if media:
media.mount()
filepath = media.convertDevicePath(filepath)
self._processcache[dirname] = media
else:
isfile = os.path.isfile
paths = []
path = dirname
while path != "/":
paths.append(path)
if isfile(path):
for media in hooks.call("discover-device-media", path):
if media:
media.mount()
self._medias.append(media)
filepath = media.convertDevicePath(filepath)
self._processcache.update(
dict.fromkeys(paths, media))
break
if media:
break
path = os.path.dirname(path)
else:
self._processcache.update(dict.fromkeys(paths, None))
return filepath, media
def getDefault(self):
default = sysconf.get("default-localmedia")
if default:
return self.findMountPoint(default, subpath=True)
return None
def __iter__(self):
return iter(self._medias)
class Media(object):
order = 1000
def __init__(self, mountpoint, device=None,
type=None, options=None, removable=False):
self._mountpoint = os.path.normpath(mountpoint)
self._device = device
self._type = type
self._options = options
self._removable = removable
self.resetState()
def resetState(self):
self._wasmounted = self.isMounted()
def restoreState(self):
if self._wasmounted:
self.mount()
else:
self.umount()
def getMountPoint(self):
return self._mountpoint
def getDevice(self):
return self._device
def getType(self):
return self._type
def getOptions(self):
return self._options
def isRemovable(self):
return self._removable
def wasMounted(self):
return self._wasmounted
def isMounted(self):
if not os.path.isfile("/proc/mounts"):
if self._mountpoint:
return os.path.ismount(self._mountpoint)
raise Error, _("/proc/mounts not found")
for line in open("/proc/mounts"):
device, mountpoint, type = line.split()[:3]
if mountpoint == self._mountpoint:
return True
return False
def mount(self):
return True
def umount(self):
return True
def eject(self):
if self._device:
status, output = commands.getstatusoutput("eject %s" %
self._device)
if status == 0:
return True
return False
def joinPath(self, path):
if path.startswith("localmedia:/"):
path = path[12:]
while path and path[0] == "/":
path = path[1:]
return os.path.join(self._mountpoint, path)
def joinURL(self, path):
if path.startswith("localmedia:/"):
path = path[12:]
while path and path[0] == "/":
path = path[1:]
return os.path.join("file://"+self._mountpoint, path)
def convertDevicePath(self, path):
if path.startswith(self._device):
path = path[len(self._device):]
while path and path[0] == "/":
path = path[1:]
path = os.path.join(self._mountpoint, path)
return path
def hasFile(self, path, comparepath=None):
if self.isMounted():
filepath = self.joinPath(path)
if (os.path.isfile(filepath) and
not comparepath or compareFiles(path, comparepath)):
return True
return False
def __lt__(self, other):
return self.order < other.order
class MountMedia(Media):
def mount(self):
if self.isMounted():
return True
if self._device:
cmd = "mount %s %s" % (self._device, self._mountpoint)
if self._type:
cmd += " -t %s" % self._type
else:
cmd = "mount %s" % self._mountpoint
if self._options:
cmd += " -o %s" % self._options
status, output = commands.getstatusoutput(cmd)
if status != 0:
iface.debug(output)
return False
return True
class UmountMedia(Media):
def umount(self):
if not self.isMounted():
return True
status, output = commands.getstatusoutput("umount %s" %
self._mountpoint)
if status != 0:
iface.debug(output)
return False
return True
class BasicMedia(MountMedia, UmountMedia):
pass
class AutoMountMedia(UmountMedia):
order = 500
def mount(self):
try:
os.listdir(self._mountpoint)
except OSError:
return False
else:
return True
class DeviceMedia(BasicMedia):
order = 100
def mount(self):
if not os.path.isdir(self._mountpoint):
os.mkdir(self._mountpoint)
BasicMedia.mount(self)
def umount(self):
BasicMedia.umount(self)
try:
os.rmdir(self._mountpoint)
except OSError:
pass
def discoverFstabMedias(filename="/etc/fstab"):
result = []
if os.path.isfile(filename):
for line in open(filename):
line = line.strip()
if not line or line[0] == "#":
continue
tokens = line.split()
if len(tokens) < 3:
continue
device, mountpoint, type = tokens[:3]
if device == "none":
device = None
if type == "supermount":
result.append(MountMedia(mountpoint))
elif (type in ("iso9660", "udf", "udf,iso9660") or
device in ("/dev/cdrom", "/dev/dvd") or
mountpoint.endswith("/cdrom") or mountpoint.endswith("/dvd")):
result.append(BasicMedia(mountpoint, device, removable=True))
return result
hooks.register("discover-medias", discoverFstabMedias)
def discoverAutoMountMedias(filename="/etc/auto.master"):
result = []
if os.access(filename, os.R_OK):
for line in open(filename):
line = line.strip()
if not line or line[0] == "#":
continue
tokens = line.split()
if len(tokens) < 2:
continue # +auto.master syntax, not yet supported
prefix, mapfile = tokens[:2]
if os.access(mapfile, os.R_OK):
firstline = False
for line in open(mapfile):
if firstline and line.startswith("#!"):
firstline = False
break
line = line.strip()
if not line or line[0] == "#":
continue
tokens = line.split()
if len(tokens) == 2:
key, location = tokens
type = None
elif len(tokens) == 3:
key, type, location = tokens
else:
continue
if (type and "-fstype=iso9660" in type or
location in (":/dev/cdrom", ":/dev/dvd")):
mountpoint = os.path.join(prefix, key)
device = location[1:]
result.append(AutoMountMedia(mountpoint, device,
removable=True))
return result
hooks.register("discover-medias", discoverAutoMountMedias)
def discoverHalVolumeMedias():
result = []
if dbus:
import sys
import StringIO
olderr = sys.stderr
sys.stderr = StringIO.StringIO()
try:
bus = dbus.SystemBus()
hal_object = bus.get_object('org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
hal_manager = dbus.Interface(hal_object, 'org.freedesktop.Hal.Manager')
volume_udi_list = hal_manager.FindDeviceByCapability('volume')
for udi in volume_udi_list:
dev_object = bus.get_object('org.freedesktop.Hal', udi)
volume = dbus.Interface(dev_object, 'org.freedesktop.Hal.Device')
device = volume.GetProperty('block.device')
fstype = volume.GetProperty('volume.fstype')
mount_point = volume.GetProperty('volume.mount_point')
storage_udi = volume.GetProperty('block.storage_device')
dev_object = bus.get_object('org.freedesktop.Hal', storage_udi)
storage = dbus.Interface(dev_object, 'org.freedesktop.Hal.Device')
drive_type = storage.GetProperty('storage.drive_type')
if mount_point and (fstype == "iso9660" or drive_type == "cdrom"):
result.append(AutoMountMedia(mount_point, device,
removable=True))
except:
pass
err = sys.stderr.getvalue()
sys.stderr = olderr
return result
hooks.register("discover-medias", discoverHalVolumeMedias)
def discoverDeviceKitDisksMedias():
result = []
if dbus:
import sys
import StringIO
olderr = sys.stderr
sys.stderr = StringIO.StringIO()
try:
bus = dbus.SystemBus()
dk_object = bus.get_object('org.freedesktop.DeviceKit.Disks',
'/org/freedesktop/DeviceKit/Disks')
dk_interface = dbus.Interface(dk_object, 'org.freedesktop.DeviceKit.Disks')
for path in dk_interface.EnumerateDevices():
dev_object = bus.get_object('org.freedesktop.DeviceKit.Disks', path)
interface = 'org.freedesktop.DeviceKit.Disks.Device'
volume = dbus.Interface(dev_object, 'org.freedesktop.DBus.Properties')
device = str(volume.Get(interface, 'DeviceFile'))
fstype = str(volume.Get(interface, 'IdType'))
mount_paths = volume.Get(interface, 'DeviceMountPaths')
optical_disc = bool(volume.Get(interface, 'DeviceIsOpticalDisc'))
is_removable = bool(volume.Get(interface, 'DeviceIsRemovable'))
if mount_paths and (fstype == "iso9660" or optical_disc):
mount_point = unicode(mount_paths.pop())
result.append(AutoMountMedia(mount_point, device,
removable=is_removable))
except:
pass
err = sys.stderr.getvalue()
sys.stderr = olderr
return result
hooks.register("discover-medias", discoverDeviceKitDisksMedias)
def discoverDeviceMedia(path):
mntdir = os.path.join(sysconf.get("data-dir"), "mnt")
if not os.path.isdir(mntdir):
try:
os.makedirs(mntdir)
except OSError:
return None
elif not os.access(mntdir, os.W_OK):
return None
dirname, basename = os.path.split(path)
suffix = 0
mountpoint = os.path.join(mntdir, basename)
while os.path.ismount(mountpoint):
suffix += 1
mountpoint = os.path.join(mntdir, basename+(".%d" % suffix))
if suffix:
basename += ".%d" % suffix
st = os.stat(path)
if stat.S_ISBLK(st.st_mode):
options = None
else:
options = "loop"
return DeviceMedia(mountpoint, path, options=options)
hooks.register("discover-device-media", discoverDeviceMedia)
|