/usr/share/pyshared/parted/device.py is in python-parted 3.6-4.
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 | #
# device.py
# Python bindings for libparted (built on top of the _ped Python module).
#
# Copyright (C) 2009 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): David Cantrell <dcantrell@redhat.com>
#
import math
import warnings
import _ped
import parted
from disk import diskType
from decorators import localeC
class Device(object):
"""Device()
Device represents a phyiscal piece of hardware in the system, e.g. a
disk. A Device should be considered a low-level and operating system
specific interface to the hardware.
A number of read-only properties about the Device are available.
For information on the individual methods, see help(Device.METHODNAME)"""
@localeC
def __init__(self, path=None, PedDevice=None):
"""Create a new Device object based on the specified path or the
already existing _ped.Device object. You must provide either a
path (e.g., "/dev/sda") or an existing _ped.Device object, but
not both."""
if PedDevice:
self.__device = PedDevice
elif path is not None:
self.__device = _ped.device_get(path)
else:
raise parted.DeviceException, "no path or PedDevice specified"
def __eq__(self, other):
return not self.__ne__(other)
def __ne__(self, other):
if hash(self) == hash(other):
return False
if type(self) != type(other):
return True
return self.model != other.model or self.path != other.path or self.type != other.type or self.length != other.length
def __getCHS(self, geometry):
return (geometry.cylinders, geometry.heads, geometry.sectors)
@property
def model(self):
"""Model name and vendor of this device."""
return self.__device.model
@property
def path(self):
"""Filesystem node path of this device (e.g., /dev/sda)."""
return self.__device.path
@property
def type(self):
"""Type of this device. An integer constant corresponding
to one of the parted.DEVICE_* values.
"""
return self.__device.type
@property
def sectorSize(self):
"""Sector size (in bytes) for this device."""
return self.__device.sector_size
@property
def physicalSectorSize(self):
"""Physical sector size (in bytes) for this device. Not always
the same as sectorSize, but is a multiple of sectorSize.
"""
return self.__device.phys_sector_size
@property
def length(self):
"""The size of this device in sectors."""
return self.__device.length
@property
def openCount(self):
"""How many times the open() method has been called on this device."""
return self.__device.open_count
@property
def readOnly(self):
"""True if the device is currently in read-only mode, False
otherwise.
"""
return bool(self.__device.read_only)
@property
def externalMode(self):
"""True if external access mode is currently activated on this
device, False otherwise. External access mode has to be used
if you want to use an external command on the device while
you are currently using it in pyparted.
"""
return bool(self.__device.external_mode)
@property
def dirty(self):
"""True if the device is marked dirty, False otherwise."""
return bool(self.__device.dirty)
@property
def bootDirty(self):
"""True if the device is marked boot dirty, False otherwise."""
return bool(self.__device.boot_dirty)
@property
def host(self):
"""The host value of this device."""
return self.__device.host
@property
def did(self):
"""The did value of this device."""
return self.__device.did
@property
def busy(self):
"""True if this device is busy, False otherwise."""
return self.__device.is_busy()
@property
def hardwareGeometry(self):
"""A 3-tuple representing the hardware geometry of this device.
The tuple is in order of cylinders, heads, and sectors.
"""
return self.__getCHS(self.__device.hw_geom)
@property
def biosGeometry(self):
"""A 3-tuple representing the BIOS geometry of this device.
The tuple is in order of cylinders, heads, and sectors.
"""
return self.__getCHS(self.__device.bios_geom)
def __str__(self):
s = ("parted.Device instance --\n"
" model: %(model)s path: %(path)s type: %(type)s\n"
" sectorSize: %(sectorSize)s physicalSectorSize: %(physSectorSize)s\n"
" length: %(length)s openCount: %(openCount)s readOnly: %(readOnly)s\n"
" externalMode: %(external)s dirty: %(dirty)s bootDirty: %(bootDirty)s\n"
" host: %(host)s did: %(did)s busy: %(busy)s\n"
" hardwareGeometry: %(hardwareGeom)s biosGeometry: %(biosGeom)s\n"
" PedDevice: %(ped)r" %
{"model": self.model, "path": self.path, "type": self.type,
"sectorSize": self.sectorSize, "physSectorSize": self.physicalSectorSize,
"length": self.length, "openCount": self.openCount, "readOnly": self.readOnly,
"external": self.externalMode, "dirty": self.dirty, "bootDirty": self.bootDirty,
"host": self.host, "did": self.did, "busy": self.busy,
"hardwareGeom": self.hardwareGeometry, "biosGeom": self.biosGeometry,
"ped": self.__device})
return s
@localeC
def clobber(self):
"""Remove all identifying signatures of the partition table."""
return self.__device.clobber()
@localeC
def open(self):
"""Open this Device for read operations."""
return self.__device.open()
@localeC
def close(self):
"""Close this Device. Used after open() method calls."""
return self.__device.close()
@localeC
def destroy(self):
"""Destroy this Device. Operating system specific."""
return self.__device.destroy()
@localeC
def removeFromCache(self):
"""Remove this Device from the internal libparted device cache."""
return self.__device.cache_remove()
@localeC
def beginExternalAccess(self):
"""Set up the Device for use by an external program. Call this method
before running an external program that uses the Device."""
return self.__device.begin_external_access()
@localeC
def endExternalAccess(self):
"""Turn off external access mode for the Device. Call this method once
your external program has finished using the Device."""
return self.__device.end_external_access()
@localeC
def read(self, start, count):
"""From the sector indentified by start, read and return count sectors
from the Device."""
return self.__device.read(start, count)
@localeC
def write(self, buffer, start, count):
"""From the sector identified by start, write count sectors from
buffer to the Device."""
return self.__device.write(buffer, start, count)
@localeC
def sync(self, fast=False):
"""Perform a operating-system specific sync(2) operation on the
Device. If fast is True, try to perform a fast sync(2)."""
if fast:
return self.__device.sync_fast()
else:
return self.__device.sync()
@localeC
def check(self, start, count):
"""From the sector identified by start, perform an operating
system specific check on count sectors."""
return self.__device.check(start, count)
@localeC
def startSectorToCylinder(self, sector):
"""Return the closest cylinder (round down) to sector on
this Device."""
(cylinders, heads, sectors) = self.biosGeometry
return long(math.floor((float(sector) / (heads * sectors)) + 1))
@localeC
def endSectorToCylinder(self, sector):
"""Return the closest cylinder (round up) to sector on
this Device."""
(cylinders, heads, sectors) = self.biosGeometry
return long(math.ceil(float((sector + 1)) / (heads * sectors)))
@localeC
def startCylinderToSector(self, cylinder):
"""Return the sector corresponding to cylinder as a
starting cylinder on this Device."""
(cylinders, heads, sectors) = self.biosGeometry
return long((cylinder - 1) * (heads * sectors))
@localeC
def endCylinderToSector(self, cylinder):
"""Return the sector corresponding to cylinder as an
ending cylinder on this Device."""
(cylinders, heads, sectors) = self.biosGeometry
return long(((cylinder) * (heads * sectors)) - 1)
def getSize(self, unit="MB"):
"""Return the size of the Device in the unit specified. The unit
is given as a string corresponding to one of the following
abbreviations: b (bytes), KB (kilobytes), MB (megabytes), GB
(gigabytes), TB (terabytes). An invalid unit string will raise a
SyntaxError exception. The default unit is MB."""
warnings.warn("use the getLength method", DeprecationWarning)
lunit = unit.lower()
if lunit not in parted._exponent.keys():
raise SyntaxError, "invalid unit %s given" % (unit,)
size = float(self.__device.length)
size /= math.pow(1024.0, parted._exponent[lunit])
size *= self.sectorSize
return size
@localeC
def getLength(self, unit='sectors'):
"""Return the length of the device in sectors. Optionally, a SI or
IEC prefix followed by a 'B' may be given in order to convert the
length into bytes. The allowed values include B, kB, MB, GB, TB, KiB,
MiB, GiB, and TiB."""
sectors = self.__device.length
if unit == "sectors":
return sectors
return parted.formatBytes(sectors * self.sectorSize, unit)
@localeC
def getConstraint(self):
"""Return a Constraint defining the limitations imposed by
this Device."""
return parted.Constraint(PedConstraint=self.__device.get_constraint())
@property
@localeC
def minimalAlignedConstraint(self):
"""Return a Constraint defining the limitations and minimal advisable
alignment imposed by this Device."""
constraint = self.__device.get_minimal_aligned_constraint()
return parted.Constraint(PedConstraint=constraint)
@property
@localeC
def optimalAlignedConstraint(self):
"""Return a Constraint defining the limitations and optimal
alignment imposed by this Device."""
constraint = self.__device.get_optimal_aligned_constraint()
return parted.Constraint(PedConstraint=constraint)
@property
@localeC
def minimumAlignment(self):
"""Return an Alignment defining the minimum alignment for this Device."""
alignment = self.__device.get_minimum_alignment()
return parted.Alignment(PedAlignment=alignment)
@property
@localeC
def optimumAlignment(self):
"""Return an Alignment defining the optimum alignment for this Device."""
alignment = self.__device.get_optimum_alignment()
return parted.Alignment(PedAlignment=alignment)
def getPedDevice(self):
"""Return the _ped.Device object contained in this Device.
For internal module use only."""
return self.__device
|