COT.file_reference module

Wrapper classes to abstract away differences between file sources.

Classes

FileReference Semi-abstract base class for file references.
FileOnDisk Wrapper for a 'real' file on disk.
FileInTAR Wrapper for a file inside a TAR archive or OVA.
class FileInTAR(tarfile_path, filename, **kwargs)[source]

Bases: COT.file_reference.FileReference

Wrapper for a file inside a TAR archive or OVA.

__init__(tarfile_path, filename, **kwargs)[source]

Create a reference to a file contained in a TAR archive.

Parameters:
  • tarfile_path (str) -- Path to TAR archive to read
  • filename (str) -- File name in the TAR archive.
  • **kwargs -- Passed through to FileReference.__init__().
Raises:

IOError -- if tarfile_path doesn't reference a TAR file, or the TAR file does not contain filename.

add_to_archive(tarf)[source]

Copy this file into the given tarfile object.

Parameters:tarf (tarfile.TarFile) -- Add this file to that archive.
copy_to(dest_dir)[source]

Extract this file to the given destination directory.

Parameters:dest_dir (str) -- Destination directory or filename.
open(**kwds)[source]

Open the TAR and return a reference to the relevant file object.

Parameters:mode (str) -- Only 'r' and 'rb' modes are supported.
Yields:file -- File object
Raises:ValueError -- if mode is not valid.
exists

True if the file exists in the TAR archive, else False.

size

The size of this file in bytes.

class FileOnDisk(container_path, filename, checksum_algorithm=None, expected_checksum=None, expected_size=None)[source]

Bases: COT.file_reference.FileReference

Wrapper for a 'real' file on disk.

add_to_archive(tarf)[source]

Copy this file into the given tarfile object.

Parameters:tarf (tarfile.TarFile) -- Add this file to that archive.
copy_to(dest_dir)[source]

Copy this file to the given destination directory.

Parameters:dest_dir (str) -- Destination directory or filename.
open(**kwds)[source]

Open the file and return a reference to the file object.

Parameters:mode (str) -- Mode such as 'r', 'w', 'a', 'w+', etc.
Yields:file -- File object
exists

True if the file exists on disk, else False.

file_path

Directory + filename.

size

The size of this file, in bytes.

class FileReference(container_path, filename, checksum_algorithm=None, expected_checksum=None, expected_size=None)[source]

Bases: object

Semi-abstract base class for file references.

__init__(container_path, filename, checksum_algorithm=None, expected_checksum=None, expected_size=None)[source]

Common initialization and validation logic.

Parameters:
  • container_path (str) -- Path to container (directory, TAR file, etc.)
  • filename (str) -- Relative path within the container to the file itself
  • checksum_algorithm (str) -- 'sha1', 'sha256', etc.
  • expected_checksum (str) -- Expected checksum of the file, if any.
  • expected_size (int) -- Expected size of the file, in bytes, if any.
Raises:

IOError -- if the file does not actually exist or is not readable.

classmethod create(container_path, filename, **kwargs)[source]

Create a reference to a file in a container of some sort.

Parameters:
  • container_path (str) -- Absolute path to a container such as a directory or a TAR file.
  • filename (str) -- Name of file within the container in question.
  • **kwargs -- See :meth:__init__()
Returns:

FileReference -- instance of appropriate subclass

open(**kwds)[source]

Open the file and yield a reference to the file object.

Automatically closes the file when done. Some subclasses may not support all modes.

Parameters:mode (str) -- Mode such as 'r', 'w', 'a', 'w+', etc.
Yields:file -- File object
refresh()[source]

Make sure all information in this reference is still valid.

checksum

Checksum of the referenced file.

exists

Report whether this file actually exists.

file_path

Actual path to a real file, if any.

size

Size of the referenced file, in bytes.