Package CedarBackup2 :: Package writers :: Module dvdwriter :: Class DvdWriter
[show private | hide private]
[frames | no frames]

Type DvdWriter

object --+
         |
        DvdWriter


Class representing a device that knows how to write some kinds of DVD media.

Summary

This is a class representing a device that knows how to write some kinds of DVD media. It provides common operations for the device, such as ejecting the media and writing data to the media.

This class is implemented in terms of the eject, growisofs and dvd+rw-mediainfo utilities, all 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:
  __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.

Unlike the CdWriter, the DvdWriter can only operate in terms of filesystem devices, not SCSI devices. So, although the constructor interface accepts a SCSI device parameter for the sake of compatibility, it's not used.

Media Types

This class knows how to write to DVD+R and DVD+RW media, represented by the following constants:

The difference is that DVD+RW media can be rewritten, while DVD+R media cannot be (although at present, DvdWriter does not really differentiate between rewritable and non-rewritable media).

The capacities are 4.4 GB because Cedar Backup deals in "true" gigabytes of 1024*1024*1024 bytes per gigabyte.

The underlying growisofs utility does support other kinds of media (including DVD-R, DVD-RW and BlueRay) which work somewhat differently than standard DVD+R and DVD+RW media. I don't support these other kinds of media because I haven't had any opportunity to work with them. The same goes for dual-layer media of any type.

Device Attributes vs. Media Attributes

As with the cdwriter functionality, a given dvdwriter instance has two different kinds of attributes associated with it. I call these device attributes and media attributes.

Device attributes are things which can be determined without looking at the media. Media attributes are attributes which vary depending on the state of the media. 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.

Compared to cdwriters, dvdwriters have very few attributes. This is due to differences between the way growisofs works relative to cdrecord.

Media Capacity

One major difference between the cdrecord/mkisofs utilities used by the cdwriter class and the growisofs utility used here is that the process of estimating remaining capacity and image size is more straightforward with cdrecord/mkisofs than with growisofs.

In this class, remaining capacity is calculated by asking doing a dry run of growisofs and grabbing some information from the output of that command. Image size is estimated by asking the IsoImage class for an estimate and then adding on a "fudge factor" determined through experimentation.

Testing

It's rather difficult to test this code in an automated fashion, even if you have access to a physical DVD 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, some 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 "difficult" 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 DVD writer object.
  isRewritable(self)
Indicates whether the media is rewritable per configuration.
  retrieveCapacity(self, entireDisc)
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.
  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.
  writeImage(self, imagePath, newDisc, writeMulti)
Writes an ISO image to the media in the device.
  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.
  _writeImage(self, newDisc, imagePath, entries, mediaLabel)
Writes an image to disc using either an entries list or an ISO image on disk.
  _getEstimatedImageSize(entries)
Gets the estimated size of a set of image entries. (Static method)
  _searchForOverburn(output)
Search for an "overburn" error message in growisofs output. (Static method)
  _buildWriteArgs(newDisc, hardwareId, driveSpeed, imagePath, entries, mediaLabel, dryRun)
Builds a list of arguments to be passed to a growisofs command. (Static method)
  _getDevice(self)
Property target used to get the device value.
  _getDeviceCanEject(self)
Property target used to get the device-can-eject flag.
  _getDeviceHasTray(self)
Property target used to get the device-has-tray flag.
  _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.
  _parseSectorsUsed(output)
Parse sectors used information out of growisofs output. (Static method)
  _retrieveSectorsUsed(self)
Retrieves the number of sectors used on the current media.
    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 (saved for reference only).
  hardwareId: Hardware id for this writer (always the device path).
  driveSpeed: Speed at which the drive writes.
  media: Definition of media that is expected to be in the device.
  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=2, noEject=False, unittest=False)
(Constructor)

Initializes a DVD writer object.

Since growisofs can only address devices using the device path (i.e. /dev/dvd), the hardware id will always be set based on the device. If passed in, it will be saved for reference purposes only.

We have no way to query the device to ask whether it has a tray or can be safely opened and closed. So, the noEject flag is used to set these values. If noEject=False, then we assume a tray exists and open/close is safe. If noEject=True, then we assume that there is no tray and open/close is not safe.
Parameters:
device - Filesystem device associated with this writer.
           (type=Absolute path to a filesystem device, i.e. /dev/dvd)
