Package CedarBackup2 :: Module writer :: Class CdWriter
[show private | hide private]
[frames | no frames]

Type CdWriter

object --+
         |
        CdWriter


Class representing a device that knows how to write CD media.

Summary

This is a class representing a device that knows how to write CD media. It provides common operations for the device, such as ejecting the media, writing an ISO image to the media, or checking for the current media capacity. It also provides a place to store device attributes, such as whether the device supports writing multisession discs, etc.

This class is implemented in terms of the eject and cdrecord programs, both of which should be available on most UN*X platforms.

The public methods other than the constructor are part of an "image writer" interface that will be shared with other future classes which write other kinds of media.

Media Types

This class knows how to write to two different kinds of media, represented by the following constants:

Most hardware can read and write both 74-minute and 80-minute CD-R and CD-RW media. Some older drives may only be able to write CD-R media. The difference between the two is that CD-RW media can be rewritten (erased), while CD-R media cannot be.

I do not support any other configurations for a couple of reasons. The first is that I've never tested any other kind of media. The second is that anything other than 74 or 80 minute is apparently non-standard.

Device Attributes vs. Media Attributes

A given writer instance has two different kinds of attributes associated with it, which I call device attributes and media attributes. Device attributes are things which can be determined without looking at the media, such as whether the drive supports writing multisession disks or has a tray. Media attributes are attributes which vary depending on the state of the media, such as the remaining capacity on a disc. In general, device attributes are available via instance variables and are constant over the life of an object, while media attributes can be retrieved through method calls.

Talking to Hardware

This class needs to talk to CD writer hardware in two different ways: through cdrecord to actually write to the media, and through the filesystem to do things like open and close the tray.

Historically, CdWriter has interacted with cdrecord using the scsiId attribute, and with most other utilities using the device attribute. This changed somewhat in Cedar Backup 2.9.0.

When Cedar Backup was first written, the only way to interact with cdrecord was by using a SCSI device id. IDE devices were mapped to pseudo-SCSI devices through the kernel. Later, extended SCSI "methods" arrived, and it became common to see ATA:1,0,0 or ATAPI:0,0,0 as a way to address IDE hardware. By late 2006, ATA and ATAPI had apparently been deprecated in favor of just addressing the IDE device directly by name, i.e. /dev/cdrw.

Because of this latest development, it no longer makes sense to require a CdWriter to be created with a SCSI id -- there might not be one. So, the passed-in SCSI id is now optional. Also, there is now a hardwareId attribute. This attribute is filled in with either the SCSI id (if provided) or the device (otherwise). The hardware id is the value that will be passed to cdrecord in the dev= argument.

Testing

It's rather difficult to test this code in an automated fashion, even if you have access to a physical CD writer drive. It's even more difficult to test it if you are running on some build daemon (think of a Debian autobuilder) which can't be expected to have any hardware or any media that you could write to.

Because of this, much of the implementation below is in terms of static methods that are supposed to take defined actions based on their arguments. Public methods are then implemented in terms of a series of calls to simplistic static methods. This way, we can test as much as possible of the functionality via testing the static methods, while hoping that if the static methods are called appropriately, things will work properly. It's not perfect, but it's much better than no testing at all.
Method Summary
  __init__(self, device, scsiId, driveSpeed, mediaType, unittest)
Initializes a CD writer object.
  isRewritable(self)
Indicates whether the media is rewritable per configuration.
  _retrieveProperties(self)
Retrieves properties for a device from cdrecord.
  retrieveCapacity(self, entireDisc, useMulti)
Retrieves capacity for the current media in terms of a MediaCapacity object.
  _getBoundaries(self, entireDisc, useMulti)
Gets the ISO boundaries for the media.
  _calculateCapacity(media, boundaries)
Calculates capacity for the media in terms of boundaries. (Static method)
  openTray(self)
Opens the device's tray and leaves it open.
  closeTray(self)
Closes the device's tray.
  refreshMedia(self)
Opens and then immediately closes the device's tray, to refresh the device's idea of the media.
  writeImage(self, imagePath, newDisc, writeMulti)
