Ejemplo n.º 1
0
def _FindAndVerifyPartitionsAndImages(partitions, directory, board):
  """Validate partitions and images.

  Validate all partition names and partition directories. Cannot stop mid
  flash so its important to validate everything first.

  Args:
    Partitions: partitions to be tested.
    directory: directory containing the images.
    board: board name of the device to flash.

  Returns:
    Dictionary with exact partition, image name mapping.
  """

  files = os.listdir(directory)
  return_dict = collections.OrderedDict()

  def find_file(pattern):
    for filename in files:
      if fnmatch.fnmatch(filename, pattern):
        return os.path.join(directory, filename)
    return None

  for partition in partitions:
    partition_info = _KNOWN_PARTITIONS[partition]
    image_file = find_file(partition_info['image'])
    if image_file:
      return_dict[partition] = image_file
    elif ('optional' not in partition_info
          or not partition_info['optional'](board)):
      raise device_errors.FastbootCommandFailedError(
          'Failed to flash device. Could not find image for %s.',
          partition_info['image'])
  return return_dict
Ejemplo n.º 2
0
 def FastbootCommandFailedError(self,
                                args=None,
                                output=None,
                                status=None,
                                msg=None):
   return mock.Mock(side_effect=device_errors.FastbootCommandFailedError(
       args, output, status, msg, str(self.device_utils_mock)))
Ejemplo n.º 3
0
 def testPicklable_FastbootCommandFailedError(self):
     original = device_errors.FastbootCommandFailedError(
         ['these', 'are', 'fastboot', 'args'],
         'fastboot failure output',
         status=':(',
         device_serial='0123456789abcdef')
     self.assertIsPicklable(original)
Ejemplo n.º 4
0
 def find_file(pattern):
     for filename in files:
         if fnmatch.fnmatch(filename, pattern):
             return os.path.join(directory, filename)
     raise device_errors.FastbootCommandFailedError(
         'Failed to flash device. Counld not find image for %s.',
         pattern)
Ejemplo n.º 5
0
def _FindAndVerifyPartitionsAndImages(partitions, directory, supports_ab):
    """Validate partitions and images.

  Validate all partition names and partition directories. Cannot stop mid
  flash so its important to validate everything first.

  Args:
    Partitions: partitions to be tested.
    directory: directory containing the images.
    supports_ab: boolean to indicate if the device supports A/B system updates.

  Returns:
    Dictionary with exact partition, image name mapping.
  """

    files = os.listdir(directory)
    return_dict = collections.OrderedDict()

    def find_file(pattern):
        for filename in files:
            if fnmatch.fnmatch(filename, pattern):
                return os.path.join(directory, filename)
        return None

    def is_image_required(partition_info, supports_ab):
        """Check if an image is required.

    An image will be required if:
     - key 'optional' does not exist in partition_info.
     - and (
        - the device does not support A/B updates,
        - and key 'ab_exclusive' does not exist or is False in partition_info.
       ) or (
        - the device supports A/B updates,
        - and key 'ab_exclusive' does not exist or is True in partition_info.
       )
    """
        return (not 'optional' in partition_info and
                ((not supports_ab and
                  (not 'ab_exclusive' in partition_info
                   or not partition_info.get('ab_exclusive'))) or
                 (supports_ab and (not 'ab_exclusive' in partition_info
                                   or partition_info.get('ab_exclusive')))))

    for partition in partitions:
        partition_info = _KNOWN_PARTITIONS[partition]
        image_file = find_file(partition_info['image'])
        if image_file:
            return_dict[partition] = image_file
        elif is_image_required(partition_info, supports_ab):
            raise device_errors.FastbootCommandFailedError(
                [],
                '',
                message='Failed to flash device%s. Could not find image for %s.'
                % (' which supports A/B updates' if supports_ab else '',
                   partition_info['image']))
    return return_dict
Ejemplo n.º 6
0
 def GetVar(self, variable, timeout=None, retries=None):
     args = ['getvar', variable]
     output = self._RunDeviceFastbootCommand(args)
     # getvar returns timing information on the last line of output, so only
     # parse the first line.
     output = output.splitlines()[0]
     # And the first line should match the format '$(var): $(value)'.
     if variable + ': ' not in output:
         raise device_errors.FastbootCommandFailedError(
             args, output, message="Unknown 'getvar' output format.")
     return output.split('%s: ' % variable)[1].strip()
Ejemplo n.º 7
0
    def _RunFastbootCommand(cls, cmd):
        """Run a generic fastboot command.

    Args:
      cmd: Command to run. Must be list of args, the first one being the command

    Returns:
      output of command.

    Raises:
      TypeError: If cmd is not of type list.
    """
        if type(cmd) == list:
            cmd = [cls._fastboot_path.read()] + cmd
        else:
            raise TypeError(
                'Command for _RunDeviceFastbootCommand must be a list.')
        status, output = cmd_helper.GetCmdStatusAndOutput(cmd)
        if int(status) != 0:
            raise device_errors.FastbootCommandFailedError(cmd, output, status)
        return output
Ejemplo n.º 8
0
    def _RunFastbootCommand(self, cmd):
        """Run a command line command using the fastboot android tool.

    Args:
      cmd: Command to run. Must be list of args, the first one being the command

    Returns:
      output of command.

    Raises:
      TypeError: If cmd is not of type list.
    """
        if type(cmd) == list:
            cmd = [self._fastboot_path.read(), '-s', self._device_serial] + cmd
        else:
            raise TypeError('Command for _RunFastbootCommand must be a list.')
        status, output = cmd_helper.GetCmdStatusAndOutput(cmd)
        if int(status) != 0:
            raise device_errors.FastbootCommandFailedError(
                cmd, output, status, self._device_serial)
        return output
Ejemplo n.º 9
0
    def _RunFastbootCommand(cls, cmd):
        """Run a generic fastboot command.

    Args:
      cmd: Command to run. Must be list of args, the first one being the command

    Returns:
      output of command.

    Raises:
      TypeError: If cmd is not of type list.
    """
        if isinstance(cmd, list):
            cmd = [cls._fastboot_path.read()] + cmd
        else:
            raise TypeError(
                'Command for _RunDeviceFastbootCommand must be a list.')
        # fastboot can't be trusted to keep non-error output out of stderr, so
        # capture stderr as part of stdout.
        status, output = cmd_helper.GetCmdStatusAndOutput(cmd,
                                                          merge_stderr=True)
        if int(status) != 0:
            raise device_errors.FastbootCommandFailedError(cmd, output, status)
        return output