CedarBackup3.writers.dvdwriter
¶
Provides functionality related to DVD writer devices.
Module Attributes¶
- CedarBackup3.writers.dvdwriter.MEDIA_DVDPLUSR¶
Constant representing DVD+R media
- CedarBackup3.writers.dvdwriter.MEDIA_DVDPLUSRW¶
Constant representing DVD+RW media
- author
Kenneth J. Pronovici <pronovic@ieee.org>
- author
Dmitry Rutsky <rutsky@inbox.ru>
Module Contents¶
- CedarBackup3.writers.dvdwriter.logger¶
- CedarBackup3.writers.dvdwriter.MEDIA_DVDPLUSR = 1¶
- CedarBackup3.writers.dvdwriter.MEDIA_DVDPLUSRW = 2¶
- CedarBackup3.writers.dvdwriter.GROWISOFS_COMMAND = ['growisofs']¶
- CedarBackup3.writers.dvdwriter.EJECT_COMMAND = ['eject']¶
- class CedarBackup3.writers.dvdwriter.MediaDefinition(mediaType)¶
Bases:
object
Class encapsulating information about DVD media definitions.
The following media types are accepted:
MEDIA_DVDPLUSR
: DVD+R media (4.4 GB capacity)MEDIA_DVDPLUSRW
: DVD+RW media (4.4 GB capacity)
Note that the capacity attribute returns capacity in terms of ISO sectors (
util.ISO_SECTOR_SIZE)
. This is for compatibility with the CD writer functionality.The capacities are 4.4 GB because Cedar Backup deals in “true” gigabytes of 1024*1024*1024 bytes per gigabyte.
- mediaType¶
- rewritable¶
- capacity¶
- class CedarBackup3.writers.dvdwriter.MediaCapacity(bytesUsed, bytesAvailable)¶
Bases:
object
Class encapsulating information about DVD media capacity.
Space used and space available do not include any information about media lead-in or other overhead.
- bytesUsed¶
- bytesAvailable¶
- totalCapacity¶
- utilized¶
- __str__()¶
Informal string representation for class instance.
- class CedarBackup3.writers.dvdwriter.DvdWriter(device, scsiId=None, driveSpeed=None, mediaType=MEDIA_DVDPLUSRW, noEject=False, refreshMediaDelay=0, ejectDelay=0, unittest=False)¶
Bases:
object
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
andgrowisofs
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
, theDvdWriter
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:
MEDIA_DVDPLUSR
: DVD+R media (4.4 GB capacity)MEDIA_DVDPLUSRW
: DVD+RW media (4.4 GB capacity)
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 tocdrecord
.Media Capacity
One major difference between the
cdrecord
/mkisofs
utilities used by the cdwriter class and thegrowisofs
utility used here is that the process of estimating remaining capacity and image size is more straightforward withcdrecord
/mkisofs
than withgrowisofs
.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 theIsoImage
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.
- device¶
- scsiId¶
- hardwareId¶
- driveSpeed¶
- media¶
- deviceHasTray¶
- deviceCanEject¶
- refreshMediaDelay¶
- ejectDelay¶
- isRewritable()¶
Indicates whether the media is rewritable per configuration.
- retrieveCapacity(entireDisc=False)¶
Retrieves capacity for the current media in terms of a
MediaCapacity
object.If
entireDisc
is passed in asTrue
, 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 bygrowisofs
itself.- Parameters
entireDisc (Boolean true/false) – Indicates whether to return capacity for entire disc
- Returns
MediaCapacity
object describing the capacity of the media- Raises
ValueError – If there is a problem parsing the
growisofs
outputIOError – If the media could not be read for some reason
- initializeImage(newDisc, tmpdir, mediaLabel=None)¶
Initializes the writer’s associated ISO image.
This method initializes the
image
instance variable so that the caller can use theaddImageEntry
method. Once entries have been added, thewriteImage
method can be called with no arguments.- Parameters
newDisc (Boolean true/false) – Indicates whether the disc should be re-initialized
tmpdir (String representing a directory path on disk) – Temporary directory to use if needed
mediaLabel (String, no more than 25 characters long) – Media label to be applied to the image, if any
- addImageEntry(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
.Note: Before calling this method, you must call
initializeImage
.- Parameters
path (String representing a path on disk) – File or directory to be added to the image
graftPoint (String representing a graft point path, as described above) – Graft point to be used when adding this entry
- Raises
ValueError – If initializeImage() was not previously called
ValueError – If the path is not a valid file or directory
- setImageNewDisc(newDisc)¶
Resets (overrides) the newDisc flag on the internal image. :param newDisc: New disc flag to set
- Raises
ValueError – If initializeImage() was not previously called
- getEstimatedImageSize()¶
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
- openTray()¶
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.
Starting with Debian wheezy on my backup hardware, I started seeing consistent problems with the eject command. I couldn’t tell whether these problems were due to the device management system or to the new kernel (3.2.0). Initially, I saw simple eject failures, possibly because I was opening and closing the tray too quickly. I worked around that behavior with the new ejectDelay flag.
Later, I sometimes ran into issues after writing an image to a disc: eject would give errors like “unable to eject, last error: Inappropriate ioctl for device”. Various sources online (like Ubuntu bug #875543) suggested that the drive was being locked somehow, and that the workaround was to run ‘eject -i off’ to unlock it. Sure enough, that fixed the problem for me, so now it’s a normal error-handling strategy.
- Raises
IOError – If there is an error talking to the device
- unlockTray()¶
Unlocks the device’s tray via ‘eject -i off’. :raises IOError: If there is an error talking to the device
- closeTray()¶
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()¶
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. (There are also configurable eject and refresh media delays which can be applied, for situations where this makes a difference.)
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. The configured delays still apply, though.
- Raises
IOError – If there is an error talking to the device
- writeImage(imagePath=None, newDisc=False, writeMulti=True)¶
Writes an ISO image to the media in the device.
If
newDisc
is passed in asTrue
, we assume that the entire disc will be re-created from scratch. Note that unlikeCdWriter
,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 asNone
, then the existing image configured withinitializeImage()
will be used. Under these circumstances, the passed-innewDisc
flag will be ignored and the value passed in toinitializeImage()
will apply instead.The
writeMulti
argument is ignored. It exists for compatibility with the Cedar Backup image writer interface.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.- Parameters
imagePath (String representing a path on disk) – Path to an ISO image on disk, or
None
to use writer’s imagenewDisc (Boolean true/false) – Indicates whether the disc should be re-initialized
writeMulti (Boolean true/false) – Unused
- 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