Package CedarBackup2 :: Package writers :: Module cdwriter :: 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.

Image Writer Interface

The following methods make up the "image writer" interface shared with other kinds of writers (such as DVD writers):
  __init__
  initializeImage()
  addImageEntry()
  writeImage()
  setImageNewDisc()
  retrieveCapacity()
  getEstimatedImageSize()

Only these methods will be used by other Cedar Backup functionality that expects a compatible image writer.

The media attribute is also assumed to be available.

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, noEject, unittest)
Initializes a CD writer object.
  isRewritable(self)
Indicates whether the media is rewritable per configuration.
  retrieveCapacity(self, entireDisc, useMulti)
Retrieves capacity for the current media in terms of a MediaCapacity object.
  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.
  initializeImage(self, newDisc, tmpdir, mediaLabel)
Initializes the writer's associated ISO image.
  addImageEntry(self, path, graftPoint)
Adds a filepath entry to the writer's associated ISO image.
  setImageNewDisc(self, newDisc)
Resets (overrides) the newDisc flag on the internal image.
  getEstimatedImageSize(self)
Gets the estimated size of the image associated with the writer.
    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=None, driveSpeed=None, mediaType=1, noEject=False, 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.

If cdrecord improperly detects whether your writer device has a tray and can be safely opened and closed, then pass in noEject=False. This will override the properties and the device will never be ejected.
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.)
noEject - Overrides properties to indicate that the device does not support eject.
           (type=Boolean true/false)
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.

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.

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.

If the writer was constructed with noEject=True, then this is a no-op.
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.

If the writer was constructed with noEject=True, then this is a no-op.
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=None, 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).

if imagePath is passed in as None, then the existing image configured with initializeImage will be used. Under these circumstances, the passed-in newDisc flag will be ignored.

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 (i.e. must take into account any existing session boundaries, etc.)
Parameters:
imagePath - Path to an ISO image on disk, or None to use writer's image
           (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.
ValueError - If no image is passed in and initializeImage() was not previously called

initializeImage(self, newDisc, tmpdir, mediaLabel=None)

Initializes the writer's associated ISO image.

This method initializes the image instance variable so that the caller can use the addImageEntry method. Once entries have been added, the writeImage method can be called with no arguments.
Parameters:
newDisc - Indicates whether the disc should be re-initialized
           (type=Boolean true/false.)
tmpdir - Temporary directory to use if needed
           (type=String representing a directory path on disk)
mediaLabel - Media label to be applied to the image, if any
           (type=String, no more than 25 characters long)

addImageEntry(self, path, graftPoint)

Adds a filepath entry to the writer's associated ISO image.

The contents of the filepath -- but not the path itself -- will be added to the image at the indicated graft point. If you don't want to use a graft point, just pass None.
Parameters:
path - File or directory to be added to the image
           (type=String representing a path on disk)
graftPoint - Graft point to be used when adding this entry
           (type=String representing a graft point path, as described above)
Raises:
ValueError - If initializeImage() was not previously called

Note: Before calling this method, you must call initializeImage.

setImageNewDisc(self, newDisc)

Resets (overrides) the newDisc flag on the internal image.
Parameters:
newDisc - New disc flag to set
Raises:
ValueError - If initializeImage() was not previously called

getEstimatedImageSize(self)

Gets the estimated size of the image associated with the writer.
Returns:
Estimated size of the image, in bytes.
Raises:
IOError - If there is a problem calling mkisofs.
ValueError - If initializeImage() was not previously called

Property Details

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.

Generated by Epydoc 2.1 on Thu Mar 29 20:58:28 2007 http://epydoc.sf.net