<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from gslib . cloud_api import BucketNotFoundException <EOL> from gslib . cloud_api import NotEmptyException <EOL> from gslib . cloud_api import NotFoundException <EOL> from gslib . cloud_api import ServiceException <EOL> from gslib . command import Command <EOL> from gslib . command import DecrementFailureCount <EOL> from gslib . command_argument import CommandArgument <EOL> from gslib . cs_api_map import ApiSelector <EOL> from gslib . exception import CommandException <EOL> from gslib . exception import NO_URLS_MATCHED_GENERIC <EOL> from gslib . exception import NO_URLS_MATCHED_TARGET <EOL> from gslib . name_expansion import NameExpansionIterator <EOL> from gslib . storage_url import StorageUrlFromString <EOL> from gslib . translation_helper import PreconditionsFromHeaders <EOL> from gslib . util import GetCloudApiInstance <EOL> from gslib . util import NO_MAX <EOL> from gslib . util import Retry <EOL> from gslib . util import StdinIterator <EOL> _SYNOPSIS = """<STR_LIT>""" <EOL> _DETAILED_HELP_TEXT = ( """<STR_LIT>""" + _SYNOPSIS + """<STR_LIT>""" ) <EOL> def _RemoveExceptionHandler ( cls , e ) : <EOL> """<STR_LIT>""" <EOL> if not cls . continue_on_error : <EOL> cls . logger . error ( str ( e ) ) <EOL> if isinstance ( e , BucketNotFoundException ) : <EOL> cls . bucket_not_found_count += <NUM_LIT:1> <EOL> cls . logger . error ( str ( e ) ) <EOL> else : <EOL> if _ExceptionMatchesBucketToDelete ( cls . bucket_strings_to_delete , e ) : <EOL> DecrementFailureCount ( ) <EOL> else : <EOL> cls . op_failure_count += <NUM_LIT:1> <EOL> def _RemoveFoldersExceptionHandler ( cls , e ) : <EOL> """<STR_LIT>""" <EOL> if ( ( isinstance ( e , CommandException ) and <EOL> NO_URLS_MATCHED_GENERIC in e . reason ) <EOL> or isinstance ( e , NotFoundException ) ) : <EOL> DecrementFailureCount ( ) <EOL> else : <EOL> raise e <EOL> def _RemoveFuncWrapper ( cls , name_expansion_result , thread_state = None ) : <EOL> cls . RemoveFunc ( name_expansion_result , thread_state = thread_state ) <EOL> def _ExceptionMatchesBucketToDelete ( bucket_strings_to_delete , e ) : <EOL> """<STR_LIT>""" <EOL> if bucket_strings_to_delete : <EOL> msg = NO_URLS_MATCHED_TARGET % '<STR_LIT>' <EOL> if msg in str ( e ) : <EOL> parts = str ( e ) . split ( msg ) <EOL> return len ( parts ) == <NUM_LIT:2> and parts [ <NUM_LIT:1> ] in bucket_strings_to_delete <EOL> class RmCommand ( Command ) : <EOL> """<STR_LIT>""" <EOL> command_spec = Command . CreateCommandSpec ( <EOL> '<STR_LIT>' , <EOL> command_name_aliases = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> usage_synopsis = _SYNOPSIS , <EOL> min_args = <NUM_LIT:0> , <EOL> max_args = NO_MAX , <EOL> supported_sub_args = '<STR_LIT>' , <EOL> file_url_ok = False , <EOL> provider_url_ok = False , <EOL> urls_start_arg = <NUM_LIT:0> , <EOL> gs_api_support = [ ApiSelector . XML , ApiSelector . JSON ] , <EOL> gs_default_api = ApiSelector . JSON , <EOL> argparse_arguments = [ <EOL> CommandArgument . MakeZeroOrMoreCloudURLsArgument ( ) <EOL> ] <EOL> ) <EOL> help_spec = Command . HelpSpec ( <EOL> help_name = '<STR_LIT>' , <EOL> help_name_aliases = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help_type = '<STR_LIT>' , <EOL> help_one_line_summary = '<STR_LIT>' , <EOL> help_text = _DETAILED_HELP_TEXT , <EOL> subcommand_help_text = { } , <EOL> ) <EOL> def RunCommand ( self ) : <EOL> """<STR_LIT>""" <EOL> self . continue_on_error = self . parallel_operations <EOL> self . read_args_from_stdin = False <EOL> self . all_versions = False <EOL> if self . sub_opts : <EOL> for o , unused_a in self . sub_opts : <EOL> if o == '<STR_LIT>' : <EOL> self . all_versions = True <EOL> elif o == '<STR_LIT>' : <EOL> self . continue_on_error = True <EOL> elif o == '<STR_LIT>' : <EOL> self . read_args_from_stdin = True <EOL> elif o == '<STR_LIT>' or o == '<STR_LIT>' : <EOL> self . recursion_requested = True <EOL> self . all_versions = True <EOL> if self . read_args_from_stdin : <EOL> if self . args : <EOL> raise CommandException ( '<STR_LIT>' ) <EOL> url_strs = StdinIterator ( ) <EOL> else : <EOL> if not self . args : <EOL> raise CommandException ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> url_strs = self . args <EOL> self . op_failure_count = <NUM_LIT:0> <EOL> self . bucket_not_found_count = <NUM_LIT:0> <EOL> bucket_urls_to_delete = [ ] <EOL> self . bucket_strings_to_delete = [ ] <EOL> if self . recursion_requested : <EOL> bucket_fields = [ '<STR_LIT:id>' ] <EOL> for url_str in url_strs : <EOL> url = StorageUrlFromString ( url_str ) <EOL> if url . IsBucket ( ) or url . IsProvider ( ) : <EOL> for blr in self . WildcardIterator ( url_str ) . IterBuckets ( <EOL> bucket_fields = bucket_fields ) : <EOL> bucket_urls_to_delete . append ( blr . storage_url ) <EOL> self . bucket_strings_to_delete . append ( url_str ) <EOL> self . preconditions = PreconditionsFromHeaders ( self . headers or { } ) <EOL> try : <EOL> name_expansion_iterator = NameExpansionIterator ( <EOL> self . command_name , self . debug , self . logger , self . gsutil_api , <EOL> url_strs , self . recursion_requested , project_id = self . project_id , <EOL> all_versions = self . all_versions , <EOL> continue_on_error = self . continue_on_error or self . parallel_operations ) <EOL> self . Apply ( _RemoveFuncWrapper , name_expansion_iterator , <EOL> _RemoveExceptionHandler , <EOL> fail_on_error = ( not self . continue_on_error ) , <EOL> shared_attrs = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except CommandException as e : <EOL> if _ExceptionMatchesBucketToDelete ( self . bucket_strings_to_delete , e ) : <EOL> DecrementFailureCount ( ) <EOL> else : <EOL> raise <EOL> except ServiceException , e : <EOL> if not self . continue_on_error : <EOL> raise <EOL> if self . bucket_not_found_count : <EOL> raise CommandException ( '<STR_LIT>' ) <EOL> if self . op_failure_count and not self . continue_on_error : <EOL> raise CommandException ( '<STR_LIT>' ) <EOL> if self . recursion_requested : <EOL> folder_object_wildcards = [ ] <EOL> for url_str in url_strs : <EOL> url = StorageUrlFromString ( url_str ) <EOL> if url . IsObject ( ) : <EOL> folder_object_wildcards . append ( '<STR_LIT>' % url_str ) <EOL> if folder_object_wildcards : <EOL> self . continue_on_error = True <EOL> try : <EOL> name_expansion_iterator = NameExpansionIterator ( <EOL> self . command_name , self . debug , <EOL> self . logger , self . gsutil_api , <EOL> folder_object_wildcards , self . recursion_requested , <EOL> project_id = self . project_id , <EOL> all_versions = self . all_versions ) <EOL> self . Apply ( _RemoveFuncWrapper , name_expansion_iterator , <EOL> _RemoveFoldersExceptionHandler , <EOL> fail_on_error = False ) <EOL> except CommandException as e : <EOL> if not e . reason . startswith ( NO_URLS_MATCHED_GENERIC ) : <EOL> raise <EOL> for url in bucket_urls_to_delete : <EOL> self . logger . info ( '<STR_LIT>' , url ) <EOL> @ Retry ( NotEmptyException , tries = <NUM_LIT:3> , timeout_secs = <NUM_LIT:1> ) <EOL> def BucketDeleteWithRetry ( ) : <EOL> self . gsutil_api . DeleteBucket ( url . bucket_name , provider = url . scheme ) <EOL> BucketDeleteWithRetry ( ) <EOL> if self . op_failure_count : <EOL> plural_str = '<STR_LIT:s>' if self . op_failure_count else '<STR_LIT>' <EOL> raise CommandException ( '<STR_LIT>' % ( <EOL> self . op_failure_count , plural_str , plural_str ) ) <EOL> return <NUM_LIT:0> <EOL> def RemoveFunc ( self , name_expansion_result , thread_state = None ) : <EOL> gsutil_api = GetCloudApiInstance ( self , thread_state = thread_state ) <EOL> exp_src_url = name_expansion_result . expanded_storage_url <EOL> self . logger . info ( '<STR_LIT>' , exp_src_url ) <EOL> gsutil_api . DeleteObject ( <EOL> exp_src_url . bucket_name , exp_src_url . object_name , <EOL> preconditions = self . preconditions , generation = exp_src_url . generation , <EOL> provider = exp_src_url . scheme ) </s>
