# Path: lava_dispatcher/action.py
# class InfrastructureError(LAVAError):
#     """
#     Exceptions based on an error raised by a component of the
#     test which is neither the LAVA dispatcher code nor the
#     code being executed on the device under test. This includes
#     errors arising from the device (like the arndale SD controller
#     issue) and errors arising from the hardware to which the device
#     is connected (serial console connection, ethernet switches or
#     internet connection beyond the control of the device under test).
# 
#     Use LAVABug for errors arising from bugs in LAVA code.
#     """
#     error_help = "InfrastructureError: The Infrastructure is not working " \
#                  "correctly. Please report this error to LAVA admins."
#     error_type = "Infrastructure"
# 
# class JobError(LAVAError):
#     """
#     An Error arising from the information supplied as part of the TestJob
#     e.g. HTTP404 on a file to be downloaded as part of the preparation of
#     the TestJob or a download which results in a file which tar or gzip
#     does not recognise.
#     """
#     error_help = "JobError: Your job cannot terminate cleanly."
#     error_type = "Job"
# 
# class LAVABug(LAVAError):
#     """
#     An error that is raised when an un-expected error is catched. Only happen
#     when a bug is encountered.
#     """
#     error_help = "LAVABug: This is probably a bug in LAVA, please report it."
#     error_type = "Bug"
# 
# Path: lava_dispatcher/utils/constants.py
# LXC_PATH = "/var/lib/lxc"
# 
# LAVA_LXC_HOME = "/lava-lxc"
# 
# Path: lava_dispatcher/utils/compression.py
# def decompress_file(infile, compression):
#     if not compression:
#         return infile
#     if compression not in decompress_command_map.keys():
#         raise JobError("Cannot find shell command to decompress: %s" % compression)
# 
#     # Check that the command does exists
#     which(decompress_command_map[compression])
# 
#     with chdir(os.path.dirname(infile)):
#         cmd = "%s %s" % (decompress_command_map[compression], infile)
#         outfile = infile
#         if infile.endswith(compression):
#             outfile = infile[:-(len(compression) + 1)]
#         try:
#             # safe to use shell=True here, no external arguments
#             subprocess.check_output(cmd, shell=True)
#             return outfile
#         except (OSError, subprocess.CalledProcessError) as exc:
#             raise InfrastructureError('unable to decompress file %s: %s' % (infile, exc))
# 
# Path: lava_dispatcher/utils/decorator.py
# def replace_exception(cls_from, cls_to):
#     def replace_exception_wrapper(func):
#         @wraps(func)
#         def function_wrapper(*args, **kwargs):
#             try:
#                 return func(*args, **kwargs)
#             except cls_from as exc:
#                 raise cls_to(exc)
#         return function_wrapper
#     return replace_exception_wrapper


import atexit
import os
import shutil
import tarfile
import tempfile
import guestfs
import subprocess
import glob
import logging
import magic
import errno
from configobj import ConfigObj
from lava_dispatcher.action import InfrastructureError, JobError, LAVABug
from lava_dispatcher.utils.constants import (
    LXC_PATH,
    LAVA_LXC_HOME,
)
from lava_dispatcher.utils.compression import decompress_file
from lava_dispatcher.utils.decorator import replace_exception
    return guest.blkid(guest_device)['UUID']


@replace_exception(RuntimeError, JobError)
def prepare_install_base(output, size):
    """
    Create an empty image of the specified size (in bytes),
    ready for an installer to partition, create filesystem(s)
    and install files.
    """
    guest = guestfs.GuestFS(python_return_dict=True)
    guest.disk_create(output, "raw", size)
    guest.add_drive_opts(output, format="raw", readonly=False)
    guest.launch()
    devices = guest.list_devices()
    if len(devices) != 1:
        raise InfrastructureError("Unable to prepare guestfs")
    guest.shutdown()


@replace_exception(RuntimeError, JobError)
def copy_out_files(image, filenames, destination):
    """
    Copies a list of files out of the image to the specified
    destination which must exist. Launching the guestfs is
    expensive, so copy out all files in one operation. The
    filenames list must contain unique filenames even if the
    source files exist in separate directories.
    """
    if not isinstance(filenames, list):

