<s> from __future__ import absolute_import , print_function <EOL> import sys <EOL> import logging <EOL> import textwrap <EOL> import traceback <EOL> from testmill import argparse , console , ravello , error , _version <EOL> from testmill . state import env <EOL> usage = textwrap . dedent ( """<STR_LIT>""" ) <EOL> description = textwrap . dedent ( """<STR_LIT>""" ) <EOL> from testmill import ( command_login , command_ps , command_lint , <EOL> command_logout , command_run , command_ssh , <EOL> command_save , command_restore , command_clean ) <EOL> subcommands = { <EOL> '<STR_LIT>' : ( command_login . do_login , command_login . add_args ) , <EOL> '<STR_LIT>' : ( command_logout . do_logout , command_logout . add_args ) , <EOL> '<STR_LIT>' : ( command_ps . do_ps , command_ps . add_args ) , <EOL> '<STR_LIT>' : ( command_run . do_run , command_run . add_args ) , <EOL> '<STR_LIT>' : ( command_ssh . do_ssh , command_ssh . add_args ) , <EOL> '<STR_LIT>' : ( command_lint . do_lint , command_lint . add_args ) , <EOL> '<STR_LIT>' : ( command_save . do_save , command_save . add_args ) , <EOL> '<STR_LIT>' : ( command_restore . do_restore , command_restore . add_args ) , <EOL> '<STR_LIT>' : ( command_clean . do_clean , command_clean . add_args ) <EOL> } <EOL> def create_parser ( ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> sys . argv [ <NUM_LIT:0> ] = sys . argv [ <NUM_LIT:0> ] . rstrip ( '<STR_LIT>' ) <EOL> parser = argparse . ArgumentParser ( usage = usage , description = description , <EOL> add_help = False ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> return parser <EOL> def parse_args ( parser , argv = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> args = parser . parse_args ( argv ) <EOL> except argparse . ParseError as e : <EOL> args = e . namespace <EOL> if not ( args . help or args . version ) and not args . subcmd : <EOL> console . write_err ( parser . format_usage ( ) ) <EOL> console . error ( str ( e ) ) <EOL> error . exit ( error . EX_USAGE ) <EOL> if args . help and not args . subcmd : <EOL> console . write_err ( parser . format_help ( ) ) <EOL> error . exit ( error . EX_OK ) <EOL> if args . version and not args . subcmd : <EOL> console . writeln_err ( _version . version_string ) <EOL> error . exit ( error . EX_OK ) <EOL> subcmd = args . subcmd <EOL> if subcmd not in subcommands : <EOL> console . error ( "<STR_LIT>" , subcmd ) <EOL> error . exit ( error . EX_USAGE ) <EOL> add_args = subcommands [ subcmd ] [ <NUM_LIT:1> ] <EOL> add_args ( parser ) <EOL> try : <EOL> args = parser . parse_args ( argv ) <EOL> except argparse . ParseError as e : <EOL> args = e . namespace <EOL> if not args . help : <EOL> console . write_err ( parser . format_usage ( ) ) <EOL> console . error ( str ( e ) ) <EOL> error . exit ( error . EX_USAGE ) <EOL> if args . help : <EOL> console . write_err ( parser . format_help ( ) ) <EOL> error . exit ( error . EX_OK ) <EOL> return args <EOL> def create_environment ( args ) : <EOL> """<STR_LIT>""" <EOL> env . username = args . user <EOL> env . password = args . password <EOL> env . service_url = args . service_url <EOL> env . quiet = args . quiet <EOL> env . verbose = args . verbose <EOL> env . manifest = args . manifest <EOL> env . debug = args . debug <EOL> env . always_confirm = args . yes <EOL> env . args = args <EOL> env . api = ravello . RavelloClient ( env . username , env . password , env . service_url ) <EOL> def setup_logging ( ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> if getattr ( logger , '<STR_LIT>' , False ) : <EOL> return <EOL> logger . setLevel ( logging . DEBUG if env . debug and env . verbose <EOL> else logging . INFO ) <EOL> handler = logging . StreamHandler ( console ) <EOL> template = '<STR_LIT>' <EOL> handler . setFormatter ( logging . Formatter ( template ) ) <EOL> logger . addHandler ( handler ) <EOL> logger . _testmill_configured = True <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> console . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> error . exit ( <NUM_LIT:1> ) <EOL> parser = create_parser ( ) <EOL> args = parse_args ( parser , argv ) <EOL> create_environment ( args ) <EOL> setup_logging ( ) <EOL> command = subcommands [ args . subcmd ] [ <NUM_LIT:0> ] <EOL> try : <EOL> ret = command ( args , env ) <EOL> except KeyboardInterrupt : <EOL> console . complete_partial_line ( ) <EOL> console . writeln ( '<STR_LIT>' ) <EOL> ret = error . EX_INTERRUPTED <EOL> except SystemExit as e : <EOL> console . complete_partial_line ( ) <EOL> if env . debug : <EOL> console . error ( '<STR_LIT>' ) <EOL> lines = traceback . format_exception ( * sys . exc_info ( ) ) <EOL> console . writeln_err ( '<STR_LIT>' ) <EOL> console . writeln_err ( '<STR_LIT>' . join ( lines ) ) <EOL> ret = e [ <NUM_LIT:0> ] <EOL> except Exception as e : <EOL> console . complete_partial_line ( ) <EOL> console . error ( str ( e ) ) <EOL> if env . debug : <EOL> lines = [ '<STR_LIT>' ] <EOL> lines += traceback . format_exception ( * sys . exc_info ( ) ) <EOL> console . writeln_err ( ) <EOL> console . writeln_err ( '<STR_LIT>' . join ( lines ) ) <EOL> console . writeln_err ( '<STR_LIT>' . format ( env ) ) <EOL> ret = getattr ( e , '<STR_LIT>' , error . EX_SOFTWARE ) <EOL> return ret <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