Writes an ISO image to the media in the device.
  _blankMedia(self)
Blanks the media in the device, if the media is rewritable.
  _parsePropertiesOutput(output)
Parses the output from a cdrecord properties command. (Static method)
  _parseBoundariesOutput(output)
Parses the output from a cdrecord capacity command. (Static method)
  _buildOpenTrayArgs(device)
Builds a list of arguments to be passed to a eject command. (Static method)
  _buildCloseTrayArgs(device)
Builds a list of arguments to be passed to a eject command. (Static method)
  _buildPropertiesArgs(hardwareId)
Builds a list of arguments to be passed to a cdrecord command. (Static method)
  _buildBoundariesArgs(hardwareId)
Builds a list of arguments to be passed to a cdrecord command. (Static method)
  _buildBlankArgs(hardwareId, driveSpeed)
Builds a list of arguments to be passed to a cdrecord command. (Static method)
  _buildWriteArgs(hardwareId, imagePath, driveSpeed, writeMulti)
Builds a list of arguments to be passed to a cdrecord command. (Static method)
  _getDevice(self)
Property target used to get the device value.
  _getDeviceBufferSize(self)
Property target used to get the device buffer size.
  _getDeviceCanEject(self)
Property target used to get the device-can-eject flag.
  _getDeviceHasTray(self)
Property target used to get the device-has-tray flag.
  _getDeviceId(self)
Property target used to get the device id.
  _getDeviceSupportsMulti(self)
Property target used to get the device-support-multi flag.
  _getDeviceType(self)
Property target used to get the device type.
  _getDeviceVendor(self)
Property target used to get the device vendor.
  _getDriveSpeed(self)
Property target used to get the drive speed.
  _getHardwareId(self)
Property target used to get the hardware id value.
  _getMedia(self)
Property target used to get the media description.
  _getScsiId(self)
Property target used to get the SCSI id value.
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __repr__(x)
x.__repr__() <==> repr(x)
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
x.__str__() <==> str(x)

Property Summary
  device: Filesystem device name for this writer.
  scsiId: SCSI id for the device, in the form [<method>:]scsibus,target,lun.
  hardwareId: Hardware id for this writer, either SCSI id or device path.
  driveSpeed: Speed at which the drive writes.
  media: Definition of media that is expected to be in the device.
  deviceType: Type of the device, as returned from cdrecord -prcap.
  deviceVendor: Vendor of the device, as returned from cdrecord -prcap.
  deviceId: Device identification, as returned from cdrecord -prcap.
  deviceBufferSize: Size of the device's write buffer, in bytes.
  deviceSupportsMulti: Indicates whether device supports multisession discs.
  deviceHasTray: Indicates whether the device has a media tray.
  deviceCanEject: Indicates whether the device supports ejecting its media.

Instance Method Details

__init__(self, device, scsiId, driveSpeed=None, mediaType=1, unittest=False)
(Constructor)

Initializes a CD writer object.

The current user must have write access to the device at the time the object is instantiated, or an exception will be thrown. However, no media-related validation is done, and in fact there is no need for any media to be in the drive until one of the other media attribute-related methods is called.

The various instance variables such as deviceType, deviceVendor, etc. might be None, if we're unable to parse this specific information from the cdrecord output. This information is just for reference.

The SCSI id is optional, but the device path is required. If the SCSI id is passed in, then the hardware id attribute will be taken from the SCSI id. Otherwise, the hardware id will be taken from the device.
Parameters:
device - Filesystem device associated with this writer.
           (type=Absolute path to a filesystem device, i.e. /dev/cdrw)
scsiId - SCSI id for the device (optional).
           (type=If provided, SCSI id in the form [<method>:]scsibus,target,lun)
driveSpeed - Speed at which the drive writes.
           (type=Use 2 for 2x device, etc. or None to use device default.)
mediaType - Type of the media that is assumed to be in the drive.
           (type=One of the valid media type as discussed above.)
unittest - Turns off certain validations, for use in unit testing.
           (type=Boolean true/false)
