COT.file_reference module

Wrapper classes to abstract away differences between file sources.

class FileInTAR(tarfile_path, filename)[source]

Bases: object

Wrapper for a file inside a TAR archive or OVA.

__init__(tarfile_path, filename)[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.
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.
close()[source]

Close the file object previously opened.

copy_to(dest_dir)[source]

Extract this file to the given destination directory.

Parameters:dest_dir (str) – Destination directory or filename.
open(mode)[source]

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

Parameters:mode (str) – Only ‘r’ and ‘rb’ modes are supported.
Returns: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(file_path, filename=None)[source]

Bases: object

Wrapper for a ‘real’ file on disk.

__init__(file_path, filename=None)[source]

Create a reference to a file on disk.

Parameters:
  • file_path (str) – File path or directory path
  • filename (str) – If specified, file_path is considered to be a directory containing this filename. If not specified, the final element in file_path is considered the filename.
Raises:

IOError – if no such file exists

Examples

>>> a = FileOnDisk('/etc/resolv.conf')
>>> b = FileOnDisk('/etc', 'resolv.conf')
>>> a == b
True
add_to_archive(tarf)[source]

Copy this file into the given tarfile object.

Parameters:tarf (tarfile.TarFile) – Add this file to that archive.
close()[source]

Close the file previously opened.

copy_to(dest_dir)[source]

Copy this file to the given destination directory.

Parameters:dest_dir (str) – Destination directory or filename.
open(mode)[source]

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

Parameters:mode (str) – Mode such as ‘r’, ‘w’, ‘a’, ‘w+’, etc.
Returns:file – File object
exists

True if the file exists on disk, else False.

size

The size of this file, in bytes.