<s> from __future__ import print_function <EOL> import argparse <EOL> import inspect <EOL> import os <EOL> import sys <EOL> import warnings <EOL> import decorator <EOL> import jsonschema <EOL> from oslo_config import cfg <EOL> from oslo_utils import encodeutils <EOL> import prettytable <EOL> from requests . packages import urllib3 <EOL> import six <EOL> import sqlalchemy . exc <EOL> from rally . common . i18n import _ <EOL> from rally . common import logging <EOL> from rally . common . plugin import discover <EOL> from rally . common . plugin import info <EOL> from rally . common import version <EOL> from rally import exceptions <EOL> CONFIG_SEARCH_PATHS = [ sys . prefix + "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> CONF = cfg . CONF <EOL> LOG = logging . getLogger ( __name__ ) <EOL> MARGIN = <NUM_LIT:3> <EOL> def find_config_files ( path_list ) : <EOL> for path in path_list : <EOL> abspath = os . path . abspath ( os . path . expanduser ( path ) ) <EOL> confname = abspath + "<STR_LIT>" <EOL> if os . path . isfile ( confname ) : <EOL> return [ confname ] <EOL> return None <EOL> class MissingArgs ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , missing ) : <EOL> self . missing = missing <EOL> msg = _ ( "<STR_LIT>" ) % "<STR_LIT:U+002CU+0020>" . join ( missing ) <EOL> super ( MissingArgs , self ) . __init__ ( msg ) <EOL> def validate_args ( fn , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> argspec = inspect . getargspec ( fn ) <EOL> num_defaults = len ( argspec . defaults or [ ] ) <EOL> required_args = argspec . args [ : len ( argspec . args ) - num_defaults ] <EOL> if getattr ( fn , "<STR_LIT>" , None ) : <EOL> required_args . pop ( <NUM_LIT:0> ) <EOL> missing_required_args = required_args [ len ( args ) : ] <EOL> missing = [ arg for arg in missing_required_args if arg not in kwargs ] <EOL> if missing : <EOL> raise MissingArgs ( missing ) <EOL> def print_list ( objs , fields , formatters = None , sortby_index = <NUM_LIT:0> , <EOL> mixed_case_fields = None , field_labels = None , <EOL> normalize_field_names = False , <EOL> table_label = None , print_header = True , print_border = True , <EOL> out = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> formatters = formatters or { } <EOL> mixed_case_fields = mixed_case_fields or [ ] <EOL> field_labels = field_labels or fields <EOL> if len ( field_labels ) != len ( fields ) : <EOL> raise ValueError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { "<STR_LIT>" : field_labels , "<STR_LIT>" : fields } ) <EOL> if sortby_index is None : <EOL> kwargs = { } <EOL> else : <EOL> kwargs = { "<STR_LIT>" : field_labels [ sortby_index ] } <EOL> pt = prettytable . PrettyTable ( field_labels ) <EOL> pt . align = "<STR_LIT:l>" <EOL> for o in objs : <EOL> row = [ ] <EOL> for field in fields : <EOL> if field in formatters : <EOL> row . append ( formatters [ field ] ( o ) ) <EOL> else : <EOL> if normalize_field_names : <EOL> if field in mixed_case_fields : <EOL> field_name = field . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) <EOL> else : <EOL> field_name = field . lower ( ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:_>" ) <EOL> else : <EOL> field_name = field <EOL> if isinstance ( o , dict ) : <EOL> data = o . get ( field_name , "<STR_LIT>" ) <EOL> else : <EOL> data = getattr ( o , field_name , "<STR_LIT>" ) <EOL> row . append ( data ) <EOL> pt . add_row ( row ) <EOL> if not print_border or not print_header : <EOL> pt . set_style ( prettytable . PLAIN_COLUMNS ) <EOL> pt . left_padding_width = <NUM_LIT:0> <EOL> pt . right_padding_width = <NUM_LIT:1> <EOL> table_body = pt . get_string ( header = print_header , <EOL> border = print_border , <EOL> ** kwargs ) + "<STR_LIT:\n>" <EOL> table_header = "<STR_LIT>" <EOL> if table_label : <EOL> table_width = table_body . index ( "<STR_LIT:\n>" ) <EOL> table_header = make_table_header ( table_label , table_width ) <EOL> table_header += "<STR_LIT:\n>" <EOL> if six . PY3 : <EOL> if table_header : <EOL> out . write ( encodeutils . safe_encode ( table_header ) . decode ( ) ) <EOL> out . write ( encodeutils . safe_encode ( table_body ) . decode ( ) ) <EOL> else : <EOL> if table_header : <EOL> out . write ( encodeutils . safe_encode ( table_header ) ) <EOL> out . write ( encodeutils . safe_encode ( table_body ) ) <EOL> def make_table_header ( table_label , table_width , <EOL> junction_char = "<STR_LIT:+>" , horizontal_char = "<STR_LIT:->" , <EOL> vertical_char = "<STR_LIT:|>" ) : <EOL> """<STR_LIT>""" <EOL> if len ( table_label ) >= ( table_width - <NUM_LIT:2> ) : <EOL> raise ValueError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> label_and_space_width = table_width - len ( table_label ) - <NUM_LIT:2> <EOL> padding = <NUM_LIT:0> if label_and_space_width % <NUM_LIT:2> == <NUM_LIT:0> else <NUM_LIT:1> <EOL> half_table_width = label_and_space_width // <NUM_LIT:2> <EOL> left_spacing = ( "<STR_LIT:U+0020>" * half_table_width ) <EOL> right_spacing = ( "<STR_LIT:U+0020>" * ( half_table_width + padding ) ) <EOL> border_line = "<STR_LIT>" . join ( ( junction_char , <EOL> ( horizontal_char * ( table_width - <NUM_LIT:2> ) ) , <EOL> junction_char , ) ) <EOL> label_line = "<STR_LIT>" . join ( ( vertical_char , <EOL> left_spacing , <EOL> table_label , <EOL> right_spacing , <EOL> vertical_char , ) ) <EOL> return "<STR_LIT:\n>" . join ( ( border_line , label_line , ) ) <EOL> def make_header ( text , size = <NUM_LIT> , symbol = "<STR_LIT:->" ) : <EOL> """<STR_LIT>""" <EOL> header = symbol * size + "<STR_LIT:\n>" <EOL> header += "<STR_LIT>" % text <EOL> header += symbol * size + "<STR_LIT:\n>" <EOL> return header <EOL> def suppress_warnings ( f ) : <EOL> f . _suppress_warnings = True <EOL> return f <EOL> @ decorator . decorator <EOL> def process_keystone_exc ( f , * args , ** kwargs ) : <EOL> from keystoneclient import exceptions as keystone_exc <EOL> try : <EOL> return f ( * args , ** kwargs ) <EOL> except keystone_exc . Unauthorized as e : <EOL> print ( _ ( "<STR_LIT>" ) % e ) <EOL> return <NUM_LIT:1> <EOL> except keystone_exc . AuthorizationFailure as e : <EOL> print ( _ ( "<STR_LIT>" ) % e ) <EOL> return <NUM_LIT:1> <EOL> except keystone_exc . ConnectionRefused as e : <EOL> print ( _ ( "<STR_LIT>" ) % e ) <EOL> return <NUM_LIT:1> <EOL> class CategoryParser ( argparse . ArgumentParser ) : <EOL> """<STR_LIT>""" <EOL> def format_help ( self ) : <EOL> formatter = self . _get_formatter ( ) <EOL> formatter . add_usage ( self . usage , self . _actions , <EOL> self . _mutually_exclusive_groups ) <EOL> formatter . add_text ( self . description ) <EOL> for action_group in self . _action_groups [ <NUM_LIT:1> : ] : <EOL> formatter . start_section ( action_group . title ) <EOL> formatter . add_text ( action_group . description ) <EOL> formatter . add_arguments ( action_group . _group_actions ) <EOL> formatter . end_section ( ) <EOL> formatter . add_text ( self . epilog ) <EOL> return formatter . format_help ( ) <EOL> def error ( self , message ) : <EOL> self . print_help ( sys . stderr ) <EOL> if message . startswith ( "<STR_LIT>" ) and message . endswith ( "<STR_LIT>" ) : <EOL> missing_arg = message . split ( ) [ <NUM_LIT:1> ] <EOL> print ( _ ( "<STR_LIT>" ) % missing_arg ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def pretty_float_formatter ( field , ndigits = None ) : <EOL> """<STR_LIT>""" <EOL> def _formatter ( obj ) : <EOL> value = obj [ field ] if type ( obj ) == dict else getattr ( obj , field ) <EOL> if type ( value ) in ( int , float ) : <EOL> if ndigits : <EOL> return round ( value , ndigits ) <EOL> return value <EOL> return "<STR_LIT>" <EOL> return _formatter <EOL> def args ( * args , ** kwargs ) : <EOL> def _decorator ( func ) : <EOL> func . __dict__ . setdefault ( "<STR_LIT:args>" , [ ] ) . insert ( <NUM_LIT:0> , ( args , kwargs ) ) <EOL> if "<STR_LIT>" not in kwargs and "<STR_LIT:action>" not in kwargs : <EOL> kwargs [ "<STR_LIT>" ] = "<STR_LIT>" % args [ <NUM_LIT:0> ] . replace ( <EOL> "<STR_LIT>" , "<STR_LIT>" ) . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) <EOL> return func <EOL> return _decorator <EOL> def alias ( command_name ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> func . alias = command_name <EOL> return func <EOL> return decorator <EOL> def deprecated_args ( * args , ** kwargs ) : <EOL> def _decorator ( func ) : <EOL> if "<STR_LIT>" not in kwargs : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> func . __dict__ . setdefault ( "<STR_LIT:args>" , [ ] ) . insert ( <NUM_LIT:0> , ( args , kwargs ) ) <EOL> func . __dict__ . setdefault ( "<STR_LIT>" , [ ] ) <EOL> func . deprecated_args . append ( args [ <NUM_LIT:0> ] ) <EOL> help_msg = "<STR_LIT>" % kwargs . pop ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in kwargs : <EOL> help_msg += "<STR_LIT>" % kwargs . pop ( "<STR_LIT>" ) <EOL> if "<STR_LIT>" in kwargs : <EOL> help_msg += kwargs [ "<STR_LIT>" ] <EOL> kwargs [ "<STR_LIT>" ] = help_msg <EOL> return func <EOL> return _decorator <EOL> def _methods_of ( cls ) : <EOL> """<STR_LIT>""" <EOL> all_methods = inspect . getmembers ( <EOL> cls , predicate = lambda x : inspect . ismethod ( x ) or inspect . isfunction ( x ) ) <EOL> methods = [ m for m in all_methods if not m [ <NUM_LIT:0> ] . startswith ( "<STR_LIT:_>" ) ] <EOL> return methods <EOL> def _compose_category_description ( category ) : <EOL> descr_pairs = _methods_of ( category ) <EOL> description = "<STR_LIT>" <EOL> doc = category . __doc__ <EOL> if doc : <EOL> description = doc . strip ( ) <EOL> if descr_pairs : <EOL> description += "<STR_LIT>" <EOL> sublen = lambda item : len ( item [ <NUM_LIT:0> ] ) <EOL> first_column_len = max ( map ( sublen , descr_pairs ) ) + MARGIN <EOL> for item in descr_pairs : <EOL> name = getattr ( item [ <NUM_LIT:1> ] , "<STR_LIT>" , item [ <NUM_LIT:0> ] ) <EOL> if item [ <NUM_LIT:1> ] . __doc__ : <EOL> doc = info . parse_docstring ( <EOL> item [ <NUM_LIT:1> ] . __doc__ ) [ "<STR_LIT>" ] <EOL> else : <EOL> doc = "<STR_LIT>" <EOL> name += "<STR_LIT:U+0020>" * ( first_column_len - len ( name ) ) <EOL> description += "<STR_LIT>" % ( name , doc ) <EOL> return description <EOL> def _compose_action_description ( action_fn ) : <EOL> description = "<STR_LIT>" <EOL> if action_fn . __doc__ : <EOL> parsed_doc = info . parse_docstring ( action_fn . __doc__ ) <EOL> short = parsed_doc . get ( "<STR_LIT>" ) <EOL> long = parsed_doc . get ( "<STR_LIT>" ) <EOL> description = "<STR_LIT>" % ( short , long ) if long else short <EOL> return description <EOL> def _add_command_parsers ( categories , subparsers ) : <EOL> subparsers . _parser_class = CategoryParser <EOL> parser = subparsers . add_parser ( "<STR_LIT:version>" ) <EOL> parser = subparsers . add_parser ( "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , nargs = "<STR_LIT:?>" ) <EOL> for category in categories : <EOL> command_object = categories [ category ] ( ) <EOL> descr = _compose_category_description ( categories [ category ] ) <EOL> parser = subparsers . add_parser ( <EOL> category , description = descr , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter ) <EOL> parser . set_defaults ( command_object = command_object ) <EOL> category_subparsers = parser . add_subparsers ( dest = "<STR_LIT:action>" ) <EOL> for method_name , method in _methods_of ( command_object ) : <EOL> descr = _compose_action_description ( method ) <EOL> parser = category_subparsers . add_parser ( <EOL> getattr ( method , "<STR_LIT>" , method_name ) , <EOL> formatter_class = argparse . RawDescriptionHelpFormatter , <EOL> description = descr , help = descr ) <EOL> action_kwargs = [ ] <EOL> for args , kwargs in getattr ( method , "<STR_LIT:args>" , [ ] ) : <EOL> kwargs . setdefault ( "<STR_LIT>" , args [ <NUM_LIT:0> ] [ <NUM_LIT:2> : ] ) <EOL> action_kwargs . append ( kwargs [ "<STR_LIT>" ] ) <EOL> kwargs [ "<STR_LIT>" ] = "<STR_LIT>" + kwargs [ "<STR_LIT>" ] <EOL> parser . add_argument ( * args , ** kwargs ) <EOL> parser . set_defaults ( action_fn = method ) <EOL> parser . set_defaults ( action_kwargs = action_kwargs ) <EOL> parser . add_argument ( "<STR_LIT>" , nargs = "<STR_LIT:*>" ) <EOL> def validate_deprecated_args ( argv , fn ) : <EOL> if ( len ( argv ) > <NUM_LIT:3> <EOL> and ( argv [ <NUM_LIT:2> ] == fn . __name__ ) <EOL> and getattr ( fn , "<STR_LIT>" , None ) ) : <EOL> for item in fn . deprecated_args : <EOL> if item in argv [ <NUM_LIT:3> : ] : <EOL> LOG . warning ( "<STR_LIT>" % ( item , <EOL> fn . __name__ ) ) <EOL> def run ( argv , categories ) : <EOL> parser = lambda subparsers : _add_command_parsers ( categories , subparsers ) <EOL> category_opt = cfg . SubCommandOpt ( "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> handler = parser ) <EOL> CONF . register_cli_opt ( category_opt ) <EOL> help_msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> CONF . register_cli_opt ( cfg . ListOpt ( "<STR_LIT>" , <EOL> default = os . environ . get ( <EOL> "<STR_LIT>" ) , <EOL> help = help_msg ) ) <EOL> try : <EOL> CONF ( argv [ <NUM_LIT:1> : ] , project = "<STR_LIT>" , version = version . version_string ( ) , <EOL> default_config_files = find_config_files ( CONFIG_SEARCH_PATHS ) ) <EOL> logging . setup ( "<STR_LIT>" ) <EOL> if not CONF . get ( "<STR_LIT>" ) : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> requests_log = logging . getLogger ( "<STR_LIT>" ) . logger <EOL> requests_log . setLevel ( logging . WARNING ) <EOL> urllib3_log = logging . getLogger ( "<STR_LIT>" ) . logger <EOL> urllib3_log . setLevel ( logging . WARNING ) <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> for warning in ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) : <EOL> warning_cls = getattr ( urllib3 . exceptions , warning , None ) <EOL> if warning_cls is not None : <EOL> urllib3 . disable_warnings ( warning_cls ) <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> boto_log = logging . getLogger ( "<STR_LIT>" ) . logger <EOL> boto_log . setLevel ( logging . CRITICAL ) <EOL> except cfg . ConfigFilesNotFoundError : <EOL> cfgfile = CONF . config_file [ - <NUM_LIT:1> ] if CONF . config_file else None <EOL> if cfgfile and not os . access ( cfgfile , os . R_OK ) : <EOL> st = os . stat ( cfgfile ) <EOL> print ( _ ( "<STR_LIT>" ) % cfgfile ) <EOL> try : <EOL> os . execvp ( "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" % st . st_uid ] + sys . argv ) <EOL> except Exception : <EOL> print ( _ ( "<STR_LIT>" ) ) <EOL> print ( _ ( "<STR_LIT>" ) % argv [ <NUM_LIT:0> ] ) <EOL> return ( <NUM_LIT:2> ) <EOL> if CONF . category . name == "<STR_LIT:version>" : <EOL> print ( version . version_string ( ) ) <EOL> return ( <NUM_LIT:0> ) <EOL> if CONF . category . name == "<STR_LIT>" : <EOL> print ( _generate_bash_completion_script ( ) ) <EOL> return ( <NUM_LIT:0> ) <EOL> fn = CONF . category . action_fn <EOL> fn_args = [ encodeutils . safe_decode ( arg ) <EOL> for arg in CONF . category . action_args ] <EOL> fn_kwargs = { } <EOL> for k in CONF . category . action_kwargs : <EOL> v = getattr ( CONF . category , "<STR_LIT>" + k ) <EOL> if v is None : <EOL> continue <EOL> if isinstance ( v , six . string_types ) : <EOL> v = encodeutils . safe_decode ( v ) <EOL> fn_kwargs [ k ] = v <EOL> try : <EOL> validate_args ( fn , * fn_args , ** fn_kwargs ) <EOL> except MissingArgs as e : <EOL> print ( fn . __doc__ ) <EOL> CONF . print_help ( ) <EOL> print ( "<STR_LIT>" ) <EOL> for missing in e . missing : <EOL> for arg in fn . args : <EOL> if arg [ <NUM_LIT:1> ] . get ( "<STR_LIT>" , "<STR_LIT>" ) . endswith ( missing ) : <EOL> print ( "<STR_LIT:U+0020>" + arg [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> break <EOL> return ( <NUM_LIT:1> ) <EOL> try : <EOL> for path in CONF . plugin_paths or [ ] : <EOL> discover . load_plugins ( path ) <EOL> validate_deprecated_args ( argv , fn ) <EOL> if getattr ( fn , "<STR_LIT>" , False ) : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> ret = fn ( * fn_args , ** fn_kwargs ) <EOL> else : <EOL> ret = fn ( * fn_args , ** fn_kwargs ) <EOL> return ( ret ) <EOL> except ( IOError , TypeError , ValueError , exceptions . DeploymentNotFound , <EOL> exceptions . TaskNotFound , jsonschema . ValidationError ) as e : <EOL> if logging . is_debug ( ) : <EOL> LOG . exception ( e ) <EOL> print ( e ) <EOL> return <NUM_LIT:1> <EOL> except sqlalchemy . exc . OperationalError as e : <EOL> if logging . is_debug ( ) : <EOL> LOG . exception ( e ) <EOL> print ( e ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( CONF . database . connection ) <EOL> return <NUM_LIT:1> <EOL> except Exception : <EOL> print ( _ ( "<STR_LIT>" ) ) <EOL> raise <EOL> def _generate_bash_completion_script ( ) : <EOL> from rally . cli import main <EOL> bash_data = """<STR_LIT>""" <EOL> completion = [ ] <EOL> for category , cmds in main . categories . items ( ) : <EOL> for name , command in _methods_of ( cmds ) : <EOL> command_name = getattr ( command , "<STR_LIT>" , name ) <EOL> args_list = [ ] <EOL> for arg in getattr ( command , "<STR_LIT:args>" , [ ] ) : <EOL> if getattr ( command , "<STR_LIT>" , [ ] ) : <EOL> if arg [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] not in command . deprecated_args : <EOL> args_list . append ( arg [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> args_list . append ( arg [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> args = "<STR_LIT:U+0020>" . join ( args_list ) <EOL> completion . append ( """<STR_LIT>""" . format ( <EOL> cat = category , cmd = command_name , args = args ) ) <EOL> return bash_data % { "<STR_LIT:data>" : "<STR_LIT>" . join ( sorted ( completion ) ) } </s>
