<s> import sys <EOL> from neutronclient . _i18n import _ <EOL> from neutronclient . neutron import v2_0 as neutronV20 <EOL> class Purge ( neutronV20 . NeutronCommand ) : <EOL> """<STR_LIT>""" <EOL> def _pluralize ( self , string ) : <EOL> return string + '<STR_LIT:s>' <EOL> def _get_resources ( self , neutron_client , resource_types , tenant_id ) : <EOL> resources = [ ] <EOL> for resource_type in resource_types : <EOL> resources . append ( [ ] ) <EOL> resource_type_plural = self . _pluralize ( resource_type ) <EOL> opts = { '<STR_LIT>' : [ '<STR_LIT:id>' , '<STR_LIT>' ] } <EOL> if resource_type_plural == '<STR_LIT>' : <EOL> opts [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> opts [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> function = getattr ( neutron_client , '<STR_LIT>' % <EOL> resource_type_plural ) <EOL> if callable ( function ) : <EOL> returned_resources = function ( ** opts ) . get ( resource_type_plural , <EOL> [ ] ) <EOL> for resource in returned_resources : <EOL> if resource [ '<STR_LIT>' ] == tenant_id : <EOL> index = resource_types . index ( resource_type ) <EOL> resources [ index ] . append ( resource ) <EOL> self . total_resources += <NUM_LIT:1> <EOL> return resources <EOL> def _delete_resource ( self , neutron_client , resource_type , resource ) : <EOL> resource_id = resource [ '<STR_LIT:id>' ] <EOL> if resource_type == '<STR_LIT:port>' : <EOL> if resource . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> body = { '<STR_LIT>' : resource_id } <EOL> neutron_client . remove_interface_router ( resource [ '<STR_LIT>' ] , <EOL> body ) <EOL> return <EOL> function = getattr ( neutron_client , '<STR_LIT>' % resource_type ) <EOL> if callable ( function ) : <EOL> function ( resource_id ) <EOL> def _purge_resources ( self , neutron_client , resource_types , <EOL> tenant_resources ) : <EOL> deleted = { } <EOL> failed = { } <EOL> failures = False <EOL> for resources in tenant_resources : <EOL> index = tenant_resources . index ( resources ) <EOL> resource_type = resource_types [ index ] <EOL> failed [ resource_type ] = <NUM_LIT:0> <EOL> deleted [ resource_type ] = <NUM_LIT:0> <EOL> for resource in resources : <EOL> try : <EOL> self . _delete_resource ( neutron_client , resource_type , <EOL> resource ) <EOL> deleted [ resource_type ] += <NUM_LIT:1> <EOL> self . deleted_resources += <NUM_LIT:1> <EOL> except Exception : <EOL> failures = True <EOL> failed [ resource_type ] += <NUM_LIT:1> <EOL> self . total_resources -= <NUM_LIT:1> <EOL> percent_complete = <NUM_LIT:100> <EOL> if self . total_resources > <NUM_LIT:0> : <EOL> percent_complete = ( self . deleted_resources / <EOL> float ( self . total_resources ) ) * <NUM_LIT:100> <EOL> sys . stdout . write ( "<STR_LIT>" % <EOL> percent_complete ) <EOL> sys . stdout . flush ( ) <EOL> return ( deleted , failed , failures ) <EOL> def _build_message ( self , deleted , failed , failures ) : <EOL> msg = '<STR_LIT>' <EOL> deleted_msg = [ ] <EOL> for resource , value in deleted . items ( ) : <EOL> if value : <EOL> if not msg : <EOL> msg = '<STR_LIT>' <EOL> if not value == <NUM_LIT:1> : <EOL> resource = self . _pluralize ( resource ) <EOL> deleted_msg . append ( "<STR_LIT>" % ( value , resource ) ) <EOL> if deleted_msg : <EOL> msg += '<STR_LIT:U+002C>' . join ( deleted_msg ) <EOL> failed_msg = [ ] <EOL> if failures : <EOL> if msg : <EOL> msg += '<STR_LIT>' <EOL> msg += '<STR_LIT>' <EOL> for resource , value in failed . items ( ) : <EOL> if value : <EOL> if not value == <NUM_LIT:1> : <EOL> resource = self . _pluralize ( resource ) <EOL> failed_msg . append ( "<STR_LIT>" % ( value , resource ) ) <EOL> msg += '<STR_LIT:U+002C>' . join ( failed_msg ) <EOL> if msg : <EOL> msg += '<STR_LIT:.>' <EOL> else : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> return msg <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( Purge , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' ) ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> neutron_client = self . get_client ( ) <EOL> self . any_failures = False <EOL> resource_types = [ '<STR_LIT>' , '<STR_LIT:port>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> deleted = { } <EOL> failed = { } <EOL> self . total_resources = <NUM_LIT:0> <EOL> self . deleted_resources = <NUM_LIT:0> <EOL> resources = self . _get_resources ( neutron_client , resource_types , <EOL> parsed_args . tenant ) <EOL> deleted , failed , failures = self . _purge_resources ( neutron_client , <EOL> resource_types , <EOL> resources ) <EOL> print ( '<STR_LIT>' % self . _build_message ( deleted , failed , failures ) ) </s>