# Path: lava_dispatcher/action.py
# class InfrastructureError(LAVAError):
#     """
#     Exceptions based on an error raised by a component of the
#     test which is neither the LAVA dispatcher code nor the
#     code being executed on the device under test. This includes
#     errors arising from the device (like the arndale SD controller
#     issue) and errors arising from the hardware to which the device
#     is connected (serial console connection, ethernet switches or
#     internet connection beyond the control of the device under test).
# 
#     Use LAVABug for errors arising from bugs in LAVA code.
#     """
#     error_help = "InfrastructureError: The Infrastructure is not working " \
#                  "correctly. Please report this error to LAVA admins."
#     error_type = "Infrastructure"
# 
# class JobError(LAVAError):
#     """
#     An Error arising from the information supplied as part of the TestJob
#     e.g. HTTP404 on a file to be downloaded as part of the preparation of
#     the TestJob or a download which results in a file which tar or gzip
#     does not recognise.
#     """
#     error_help = "JobError: Your job cannot terminate cleanly."
#     error_type = "Job"
# 
# class LAVABug(LAVAError):
#     """
#     An error that is raised when an un-expected error is catched. Only happen
#     when a bug is encountered.
#     """
#     error_help = "LAVABug: This is probably a bug in LAVA, please report it."
#     error_type = "Bug"
# 
# Path: lava_dispatcher/utils/constants.py
# LXC_PATH = "/var/lib/lxc"
# 
# LAVA_LXC_HOME = "/lava-lxc"
# 
# Path: lava_dispatcher/utils/compression.py
# def decompress_file(infile, compression):
#     if not compression:
#         return infile
#     if compression not in decompress_command_map.keys():
#         raise JobError("Cannot find shell command to decompress: %s" % compression)
# 
#     # Check that the command does exists
#     which(decompress_command_map[compression])
# 
#     with chdir(os.path.dirname(infile)):
#         cmd = "%s %s" % (decompress_command_map[compression], infile)
#         outfile = infile
#         if infile.endswith(compression):
#             outfile = infile[:-(len(compression) + 1)]
#         try:
#             # safe to use shell=True here, no external arguments
#             subprocess.check_output(cmd, shell=True)
#             return outfile
#         except (OSError, subprocess.CalledProcessError) as exc:
#             raise InfrastructureError('unable to decompress file %s: %s' % (infile, exc))
# 
# Path: lava_dispatcher/utils/decorator.py
# def replace_exception(cls_from, cls_to):
#     def replace_exception_wrapper(func):
#         @wraps(func)
#         def function_wrapper(*args, **kwargs):
#             try:
#                 return func(*args, **kwargs)
#             except cls_from as exc:
#                 raise cls_to(exc)
#         return function_wrapper
#     return replace_exception_wrapper


# Path: lava_dispatcher/utils/filesystem.py
import atexit
import os
import shutil
import tarfile
import tempfile
import guestfs
import subprocess
import glob
import logging
import magic
import errno
from configobj import ConfigObj
from lava_dispatcher.action import InfrastructureError, JobError, LAVABug
from lava_dispatcher.utils.constants import (
    LXC_PATH,
    LAVA_LXC_HOME,
)
from lava_dispatcher.utils.compression import decompress_file
from lava_dispatcher.utils.decorator import replace_exception

    return guest.blkid(guest_device)['UUID']


@replace_exception(RuntimeError, JobError)
def prepare_install_base(output, size):
    """
    Create an empty image of the specified size (in bytes),
    ready for an installer to partition, create filesystem(s)
    and install files.
    """
    guest = guestfs.GuestFS(python_return_dict=True)
    guest.disk_create(output, "raw", size)
    guest.add_drive_opts(output, format="raw", readonly=False)
    guest.launch()
    devices = guest.list_devices()
    if len(devices) != 1:
        raise InfrastructureError("Unable to prepare guestfs")
    guest.shutdown()


@replace_exception(RuntimeError, JobError)
def copy_out_files(image, filenames, destination):
    """
    Copies a list of files out of the image to the specified
    destination which must exist. Launching the guestfs is
    expensive, so copy out all files in one operation. The
    filenames list must contain unique filenames even if the
    source files exist in separate directories.
    """
    if not isinstance(filenames, list):

        raise LAVABug('filenames must be a list')