def file_is_this_type(cls, path): """Detect whether the given file is an ISO image. Args: path (str): Path to file Returns: bool: True (file is an ISO) or False (file is not an ISO) Raises: HelperError: if ``path`` is not a file at all. """ if not os.path.exists(path): raise HelperError(2, "No such file or directory: '{0}'".format(path)) if helpers['isoinfo']: logger.debug("Using 'isoinfo' to check whether %s is an ISO", path) try: helpers['isoinfo'].call(['-i', path, '-d']) return 100 except HelperError: # Not an ISO return 0 # else, try to detect ISO files by file magic number with open(path, 'rb') as fileobj: for offset in (0x8001, 0x8801, 0x9001): fileobj.seek(offset) magic = fileobj.read(5).decode('ascii', 'ignore') if magic == "CD001": return 100 return 0
def file_is_this_type(cls, path): """Check if the given file is image type represented by this class. Args: path (str): Path to file to check. Returns: int: Confidence that this file matches. 0 is definitely not a match, 100 is definitely a match. Raises: HelperError: if no file exists at ``path``. """ if not os.path.exists(path): raise HelperError(2, "No such file or directory: '{0}'".format(path)) # Default implementation using qemu-img logger.debug("Using 'qemu-img' to check whether %s is a %s", path, cls.disk_format) output = helpers['qemu-img'].call(['info', path]) # Read the format from the output match = re.search(r"file format: (\S*)", output) if not match: raise RuntimeError("Did not find file format string in " "the output from qemu-img:\n{0}".format(output)) file_format = match.group(1) if file_format == cls.disk_format: return 100 else: return 0
def stub_install(package): """Fake successful or unsuccessful installation of tools.""" if package == "genisoimage": helpers['genisoimage']._path = "/usr/bin/genisoimage" helpers['genisoimage']._installed = True return raise HelperError(1, "not really installing!")
def __init__(self, path): """Create a representation of an existing disk. Args: path (str): Path to existing file. """ if not path: raise ValueError( "Path must be set to a valid value, but got {0}".format(path)) if not os.path.exists(path): raise HelperError(2, "No such file or directory: '{0}'".format(path)) self._path = path self._disk_subformat = None self._capacity = None self._files = None