Raises:
ValueError - If the device is not valid for some reason.
ValueError - If the SCSI id is not in a valid form.
ValueError - If the drive speed is not an integer >= 1.
IOError - If device properties could not be read for some reason.
Overrides:
__builtin__.object.__init__

Note: The unittest parameter should never be set to True outside of Cedar Backup code. It is intended for use in unit testing Cedar Backup internals and has no other sensible purpose.

isRewritable(self)

Indicates whether the media is rewritable per configuration.

_retrieveProperties(self)

Retrieves properties for a device from cdrecord.

The results are returned as a tuple of the object device attributes as returned from _parsePropertiesOutput: (deviceType, deviceVendor, deviceId, deviceBufferSize, deviceSupportsMulti, deviceHasTray, deviceCanEject).
Returns:
Results tuple as described above.
Raises:
IOError - If there is a problem talking to the device.

retrieveCapacity(self, entireDisc=False, useMulti=True)

Retrieves capacity for the current media in terms of a MediaCapacity object.

If entireDisc is passed in as True the capacity will be for the entire disc, as if it were to be rewritten from scratch. If the drive does not support writing multisession discs or if useMulti is passed in as False, the capacity will also be as if the disc were to be rewritten from scratch, but the indicated boundaries value will be None. The same will happen if the disc cannot be read for some reason. Otherwise, the capacity (including the boundaries) will represent whatever space remains on the disc to be filled by future sessions.
Parameters:
entireDisc - Indicates whether to return capacity for entire disc.
           (type=Boolean true/false)
useMulti - Indicates whether a multisession disc should be assumed, if possible.
           (type=Boolean true/false)
Returns:
MediaCapacity object describing the capacity of the media.
Raises:
IOError - If the media could not be read for some reason.

_getBoundaries(self, entireDisc=False, useMulti=True)

Gets the ISO boundaries for the media.

If entireDisc is passed in as True the boundaries will be None, as if the disc were to be rewritten from scratch. If the drive does not support writing multisession discs, the returned value will be None. The same will happen if the disc can't be read for some reason. Otherwise, the returned value will be represent the boundaries of the disc's current contents.

The results are returned as a tuple of (lower, upper) as needed by the IsoImage class. Note that these values are in terms of ISO sectors, not bytes. Clients should generally consider the boundaries value opaque, however.
Parameters:
entireDisc - Indicates whether to return capacity for entire disc.
           (type=Boolean true/false)
useMulti - Indicates whether a multisession disc should be assumed, if possible.
           (type=Boolean true/false)
Returns:
Boundaries tuple or None, as described above.
Raises:
IOError - If the media could not be read for some reason.

openTray(self)

Opens the device's tray and leaves it open.

This only works if the device has a tray and supports ejecting its media. We have no way to know if the tray is currently open or closed, so we just send the appropriate command and hope for the best. If the device does not have a tray or does not support ejecting its media, then we do nothing.
Raises:
IOError - If there is an error talking to the device.

closeTray(self)

Closes the device's tray.

This only works if the device has a tray and supports ejecting its media. We have no way to know if the tray is currently open or closed, so we just send the appropriate command and hope for the best. If the device does not have a tray or does not support ejecting its media, then we do nothing.
Raises:
IOError - If there is an error talking to the device.

refreshMedia(self)

Opens and then immediately closes the device's tray, to refresh the device's idea of the media.

Sometimes, a device gets confused about the state of its media. Often, all it takes to solve the problem is to eject the media and then immediately reload it.

This only works if the device has a tray and supports ejecting its media. We have no way to know if the tray is currently open or closed, so we just send the appropriate command and hope for the best. If the device does not have a tray or does not support ejecting its media, then we do nothing.
Raises:
IOError - If there is an error talking to the device.

writeImage(self, imagePath, newDisc=False, writeMulti=True)

Writes an ISO image to the media in the device.

If newDisc is passed in as True, we assume that the entire disc will be overwritten, and the media will be blanked before writing it if possible (i.e. if the media is rewritable).

