Class CdWriter
source code
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:
-
MEDIA_CDR_74
: 74-minute CD-R media (650 MB capacity)
-
MEDIA_CDRW_74
: 74-minute CD-RW media (650 MB capacity)
-
MEDIA_CDR_80
: 80-minute CD-R media (700 MB capacity)
-
MEDIA_CDRW_80
: 80-minute CD-RW media (700 MB capacity)
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.
|
__init__(self,
device,
scsiId=None,
driveSpeed=None,
mediaType=1,
noEject=False,
refreshMediaDelay=0,
unittest=False)
Initializes a CD writer object. |
source code
|
|
|
isRewritable(self)
Indicates whether the media is rewritable per configuration. |
source code
|
|
|
|
|
retrieveCapacity(self,
entireDisc=False,
useMulti=True)
Retrieves capacity for the current media in terms of a
MediaCapacity object. |
source code
|
|
|
|
|
|
|
|
|
refreshMedia(self)
Opens and then immediately closes the device's tray, to refresh the
device's idea of the media. |
source code
|
|
|
writeImage(self,
imagePath=None,
newDisc=False,
writeMulti=True)
Writes an ISO image to the media in the device. |
source code
|
|
|
|
|
|
|
|
|
|
|
|
|
_getDevice(self)
Property target used to get the device value. |
source code
|
|
|
_getScsiId(self)
Property target used to get the SCSI id value. |
source code
|
|
|
_getHardwareId(self)
Property target used to get the hardware id value. |
source code
|
|
|
_getDriveSpeed(self)
Property target used to get the drive speed. |
source code
|
|
|
_getMedia(self)
Property target used to get the media description. |
source code
|
|
|
_getDeviceType(self)
Property target used to get the device type. |
source code
|
|
|
_getDeviceVendor(self)
Property target used to get the device vendor. |
source code
|
|
|
_getDeviceId(self)
Property target used to get the device id. |
source code
|
|
|
_getDeviceBufferSize(self)
Property target used to get the device buffer size. |
source code
|
|
|
_getDeviceSupportsMulti(self)
Property target used to get the device-support-multi flag. |
source code
|
|
|
_getDeviceHasTray(self)
Property target used to get the device-has-tray flag. |
source code
|
|
|
_getDeviceCanEject(self)
Property target used to get the device-can-eject flag. |
source code
|
|
|
_getRefreshMediaDelay(self)
Property target used to get the configured refresh media delay, in
seconds. |
source code
|
|
|
|
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_buildWriteArgs(hardwareId,
imagePath,
driveSpeed=None,
writeMulti=True)
Builds a list of arguments to be passed to a cdrecord
command. |
source code
|
|
|
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.
|
|
refreshMediaDelay
Refresh media delay, in seconds.
|
Inherited from object :
__class__
|
__init__(self,
device,
scsiId=None,
driveSpeed=None,
mediaType=1,
noEject=False,
refreshMediaDelay=0,
unittest=False)
(Constructor)
| source code
|
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 (Absolute path to a filesystem device, i.e. /dev/cdrw ) - Filesystem device associated with this writer.
scsiId (If provided, SCSI id in the form
[<method>:]scsibus,target,lun ) - SCSI id for the device (optional).
driveSpeed (Use 2 for 2x device, etc. or None to
use device default.) - Speed at which the drive writes.
mediaType (One of the valid media type as discussed above.) - Type of the media that is assumed to be in the drive.
noEject (Boolean true/false) - Overrides properties to indicate that the device does not support
eject.
refreshMediaDelay (Number of seconds, an integer >= 0) - Refresh media delay to use, if any
unittest (Boolean true/false) - Turns off certain validations, for use in unit testing.
- 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:
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.
|
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)
| source code
|
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 (Boolean true/false) - Indicates whether to return capacity for entire disc.
useMulti (Boolean true/false) - Indicates whether a multisession disc should be assumed, if
possible.
- 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)
| source code
|
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 (Boolean true/false) - Indicates whether to return capacity for entire disc.
useMulti (Boolean true/false) - Indicates whether a multisession disc should be assumed, if
possible.
- Returns:
- Boundaries tuple or
None , as described above.
- Raises:
IOError - If the media could not be read for some reason.
|
_calculateCapacity(media,
boundaries)
Static Method
| source code
|
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.
|
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.
|
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.
|
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 is also a configurable refresh media delay
which can be applied after the tray is closed, 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 delay still applies, though.
- Raises:
IOError - If there is an error talking to the device.
|
writeImage(self,
imagePath=None,
newDisc=False,
writeMulti=True)
| source code
|
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 (String representing a path on disk) - Path to an ISO image on disk, or None to use
writer's image
newDisc (Boolean true/false.) - Indicates whether the entire disc will overwritten.
writeMulti (Boolean true/false) - Indicates whether a multisession disc should be written, if
possible.
- 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
|
Blanks the media in the device, if the media is rewritable.
- Raises:
IOError - If the media could not be written to for some reason.
|
_parsePropertiesOutput(output)
Static Method
| source code
|
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)
Static Method
| source code
|
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 .
|
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 .
|
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)
Static Method
| source code
|
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)
Static Method
| source code
|
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)
Static Method
| source code
|
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)
Static Method
| source code
|
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 .
|
initializeImage(self,
newDisc,
tmpdir,
mediaLabel=None)
| source code
|
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 (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
|
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 (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
|
Resets (overrides) the newDisc flag on the internal image.
- Parameters:
newDisc - New disc flag to set
- Raises:
ValueError - If initializeImage() was not previously called
|
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
|
Creates an ISO image based on configuration in self._image.
- Returns:
- Path to the newly-created ISO image on disk.
- Raises:
IOError - If there is an error writing the image to disk.
ValueError - If there are no filesystem entries in the image
ValueError - If a path cannot be encoded properly.
|
_writeImage(self,
imagePath,
writeMulti,
newDisc)
| source code
|
Write an ISO image to disc using cdrecord. The disc is blanked first
if newDisc is True .
- Parameters:
imagePath - Path to an ISO image on disk
writeMulti - Indicates whether a multisession disc should be written, if
possible.
newDisc - Indicates whether the entire disc will overwritten.
|
device
Filesystem device name for this writer.
- Get Method:
- _getDevice(self)
- Property target used to get the device value.
|
scsiId
SCSI id for the device, in the form
[<method>:]scsibus,target,lun .
- Get Method:
- _getScsiId(self)
- Property target used to get the SCSI id value.
|
hardwareId
Hardware id for this writer, either SCSI id or device path.
- Get Method:
- _getHardwareId(self)
- Property target used to get the hardware id value.
|
driveSpeed
Speed at which the drive writes.
- Get Method:
- _getDriveSpeed(self)
- Property target used to get the drive speed.
|
media
Definition of media that is expected to be in the device.
- Get Method:
- _getMedia(self)
- Property target used to get the media description.
|
deviceType
Type of the device, as returned from cdrecord -prcap .
- Get Method:
- _getDeviceType(self)
- Property target used to get the device type.
|
deviceVendor
Vendor of the device, as returned from cdrecord
-prcap .
- Get Method:
- _getDeviceVendor(self)
- Property target used to get the device vendor.
|
deviceId
Device identification, as returned from cdrecord
-prcap .
- Get Method:
- _getDeviceId(self)
- Property target used to get the device id.
|
deviceBufferSize
Size of the device's write buffer, in bytes.
- Get Method:
- _getDeviceBufferSize(self)
- Property target used to get the device buffer size.
|
deviceSupportsMulti
Indicates whether device supports multisession discs.
- Get Method:
- _getDeviceSupportsMulti(self)
- Property target used to get the device-support-multi flag.
|
deviceHasTray
Indicates whether the device has a media tray.
- Get Method:
- _getDeviceHasTray(self)
- Property target used to get the device-has-tray flag.
|
deviceCanEject
Indicates whether the device supports ejecting its media.
- Get Method:
- _getDeviceCanEject(self)
- Property target used to get the device-can-eject flag.
|
refreshMediaDelay
Refresh media delay, in seconds.
- Get Method:
- _getRefreshMediaDelay(self)
- Property target used to get the configured refresh media delay, in
seconds.
|