Exemple #1
0
 def RunCommand(self):
     """Command entry point for stat command."""
     stat_fields = ENCRYPTED_FIELDS + UNENCRYPTED_FULL_LISTING_FIELDS
     found_nonmatching_arg = False
     for url_str in self.args:
         arg_matches = 0
         url = StorageUrlFromString(url_str)
         if not url.IsObject():
             raise CommandException(
                 'The stat command only works with object URLs')
         try:
             if ContainsWildcard(url_str):
                 blr_iter = self.WildcardIterator(url_str).IterObjects(
                     bucket_listing_fields=stat_fields)
             else:
                 try:
                     single_obj = self.gsutil_api.GetObjectMetadata(
                         url.bucket_name,
                         url.object_name,
                         generation=url.generation,
                         provider=url.scheme,
                         fields=stat_fields)
                 except EncryptionException:
                     # Retry without requesting hashes.
                     single_obj = self.gsutil_api.GetObjectMetadata(
                         url.bucket_name,
                         url.object_name,
                         generation=url.generation,
                         provider=url.scheme,
                         fields=UNENCRYPTED_FULL_LISTING_FIELDS)
                 blr_iter = [
                     BucketListingObject(url, root_object=single_obj)
                 ]
             for blr in blr_iter:
                 if blr.IsObject():
                     arg_matches += 1
                     # TODO: Request fewer fields if we're not printing the object.
                     if logging.getLogger().isEnabledFor(logging.INFO):
                         PrintFullInfoAboutObject(blr, incl_acl=False)
         except AccessDeniedException:
             if logging.getLogger().isEnabledFor(logging.INFO):
                 sys.stderr.write(
                     'You aren\'t authorized to read %s - skipping' %
                     url_str)
         except InvalidUrlError:
             raise
         except NotFoundException:
             pass
         if not arg_matches:
             if logging.getLogger().isEnabledFor(logging.INFO):
                 sys.stderr.write(NO_URLS_MATCHED_TARGET % url_str)
             found_nonmatching_arg = True
     if found_nonmatching_arg:
         return 1
     return 0
    def test_one_object_with_L_storage_class_update(self):
        """Tests the JSON storage class update time field."""
        if self.test_api == ApiSelector.XML:
            return unittest.skip(
                'XML API has no concept of storage class update time')
        # Case 1: Create an object message where Storage class update time is the
        # same as Creation time.
        current_time = datetime(2017, 1, 2, 3, 4, 5, 6, tzinfo=None)
        obj_metadata = apitools_messages.Object(
            name='foo',
            bucket='bar',
            timeCreated=current_time,
            updated=current_time,
            timeStorageClassUpdated=current_time,
            etag='12345')
        # Create mock object to point to obj_metadata.
        obj_ref = mock.Mock()
        obj_ref.root_object = obj_metadata
        obj_ref.url_string = 'foo'

        # Print out attributes of object message.
        with CaptureStdout() as output:
            PrintFullInfoAboutObject(obj_ref)
        output = '\n'.join(output)

        # Verify that no Storage class update time field displays since it's the
        # same as Creation time.
        find_stor_update_re = re.compile(
            r'^\s*Storage class update time:+(?P<stor_update_time_val>.+)$',
            re.MULTILINE)
        stor_update_time_match = re.search(find_stor_update_re, output)
        self.assertIsNone(stor_update_time_match)

        # Case 2: Create an object message where Storage class update time differs
        # from Creation time.
        new_update_time = datetime(2017, 2, 3, 4, 5, 6, 7, tzinfo=None)
        obj_metadata2 = apitools_messages.Object(
            name='foo2',
            bucket='bar2',
            timeCreated=current_time,
            updated=current_time,
            timeStorageClassUpdated=new_update_time,
            etag='12345')
        # Create mock object to point to obj_metadata2.
        obj_ref2 = mock.Mock()
        obj_ref2.root_object = obj_metadata2
        obj_ref2.url_string = 'foo2'

        # Print out attributes of object message.
        with CaptureStdout() as output2:
            PrintFullInfoAboutObject(obj_ref2)
        output2 = '\n'.join(output2)

        # Verify that Creation time and Storage class update time fields display and
        # are the same as the times set in the object message.
        find_time_created_re = re.compile(
            r'^\s*Creation time:\s+(?P<time_created_val>.+)$', re.MULTILINE)
        time_created_match = re.search(find_time_created_re, output2)
        self.assertIsNotNone(time_created_match)
        time_created = time_created_match.group('time_created_val')
        self.assertEqual(
            time_created,
            datetime.strftime(current_time, '%a, %d %b %Y %H:%M:%S GMT'))
        find_stor_update_re_2 = re.compile(
            r'^\s*Storage class update time:+(?P<stor_update_time_val_2>.+)$',
            re.MULTILINE)
        stor_update_time_match_2 = re.search(find_stor_update_re_2, output2)
        self.assertIsNotNone(stor_update_time_match_2)
        stor_update_time = stor_update_time_match_2.group(
            'stor_update_time_val_2')
        self.assertEqual(
            stor_update_time,
            datetime.strftime(new_update_time, '%a, %d %b %Y %H:%M:%S GMT'))