<s> from putil . rabbitmqadmin import Management , make_parser , LISTABLE , DELETABLE <EOL> import shlex <EOL> import simplejson <EOL> class RabbitManagementHelper ( object ) : <EOL> def __init__ ( self , parser , options ) : <EOL> self . parser = parser <EOL> self . options = options <EOL> def list_names ( self , listable_type ) : <EOL> list_str = '<STR_LIT>' % ( self . options , listable_type ) <EOL> ( options , args ) = self . parser . parse_args ( shlex . split ( list_str ) ) <EOL> mgmt = Management ( options , args [ <NUM_LIT:1> : ] ) <EOL> uri = mgmt . list_show_uri ( LISTABLE , '<STR_LIT:list>' , mgmt . args [ <NUM_LIT:1> : ] ) <EOL> output_json = mgmt . get ( uri ) <EOL> listables = simplejson . loads ( output_json ) <EOL> return listables <EOL> def list_names_with_prefix ( self , listables , name_prefix ) : <EOL> return [ l [ '<STR_LIT:name>' ] for l in listables if l [ '<STR_LIT:name>' ] . startswith ( name_prefix ) ] <EOL> def delete_names_with_prefix ( self , deletable_type , deleteable , name_prefix ) : <EOL> deleted = [ ] <EOL> for d in deleteable : <EOL> try : <EOL> if d [ '<STR_LIT:name>' ] . startswith ( name_prefix ) : <EOL> delete_cmd = '<STR_LIT>' % ( self . options , deletable_type , d [ '<STR_LIT:name>' ] ) <EOL> ( options , args ) = self . parser . parse_args ( shlex . split ( delete_cmd ) ) <EOL> mgmt = Management ( options , args [ <NUM_LIT:1> : ] ) <EOL> mgmt . invoke_delete ( ) <EOL> deleted . append ( d [ '<STR_LIT:name>' ] ) <EOL> except KeyError : <EOL> pass <EOL> return deleted <EOL> def clean_by_sysname ( connect_string , sysname ) : <EOL> """<STR_LIT>""" <EOL> rmh = RabbitManagementHelper ( make_parser ( ) , connect_string ) <EOL> exchanges = rmh . list_names ( '<STR_LIT>' ) <EOL> deleted_exchanges = rmh . delete_names_with_prefix ( '<STR_LIT>' , exchanges , sysname ) <EOL> queues = rmh . list_names ( '<STR_LIT>' ) <EOL> deleted_queues = rmh . delete_names_with_prefix ( '<STR_LIT>' , queues , sysname ) <EOL> return ( deleted_exchanges , deleted_queues ) </s>
