コード例 #1
0
  def DownloadFile(self, path, offset=0):
    """Download file from Google Drive.

    Args:
      path: a build item path (e.g. folderA/folderB/cts.zip)
      offset: byte offset to read from
    Yields:
      FileChunks (data read, current position, total file size)
    Raises:
      errors.FileNotFoundError: if a path is invalid
    """
    build_item = self.GetBuildItem(path)
    if (build_item is None) or (not build_item.is_file):
      raise errors.FileNotFoundError(_FILE_NOT_FOUND_ERROR % path)

    buffer_ = io.BytesIO()
    downloader = self._CreateMediaDownloader(path, buffer_,
                                             constant.DEFAULT_CHUNK_SIZE,
                                             offset)
    done = False
    while not done:
      status, done = downloader.next_chunk(num_retries=constant.NUM_RETRIES)
      yield file_util.FileChunk(
          data=buffer_.getvalue(),
          offset=status.resumable_progress,
          total_size=status.total_size)
      buffer_.seek(0)
      buffer_.truncate()
コード例 #2
0
  def _GetFileIdHelper(
      self, parent_folder_id, name):
    """Get id by query with parent folder's id and wanted file or folder's name.

    E.g. If path is b/c, and we pass in b's id and c's name. We will return
    c'id.

    Args:
      parent_folder_id: (e.g. root, some_hash_value).
      name: a file or folder's name (e.g. folderA, cat.img)
    Returns:
      subfolder's id: (e.g. some_hash_value)
    Raises:
      errors.PluginError: if the filename is not unique.
      errors.FileNotFoundError: if no file matched the subfolder's name
        or no matching file id was found.
    """
    param = {}
    param['q'] = (_QUERY_ITEM_FORMAT % (parent_folder_id, name))
    child_ids, _ = self._GetFileIds(param)

    if len(child_ids) >= 2:
      raise errors.PluginError(_DUPLICATED_OBJECT_NAME_ERROR % name)
    if not child_ids:
      raise errors.FileNotFoundError(_FILE_NOT_FOUND_ERROR % name)
    return child_ids[0]
コード例 #3
0
  def ListBuildItems(self, path=None, page_token=None, item_type=None):
    """List build items under a given path.

    Args:
      path: a build item path (e.g. /git_master/taimen-userdebug).
      page_token: an optional page token.
      item_type: a type of build items to list. Returns all types if None.
    Returns:
      (a list of base.BuildItem objects, the next page token)
    Raises:
      ValueError: if a path is invalid.
    """
    try:
      parts = path.split('/', MAX_PATH_PARTS - 1) if path else []
      if not parts:
        if item_type == base.BuildItemType.FILE:
          return [], None
        return self._ListBranches(page_token)
      elif len(parts) == 1:
        if item_type == base.BuildItemType.FILE:
          return [], None
        return self._ListTargets(branch=parts[0], page_token=page_token)
      elif len(parts) == 2:
        if item_type == base.BuildItemType.FILE:
          return [], None
        return self._ListBuilds(
            branch=parts[0], target=parts[1], page_token=page_token)
      elif len(parts) == 3:
        if item_type == base.BuildItemType.DIRECTORY:
          return [], None
        return self._ListBuildArtifacts(
            branch=parts[0], target=parts[1], build_id=parts[2],
            page_token=page_token)
      raise ValueError('invalid path: %s' % path)
    except apiclient.errors.HttpError as e:
      if e.resp.status == 404:
        raise errors.FileNotFoundError('File %s not found' % path)
      raise
コード例 #4
0
  def _GetFileItem(self, file_id):
    """Get a file item from file id.

    Args:
      file_id: a google drive file id

    Returns:
      file_item: represents an actuall google drive file

    Raises:
      errors.FileNotFoundError: if file id does not exist.
    """
    # get a file item by file id
    client = self._GetClient()
    try:
      file_item = client.files().get(
          fileId=file_id,
          fields=_FIELDS).execute(num_retries=constant.NUM_RETRIES)
      return file_item
    except apiclient.errors.HttpError as e:
      if e.resp.status == constant.HTTP_NOT_FOUND_ERROR_CODE:
        raise errors.FileNotFoundError(_INVALID_FILE_ID_ERROR % file_id)
      raise
コード例 #5
0
def FindTestResources(test_resource_objs):
  """Parses test resource obj urls (may include wildcards).

  Args:
    test_resource_objs: a list of TestResourceObjs

  Returns:
    parsed_objs: a list of TestResourceObj with urls parsed
  Raises:
    FileNotFoundError: if no file matching the test resource obj url is found
    TestResourceError: if a test resource obj url is missing
  """
  test_resource_map = {}
  for obj in test_resource_objs:
    build_locator = BuildLocator.ParseUrl(obj.url)
    if build_locator:
      build_item = FindFile(build_locator.build_channel_id,
                            build_locator.directory, build_locator.filename)
      if not build_item:
        raise errors.FileNotFoundError('Cannot find file from %s' % obj.url)
      # Build a encoded url
      url = BuildUrl(build_locator.build_channel_id, build_item)
    else:
      url = obj.url
    test_resource_map[obj.name] = ndb_models.TestResourceObj(
        name=obj.name,
        url=url,
        test_resource_type=obj.test_resource_type,
        decompress=obj.decompress,
        decompress_dir=obj.decompress_dir,
        params=ndb_models.TestResourceParameters.Clone(obj.params))
  parsed_objs = sorted(test_resource_map.values(), key=lambda x: x.name)
  for r in parsed_objs:
    logging.info('\t%s: %s', r.name, r.cache_url)
    if not r.url:
      raise errors.TestResourceError('No URL for test resource %s' % r.name)
  return parsed_objs