[virt-tools-list] [virt-bootstrap] [PATCH v6 08/26] Make get_compression_type() reusable

Cedric Bosdonnat cbosdonnat at suse.com
Sat Aug 19 12:34:30 UTC 2017


On Thu, 2017-08-17 at 10:39 +0100, Radostin Stoyanov wrote:
> Take out the code for getting compression type of tarball
> in separate function.
> ---
>  src/virtBootstrap/utils.py | 41 +++++++++++++++++++++++++++--------------
>  1 file changed, 27 insertions(+), 14 deletions(-)
> 
> diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
> index 48a931e..1d642bc 100644
> --- a/src/virtBootstrap/utils.py
> +++ b/src/virtBootstrap/utils.py
> @@ -51,6 +51,30 @@ else:
>      DEFAULT_IMG_DIR += "/.local/share/virt-bootstrap/docker_images"
>  
>  
> +def get_compression_type(tar_file):
> +    """
> +    Get compression type of tar file.
> +    """
> +    # Get mime type of archive
> +    mime_tar_file = get_mime_type(tar_file)
> +    logger.debug("Detected mime type of archive: %s", mime_tar_file)
> +
> +    compression_fmts = {
> +        'x-gzip': 'gzip',
> +        'gzip': 'gzip',
> +        'x-xz': 'xz',
> +        'x-bzip2': 'bzip2',
> +        'x-compress': 'compress',
> +        'x-lzop': 'lzop'
> +    }
> +
> +    # Check if tarball is compressed
> +    mime_type, mime_subtype = mime_tar_file.split('/')
> +    if mime_type == 'application' and mime_subtype in compression_fmts:
> +        return compression_fmts[mime_subtype]
> +    return None
> +
> +
>  def checksum(path, sum_type, sum_expected):
>      """
>      Validate file using checksum.
> @@ -209,27 +233,16 @@ def create_qcow2(tar_file, layer_file, backing_file=None, size=DEF_QCOW2_SIZE):
>          logger.info("Creating qcow2 image with backing chain")
>          execute(qemu_img_cmd)
>  
> -    # Get mime type of archive
> -    mime_tar_file = get_mime_type(tar_file)
> -    logger.debug("Detected mime type of archive: %s", mime_tar_file)
> -
>      # Extract tarball using "tar-in" command from libguestfs
>      tar_in_cmd = ["guestfish",
>                    "-a", layer_file,
>                    '-m', '/dev/sda',
>                    'tar-in', tar_file, "/"]
>  
> -    compression_fmts = {'x-gzip': 'gzip', 'gzip': 'gzip',
> -                        'x-xz': 'xz',
> -                        'x-bzip2': 'bzip2',
> -                        'x-compress': 'compress',
> -                        'x-lzop': 'lzop'}
> -
>      # Check if tarball is compressed
> -    mime_parts = mime_tar_file.split('/')
> -    if mime_parts[0] == 'application' and \
> -       mime_parts[1] in compression_fmts:
> -        tar_in_cmd.append('compress:' + compression_fmts[mime_parts[1]])
> +    compression = get_compression_type(tar_file)
> +    if compression is not None:
> +        tar_in_cmd.append('compress:' + compression)
>  
>      # Execute virt-tar-in command
>      execute(tar_in_cmd)

ACK

--
Cedric




More information about the virt-tools-list mailing list