def testPluralityCheckableIteratorWith3Elems(self):
   """Tests PluralityCheckableIterator with 3 elements."""
   input_list = range(3)
   it = iter(input_list)
   pcit = PluralityCheckableIterator(it)
   self.assertFalse(pcit.is_empty())
   self.assertTrue(pcit.has_plurality())
   output_list = list(pcit)
   self.assertEqual(input_list, output_list)
 def testPluralityCheckableIteratorWith3Elems(self):
     """Tests PluralityCheckableIterator with 3 elements."""
     input_list = range(3)
     it = iter(input_list)
     pcit = PluralityCheckableIterator(it)
     self.assertFalse(pcit.is_empty())
     self.assertTrue(pcit.has_plurality())
     output_list = list(pcit)
     self.assertEqual(input_list, output_list)
Exemplo n.º 3
0
    def __iter__(self):
        for uri_str in self.uri_strs:
            # Step 1: Expand any explicitly specified wildcards. The output from this
            # step is an iterator of BucketListingRef.
            # Starting with gs://buck*/abc* this step would expand to gs://bucket/abcd
            if ContainsWildcard(uri_str):
                post_step1_iter = self._WildcardIterator(uri_str)
            else:
                suri = self.suri_builder.StorageUri(uri_str)
                post_step1_iter = iter([BucketListingRef(suri)])
            post_step1_iter = PluralityCheckableIterator(post_step1_iter)

            # Step 2: Expand bucket subdirs and versions. The output from this
            # step is an iterator of (names_container, BucketListingRef).
            # Starting with gs://bucket/abcd this step would expand to:
            #   iter([(True, abcd/o1.txt), (True, abcd/o2.txt)]).
            if self.flat and self.recursion_requested:
                post_step2_iter = _ImplicitBucketSubdirIterator(
                    self, post_step1_iter, self.flat)
            elif self.all_versions:
                post_step2_iter = _AllVersionIterator(self,
                                                      post_step1_iter,
                                                      headers=self.headers)
            else:
                post_step2_iter = _NonContainerTuplifyIterator(post_step1_iter)
            post_step2_iter = PluralityCheckableIterator(post_step2_iter)

            # Step 3. Expand directories and buckets. This step yields the iterated
            # values. Starting with gs://bucket this step would expand to:
            #  [abcd/o1.txt, abcd/o2.txt, xyz/o1.txt, xyz/o2.txt]
            # Starting with file://dir this step would expand to:
            #  [dir/a.txt, dir/b.txt, dir/c/]
            exp_src_bucket_listing_refs = []
            wc = self._flatness_wildcard[self.flat]
            src_uri_expands_to_multi = (post_step1_iter.has_plurality()
                                        or post_step2_iter.has_plurality())
            is_multi_src_request = (self.uri_strs.has_plurality()
                                    or src_uri_expands_to_multi)

            if post_step2_iter.is_empty():
                raise CommandException('No URIs matched: %s' % uri_str)
            for (names_container, blr) in post_step2_iter:
                if (not blr.GetUri().names_container()
                        and (self.flat or not blr.HasPrefix())):
                    yield NameExpansionResult(uri_str,
                                              is_multi_src_request,
                                              src_uri_expands_to_multi,
                                              names_container,
                                              blr.GetUriString(),
                                              self.have_existing_dst_container,
                                              is_latest=blr.IsLatest())
                    continue
                if not self.recursion_requested:
                    if blr.GetUri().is_file_uri():
                        desc = 'directory'
                    else:
                        desc = 'bucket'
                    print 'Omitting %s "%s". (Did you mean to do %s -R?)' % (
                        desc, blr.GetUri(), self.command_name)
                    continue
                if blr.GetUri().is_file_uri():
                    # Convert dir to implicit recursive wildcard.
                    uri_to_iterate = '%s/%s' % (blr.GetUriString(), wc)
                else:
                    # Convert bucket to implicit recursive wildcard.
                    uri_to_iterate = blr.GetUri().clone_replace_name(wc)
                wc_iter = PluralityCheckableIterator(
                    self._WildcardIterator(uri_to_iterate))
                src_uri_expands_to_multi = (src_uri_expands_to_multi
                                            or wc_iter.has_plurality())
                is_multi_src_request = (self.uri_strs.has_plurality()
                                        or src_uri_expands_to_multi)
                for blr in wc_iter:
                    yield NameExpansionResult(uri_str,
                                              is_multi_src_request,
                                              src_uri_expands_to_multi,
                                              True,
                                              blr.GetUriString(),
                                              self.have_existing_dst_container,
                                              is_latest=blr.IsLatest())
