def _ShouldSign(self, image):
        """Whether to sign the image.

    Args:
      image: an image object.

    Returns:
      True if to sign the image, false if not to sign the image.
    """
        return gspaths.IsImage(image) or gspaths.IsDLCImage(image)
    def _PrepareImage(self, image, image_file):
        """Download an prepare an image for delta generation.

    Preparation includes downloading, extracting and converting the image into
    an on-disk format, as necessary.

    Args:
      image: an object representing the image we're processing, either
             UnsignedImageArchive or Image type from gspaths module.
      image_file: file into which the prepared image should be copied.
    """

        logging.info('Preparing image from %s as %s', image.uri, image_file)

        # Figure out what we're downloading and how to handle it.
        image_handling_by_type = {
            'signed': (None, True),
            'test': (self.TEST_IMAGE_NAME, False),
            'recovery': (self.RECOVERY_IMAGE_NAME, True),
            'base': (self.BASE_IMAGE_NAME, True),
        }
        if gspaths.IsImage(image):
            # No need to extract.
            extract_file = None
        elif gspaths.IsUnsignedImageArchive(image):
            extract_file, _ = image_handling_by_type[image.get(
                'image_type', 'signed')]
        else:
            raise Error('Unknown image type %s' % type(image))

        # Are we donwloading an archive that contains the image?
        if extract_file:
            # Archive will be downloaded to a temporary location.
            with tempfile.NamedTemporaryFile(prefix='image-archive-',
                                             suffix='.tar.xz',
                                             dir=self.work_dir,
                                             delete=False) as temp_file:
                download_file = temp_file.name
        else:
            download_file = image_file

        # Download the image file or archive.
        self.cache.GetFileCopy(image.uri, download_file)

        # If we downloaded an archive, extract the image file from it.
        if extract_file:
            cmd = ['tar', '-xJf', download_file, extract_file]
            cros_build_lib.RunCommand(cmd, cwd=self.work_dir)

            # Rename it into the desired image name.
            shutil.move(os.path.join(self.work_dir, extract_file), image_file)

            # It's safe to delete the archive at this point.
            os.remove(download_file)
def _DefaultPayloadUri(payload, random_str=None):
    """Compute the default output URI for a payload.

  For a glob that matches all potential URIs for this
  payload, pass in a random_str of '*'.

  Args:
    payload: gspaths.Payload instance.
    random_str: A hook to force a specific random_str. None means generate it.

  Returns:
    Default URI for the payload.
  """
    src_version = None
    if payload.src_image:
        src_version = payload.src_image.build.version

    if gspaths.IsDLCImage(payload.tgt_image):
        # Signed DLC payload.
        return gspaths.ChromeosReleases.DLCPayloadUri(
            payload.build,
            random_str=random_str,
            dlc_id=payload.tgt_image.dlc_id,
            dlc_package=payload.tgt_image.dlc_package,
            image_channel=payload.tgt_image.image_channel,
            image_version=payload.tgt_image.image_version,
            src_version=src_version)
    elif gspaths.IsImage(payload.tgt_image):
        # Signed payload.
        return gspaths.ChromeosReleases.PayloadUri(
            payload.build,
            random_str=random_str,
            key=payload.tgt_image.key,
            image_channel=payload.tgt_image.image_channel,
            image_version=payload.tgt_image.image_version,
            src_version=src_version)
    elif gspaths.IsUnsignedImageArchive(payload.tgt_image):
        # Unsigned test payload.
        return gspaths.ChromeosReleases.PayloadUri(payload.build,
                                                   random_str=random_str,
                                                   src_version=src_version)
    else:
        raise Error('Unknown image type %s' % type(payload.tgt_image))
def _FilterForImages(artifacts):
    """Return only instances of Image from a list of artifacts."""
    return [x for x in artifacts if gspaths.IsImage(x)]
 def testIsImage(self):
   a = float(3.14)
   self.assertFalse(gspaths.IsImage(a))
   b = gspaths.Image()
   self.assertTrue(gspaths.IsImage(b))