scsiId - SCSI id for the device (optional, for reference only).
           (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 - Tells Cedar Backup that the device cannot safely be ejected
           (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.
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)

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. The same will happen if the disc can't be read for some reason. Otherwise, the capacity will be calculated by subtracting the sectors currently used on the disc, as reported by growisofs itself.
Parameters:
entireDisc - Indicates whether to return capacity for entire disc.
           (type=Boolean true/false)
Returns:
MediaCapacity object describing the capacity of the media.
Raises:
ValueError - If there is a problem parsing the growisofs output
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.

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
ValueError - If the path is not a valid file or directory

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

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 re-created from scratch. Note that unlike CdWriter, DvdWriter does not blank rewritable media before reusing it; however, growisofs is called such that the media will be re-initialized as needed.

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 and the value passed in to initializeImage() will apply instead.

The writeMulti argument is ignored. It exists for compatibility with the Cedar Backup image writer interface.
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 disc should be re-initialized
           (type=Boolean true/false.)
writeMulti - Unused
           (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

Note: The image size indicated in the log ("Image size will be...") is an estimate. The estimate is conservative and is probably larger than the actual space that dvdwriter will use.

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.

This is an estimate and is conservative. The actual image could be as much as 450 blocks (sectors) smaller under some circmstances.
Returns:
Estimated size of the image, in bytes.
Raises:
IOError - If there is a problem calling mkisofs.
ValueError - If initializeImage() was not previously called

_writeImage(self, newDisc, imagePath, entries, mediaLabel=None)

Writes an image to disc using either an entries list or an ISO image on disk.

Callers are assumed to have done validation on paths, etc. before calling this method.
Parameters:
newDisc - Indicates whether the disc should be re-initialized
imagePath - Path to an ISO image on disk, or c{None} to use entries
entries - Mapping from path to graft point, or None to use imagePath
Raises:
IOError - If the media could not be written to for some reason.

_getDevice(self)

Property target used to get the device value.

_getDeviceCanEject(self)

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

_getDeviceHasTray(self)

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

_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.

_retrieveSectorsUsed(self)

Retrieves the number of sectors used on the current media.

This is a little ugly. We need to call growisofs in "dry-run" mode and parse some information from its output. However, to do that, we need to create a dummy file that we can pass to the command -- and we have to make sure to remove it later.

Once growisofs has been run, then we call _parseSectorsUsed to parse the output and calculate the number of sectors used on the media.
Returns:
Number of sectors used on the media

Static Method Details

_getEstimatedImageSize(entries)

Gets the estimated size of a set of image entries.

This is implemented in terms of the IsoImage class. The returned value is calculated by adding a "fudge factor" to the value from IsoImage. This fudge factor was determined by experimentation and is conservative -- the actual image could be as much as 450 blocks smaller under some circumstances.
Parameters:
entries - Dictionary mapping path to graft point.
Returns:
Total estimated size of image, in bytes.
Raises:
ValueError - If there are no entries in the dictionary
ValueError - If any path in the dictionary does not exist
IOError - If there is a problem calling mkisofs.

_searchForOverburn(output)

Search for an "overburn" error message in growisofs output.

The growisofs command returns a non-zero exit code and puts a message into the output -- even on a dry run -- if there is not enough space on the media. This is called an "overburn" condition.

The error message looks like this:
  :-( /dev/cdrom: 894048 blocks are free, 2033746 to be written!
This method looks for the overburn error message anywhere in the output. If a matching error message is found, an IOError exception is raised containing relevant information about the problem. Otherwise, the method call returns normally.
Parameters:
output - List of output lines to search, as from executeCommand
Raises:
IOError - If an overburn condition is found.

_buildWriteArgs(newDisc, hardwareId, driveSpeed, imagePath, entries, mediaLabel=None, dryRun=False)

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

The arguments will either cause growisofs to write the indicated image file to disc, or will pass growisofs a list of directories or files that should be written to disc.

If a new image is created, it will always be created with Rock Ridge extensions (-r). A volume name will be applied (-V) if mediaLabel is not None.
Parameters:
newDisc - Indicates whether the disc should be re-initialized
hardwareId - Hardware id for the device
driveSpeed - Speed at which the drive writes.
imagePath - Path to an ISO image on disk, or c{None} to use entries
entries - Mapping from path to graft point, or None to use imagePath
mediaLabel - Media label to set on the image, if any
dryRun - Says whether to make this a dry run (for checking capacity)
Returns:
List suitable for passing to util.executeCommand as args.
Raises:
ValueError - If caller does not pass one or the other of imagePath or entries.

Notes:

  • If we write an existing image to disc, then the mediaLabel is ignored. The media label is an attribute of the image, and should be set on the image when it is created.
  • We always pass the undocumented option -use-the-force-like=tty to growisofs. Without this option, growisofs will refuse to execute certain actions when running from cron. A good example is -Z, which happily overwrites an existing DVD from the command-line, but fails when run from cron. It took a while to figure that out, since it worked every time I tested it by hand. :(

_parseSectorsUsed(output)

Parse sectors used information out of growisofs output.

The first line of a growisofs run looks something like this:
  Executing 'mkisofs -C 973744,1401056 -M /dev/fd/3 -r -graft-points music4/=music | builtin_dd of=/dev/cdrom obs=32k seek=87566'

Dmitry has determined that the seek value in this line gives us information about how much data has previously been written to the media. That value multiplied by 16 yields the number of sectors used.

If the seek line cannot be found in the output, then sectors used of zero is assumed.
Returns:
Sectors used on the media, as a floating point number.
Raises:
ValueError - If the output cannot be parsed properly.

Property Details

device

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

scsiId

SCSI id for the device (saved for reference only).
Get Method:
_getScsiId(self)

hardwareId

Hardware id for this writer (always the 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)

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 Thu Mar 29 20:58:25 2007 http://epydoc.sf.net