COT.helpers.api
module¶
API for abstract access to third-party helper tools.
Abstracts away operations that require third-party helper programs, especially those that are not available through PyPI.
The actual helper programs are provided by individual classes in this package.
Functions
convert_disk_image |
Convert the given disk image to the requested format/subformat. |
create_disk_image |
Create a new disk image at the requested location. |
create_install_dir |
Check whether the given target directory exists, and create if not. |
download_and_expand |
Context manager for downloading and expanding a .tar.gz file. |
get_checksum |
Get the checksum of the given file. |
get_disk_capacity |
Get the storage capacity of the given disk image. |
get_disk_format |
Get the disk image format of the given file. |
install_file |
Copy the given src to the given dest, using sudo if needed. |
-
convert_disk_image
(file_path, output_dir, new_format, new_subformat=None)[source]¶ Convert the given disk image to the requested format/subformat.
If the disk is already in this format then it is unchanged; otherwise, will convert to a new disk in the specified output_dir and return its path.
Current supported conversions:
- .vmdk (any format) to .vmdk (streamOptimized)
- .img to .vmdk (streamOptimized)
Parameters: - file_path (str) – Disk image file to inspect/convert
- output_dir (str) – Directory to place converted image into, if needed
- new_format (str) – Desired final format
- new_subformat (str) – Desired final subformat
Returns: file_path
, if no conversion was required- or a file path in
output_dir
containing the converted image
Raises: ValueUnsupportedError – if the
new_format
and/ornew_subformat
are not supported conversion targets.
-
create_disk_image
(file_path, file_format=None, capacity=None, contents=None)[source]¶ Create a new disk image at the requested location.
Either
capacity
orcontents
or both must be specified.Parameters: - file_path (str) – Desired location of new disk image
- file_format (str) – Desired image format (if not specified, this will
be derived from the file extension of
file_path
) - capacity – Disk capacity. A string like ‘16M’ or ‘1G’.
- contents (list) – List of file paths to package into the created image. If not specified, the image will be left blank and unformatted.
-
create_install_dir
(directory, permissions=493)[source]¶ Check whether the given target directory exists, and create if not.
Parameters: - directory (str) – Directory to check/create.
- permissions (int) – Permissions bits to set on the directory.
Returns: True
-
download_and_expand
(*args, **kwds)[source]¶ Context manager for downloading and expanding a .tar.gz file.
Creates a temporary directory, downloads the specified URL into the directory, unzips and untars the file into this directory, then yields to the given block. When the block exits, the temporary directory and its contents are deleted.
with download_and_expand("http://example.com/foo.tgz") as d: # archive contents have been extracted to 'd' ... # d is automatically cleaned up.
Parameters: url (str) – URL of a .tgz or .tar.gz file to download.
-
get_checksum
(path_or_obj, checksum_type)[source]¶ Get the checksum of the given file.
Parameters: - path_or_obj (str) – File path to checksum OR an opened file object
- checksum_type (str) – Supported values are ‘md5’ and ‘sha1’.
Returns: String containing hexadecimal file checksum
-
get_disk_capacity
(file_path)[source]¶ Get the storage capacity of the given disk image.
Parameters: file_path (str) – Path to disk image file to inspect Returns: Disk capacity, in bytes
-
get_disk_format
(file_path)[source]¶ Get the disk image format of the given file.
Warning
If
file_path
refers to a file which is not a disk image at all, this function will return('raw', None)
.Parameters: file_path (str) – Path to disk image file to inspect. Returns: (format, subformat)
format
may be'vmdk'
,'raw'
, or'qcow2'
subformat
may beNone
, or various strings for'vmdk'
files.