If writeMulti is passed in as True, then a multisession disc will be written if possible (i.e. if the drive supports writing multisession discs.

By default, we assume that the disc can be written multisession and that we should append to the current contents of the disc. In any case, the ISO image must be generated appropriately (must take into account any existing session boundaries, etc.)
Parameters:
imagePath - Path to an ISO image on disk.
           (type=String representing a path on disk)
newDisc - Indicates whether the entire disc will overwritten.
           (type=Boolean true/false.)
writeMulti - Indicates whether a multisession disc should be written, if possible.
           (type=Boolean true/false)
Raises:
ValueError - If the image path is not absolute.
ValueError - If some path cannot be encoded properly.
IOError - If the media could not be written to for some reason.

_blankMedia(self)

Blanks the media in the device, if the media is rewritable.
Raises:
IOError - If the media could not be written to for some reason.

_getDevice(self)

Property target used to get the device value.

_getDeviceBufferSize(self)

Property target used to get the device buffer size.

_getDeviceCanEject(self)

Property target used to get the device-can-eject flag.

_getDeviceHasTray(self)

Property target used to get the device-has-tray flag.

_getDeviceId(self)

Property target used to get the device id.

_getDeviceSupportsMulti(self)

Property target used to get the device-support-multi flag.

_getDeviceType(self)

Property target used to get the device type.

_getDeviceVendor(self)

Property target used to get the device vendor.

_getDriveSpeed(self)

Property target used to get the drive speed.

_getHardwareId(self)

Property target used to get the hardware id value.

_getMedia(self)

Property target used to get the media description.

_getScsiId(self)

Property target used to get the SCSI id value.

Static Method Details

_calculateCapacity(media, boundaries)

Calculates capacity for the media in terms of boundaries.

If boundaries is None or the lower bound is 0 (zero), then the capacity will be for the entire disc minus the initial lead in. Otherwise, capacity will be as if the caller wanted to add an additional session to the end of the existing data on the disc.
Parameters:
media - MediaDescription object describing the media capacity.
boundaries - Session boundaries as returned from _getBoundaries.
Returns:
MediaCapacity object describing the capacity of the media.

_parsePropertiesOutput(output)

Parses the output from a cdrecord properties command.

The output parameter should be a list of strings as returned from executeCommand for a cdrecord command with arguments as from _buildPropertiesArgs. The list of strings will be parsed to yield information about the properties of the device.

The output is expected to be a huge long list of strings. Unfortunately, the strings aren't in a completely regular format. However, the format of individual lines seems to be regular enough that we can look for specific values. Two kinds of parsing take place: one kind of parsing picks out out specific values like the device id, device vendor, etc. The other kind of parsing just sets a boolean flag True if a matching line is found. All of the parsing is done with regular expressions.

Right now, pretty much nothing in the output is required and we should parse an empty document successfully (albeit resulting in a device that can't eject, doesn't have a tray and doesnt't support multisession discs). I had briefly considered erroring out if certain lines weren't found or couldn't be parsed, but that seems like a bad idea given that most of the information is just for reference.

The results are returned as a tuple of the object device attributes: (deviceType, deviceVendor, deviceId, deviceBufferSize, deviceSupportsMulti, deviceHasTray, deviceCanEject).
Parameters:
output - Output from a cdrecord -prcap command.
Returns:
Results tuple as described above.
Raises:
IOError - If there is problem parsing the output.

_parseBoundariesOutput(output)

Parses the output from a cdrecord capacity command.

The output parameter should be a list of strings as returned from executeCommand for a cdrecord command with arguments as from _buildBoundaryArgs. The list of strings will be parsed to yield information about the capacity of the media in the device.

Basically, we expect the list of strings to include just one line, a pair of values. There isn't supposed to be whitespace, but we allow it anyway in the regular expression. Any lines below the one line we parse are completely ignored. It would be a good idea to ignore stderr when executing the cdrecord command that generates output for this method, because sometimes cdrecord spits out kernel warnings about the actual output.

The results are returned as a tuple of (lower, upper) as needed by the IsoImage class. Note that these values are in terms of ISO sectors, not bytes. Clients should generally consider the boundaries value opaque, however.
Parameters:
output - Output from a cdrecord -msinfo command.
Returns:
Boundaries tuple as described above.
Raises:
IOError - If there is problem parsing the output.

Note: If the boundaries output can't be parsed, we return None.

_buildOpenTrayArgs(device)

Builds a list of arguments to be passed to a eject command.

The arguments will cause the eject command to open the tray and eject the media. No validation is done by this method as to whether this action actually makes sense.
Parameters:
device - Filesystem device name for this writer, i.e. /dev/cdrw.
Returns:
List suitable for passing to util.executeCommand as args.

_buildCloseTrayArgs(device)

Builds a list of arguments to be passed to a eject command.

The arguments will cause the eject command to close the tray and reload the media. No validation is done by this method as to whether this action actually makes sense.
Parameters:
device - Filesystem device name for this writer, i.e. /dev/cdrw.
Returns:
List suitable for passing to util.executeCommand as args.

_buildPropertiesArgs(hardwareId)

Builds a list of arguments to be passed to a cdrecord command.

The arguments will cause the cdrecord command to ask the device for a list of its capacities via the -prcap switch.
Parameters:
hardwareId - Hardware id for the device (either SCSI id or device path)
Returns:
List suitable for passing to util.executeCommand as args.

_buildBoundariesArgs(hardwareId)

Builds a list of arguments to be passed to a cdrecord command.

The arguments will cause the cdrecord command to ask the device for the current multisession boundaries of the media using the -msinfo switch.
Parameters:
hardwareId - Hardware id for the device (either SCSI id or device path)
Returns:
List suitable for passing to util.executeCommand as args.

_buildBlankArgs(hardwareId, driveSpeed=None)

Builds a list of arguments to be passed to a cdrecord command.

The arguments will cause the cdrecord command to blank the media in the device identified by hardwareId. No validation is done by this method as to whether the action makes sense (i.e. to whether the media even can be blanked).
Parameters:
hardwareId - Hardware id for the device (either SCSI id or device path)
driveSpeed - Speed at which the drive writes.
Returns:
List suitable for passing to util.executeCommand as args.

_buildWriteArgs(hardwareId, imagePath, driveSpeed=None, writeMulti=True)

Builds a list of arguments to be passed to a cdrecord command.

The arguments will cause the cdrecord command to write the indicated ISO image (imagePath) to the media in the device identified by hardwareId. The writeMulti argument controls whether to write a multisession disc. No validation is done by this method as to whether the action makes sense (i.e. to whether the device even can write multisession discs, for instance).
Parameters:
hardwareId - Hardware id for the device (either SCSI id or device path)
imagePath - Path to an ISO image on disk.
driveSpeed - Speed at which the drive writes.
writeMulti - Indicates whether to write a multisession disc.
Returns:
List suitable for passing to util.executeCommand as args.

Property Details

device

Filesystem device name for this writer.
Get Method:
_getDevice(self)

scsiId

SCSI id for the device, in the form [<method>:]scsibus,target,lun.
Get Method:
_getScsiId(self)

hardwareId

Hardware id for this writer, either SCSI id or device path.
Get Method:
_getHardwareId(self)

driveSpeed

Speed at which the drive writes.
Get Method:
_getDriveSpeed(self)

media

Definition of media that is expected to be in the device.
Get Method:
_getMedia(self)

deviceType

Type of the device, as returned from cdrecord -prcap.
Get Method:
_getDeviceType(self)

deviceVendor

Vendor of the device, as returned from cdrecord -prcap.
Get Method:
_getDeviceVendor(self)

deviceId

Device identification, as returned from cdrecord -prcap.
Get Method:
_getDeviceId(self)

deviceBufferSize

Size of the device's write buffer, in bytes.
Get Method:
_getDeviceBufferSize(self)

deviceSupportsMulti

Indicates whether device supports multisession discs.
Get Method:
_getDeviceSupportsMulti(self)

deviceHasTray

Indicates whether the device has a media tray.
Get Method:
_getDeviceHasTray(self)

deviceCanEject

Indicates whether the device supports ejecting its media.
Get Method:
_getDeviceCanEject(self)

Generated by Epydoc 2.1 on Mon Dec 18 22:53:31 2006 http://epydoc.sf.net