Exemplo n.º 4
0
  def __iter__(self):
    for uri_str in self.uri_strs:
      # Step 1: Expand any explicitly specified wildcards. The output from this
      # step is an iterator of BucketListingRef.
      # Starting with gs://buck*/abc* this step would expand to gs://bucket/abcd
      if ContainsWildcard(uri_str):
        post_step1_iter = self._WildcardIterator(uri_str)
      else:
        suri = self.suri_builder.StorageUri(uri_str)
        post_step1_iter = iter([BucketListingRef(suri)])
      post_step1_iter = PluralityCheckableIterator(post_step1_iter)

      # Step 2: Expand bucket subdirs and versions. The output from this
      # step is an iterator of (names_container, BucketListingRef).
      # Starting with gs://bucket/abcd this step would expand to:
      #   iter([(True, abcd/o1.txt), (True, abcd/o2.txt)]).
      if self.flat and self.recursion_requested:
        post_step2_iter = _ImplicitBucketSubdirIterator(self,
            post_step1_iter, self.flat)
      elif self.all_versions:
        post_step2_iter = _AllVersionIterator(self, post_step1_iter,
                                              headers=self.headers)
      else:
        post_step2_iter = _NonContainerTuplifyIterator(post_step1_iter)
      post_step2_iter = PluralityCheckableIterator(post_step2_iter)

      # Step 3. Expand directories and buckets. This step yields the iterated
      # values. Starting with gs://bucket this step would expand to:
      #  [abcd/o1.txt, abcd/o2.txt, xyz/o1.txt, xyz/o2.txt]
      # Starting with file://dir this step would expand to:
      #  [dir/a.txt, dir/b.txt, dir/c/]
      exp_src_bucket_listing_refs = []
      wc = self._flatness_wildcard[self.flat]
      src_uri_expands_to_multi = (post_step1_iter.has_plurality()
                                  or post_step2_iter.has_plurality())
      is_multi_src_request = (self.uri_strs.has_plurality
                              or src_uri_expands_to_multi)

      if post_step2_iter.is_empty():
        raise CommandException('No URIs matched: %s' % uri_str)
      for (names_container, blr) in post_step2_iter:
        if (not blr.GetUri().names_container()
            and (self.flat or not blr.HasPrefix())):
          yield NameExpansionResult(uri_str, is_multi_src_request,
                                    src_uri_expands_to_multi, names_container,
                                    blr.GetUriString(),
                                    self.have_existing_dst_container,
                                    is_latest=blr.IsLatest())
          continue
        if not self.recursion_requested:
          if blr.GetUri().is_file_uri():
            desc = 'directory'
          elif blr.GetUri().names_bucket():
            desc = 'bucket'
          else:
            desc = 'bucket subdir'
          if self.cmd_supports_recursion:
            self.logger.info(
                'Omitting %s "%s". (Did you mean to do %s -R?)',
                desc, blr.GetUri(), self.command_name)
          else:
            self.logger.info('Omitting %s "%s".', desc, blr.GetUri())
          continue
        if blr.GetUri().is_file_uri():
          # Convert dir to implicit recursive wildcard.
          uri_to_iterate = '%s/%s' % (blr.GetUriString(), wc)
        else:
          # Convert bucket to implicit recursive wildcard.
          uri_to_iterate = blr.GetUri().clone_replace_name(wc)
        wc_iter = PluralityCheckableIterator(
            self._WildcardIterator(uri_to_iterate))
        src_uri_expands_to_multi = (src_uri_expands_to_multi
                                    or wc_iter.has_plurality())
        is_multi_src_request = (self.uri_strs.has_plurality
                                or src_uri_expands_to_multi)
        for blr in wc_iter:
          yield NameExpansionResult(uri_str, is_multi_src_request,
                                    src_uri_expands_to_multi, True,
                                    blr.GetUriString(),
                                    self.have_existing_dst_container,
                                    is_latest=blr.IsLatest())