<s> import getpass <EOL> import sys <EOL> from functools import wraps <EOL> import texttable as tt <EOL> from uws . lib . terminalsize import terminalsize as console <EOL> import cli_parser <EOL> from uws import UWS <EOL> debug = False <EOL> def handle_error ( handler ) : <EOL> @ wraps ( handler ) <EOL> def handle ( self , * args , ** kwargs ) : <EOL> try : <EOL> return handler ( self , * args , ** kwargs ) <EOL> except UWS . UWSError as e : <EOL> if not debug : <EOL> print "<STR_LIT>" % e . msg <EOL> return <EOL> else : <EOL> print "<STR_LIT>" % e . msg <EOL> print e . raw <EOL> raise <EOL> return handle <EOL> @ handle_error <EOL> def list_jobs ( url , user_name , password , phases , after = None , last = None ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> filters = { } <EOL> if phases : <EOL> filters [ '<STR_LIT>' ] = phases <EOL> if after : <EOL> filters [ '<STR_LIT>' ] = after <EOL> if last : <EOL> filters [ '<STR_LIT>' ] = last <EOL> jobs = uws_client . get_job_list ( filters ) <EOL> job_phases = UWS . models . JobPhases <EOL> rows = [ [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> for job in jobs . job_reference : <EOL> if not phases or jobs . version == "<STR_LIT>" : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> else : <EOL> if job_phases . COMPLETED in phases and job_phases . COMPLETED in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . PENDING in phases and job_phases . PENDING in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . QUEUED in phases and job_phases . QUEUED in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . EXECUTING in phases and job_phases . EXECUTING in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . ERROR in phases and job_phases . ERROR in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . ABORTED in phases and job_phases . ABORTED in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . UNKNOWN in phases and job_phases . UNKNOWN in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . HELD in phases and job_phases . HELD in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . SUSPENDED in phases and job_phases . SUSPENDED in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> if job_phases . ARCHIVED in phases and job_phases . ARCHIVED in job . phase : <EOL> _register_job_reference_for_table ( rows , job ) <EOL> ( console_width , console_height ) = console . get_terminal_size ( ) <EOL> table = tt . Texttable ( max_width = console_width ) <EOL> table . set_deco ( tt . Texttable . HEADER ) <EOL> table . set_cols_dtype ( [ '<STR_LIT:t>' , '<STR_LIT:t>' , '<STR_LIT:t>' ] ) <EOL> table . add_rows ( rows ) <EOL> print "<STR_LIT>" % user_name <EOL> print table . draw ( ) <EOL> print "<STR_LIT>" % ( len ( rows ) - <NUM_LIT:1> ) <EOL> def _register_job_reference_for_table ( rows , jobref ) : <EOL> if ( jobref . reference . href is not None ) : <EOL> job_id = jobref . reference . href . rsplit ( "<STR_LIT:/>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> else : <EOL> job_id = jobref . id <EOL> rows . append ( [ job_id , jobref . id , '<STR_LIT:U+002CU+0020>' . join ( jobref . phase ) ] ) <EOL> @ handle_error <EOL> def show_job ( url , user_name , password , id , wait , phase ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> job = uws_client . get_job ( id , wait , phase ) <EOL> if wait and job . version != "<STR_LIT>" : <EOL> print "<STR_LIT>" % job . version <EOL> _print_job ( job ) <EOL> @ handle_error <EOL> def show_phase ( url , user_name , password , id ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> phase = uws_client . get_phase ( id ) <EOL> print ( phase ) <EOL> @ handle_error <EOL> def new_job ( url , user_name , password , parameters = { } , run = False ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> job = uws_client . new_job ( parameters ) <EOL> if run : <EOL> job = uws_client . run_job ( job . job_id ) <EOL> ( console_width , console_height ) = console . get_terminal_size ( ) <EOL> _print_job ( job ) <EOL> print "<STR_LIT:\n>" <EOL> print "<STR_LIT:*>" * ( console_width - <NUM_LIT:1> ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % job . job_id <EOL> print "<STR_LIT>" % ( url , user_name , job . job_id ) <EOL> print "<STR_LIT:*>" * ( console_width - <NUM_LIT:1> ) <EOL> @ handle_error <EOL> def set_parameters_job ( url , user_name , password , id , parameters = { } ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> if len ( parameters ) == <NUM_LIT:0> : <EOL> job = uws_client . get_job ( id ) <EOL> else : <EOL> job = uws_client . set_parameters_job ( id , parameters ) <EOL> _print_job ( job ) <EOL> @ handle_error <EOL> def run_job ( url , user_name , password , id ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> job = uws_client . run_job ( id ) <EOL> _print_job ( job ) <EOL> @ handle_error <EOL> def abort_job ( url , user_name , password , id ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> job = uws_client . abort_job ( id ) <EOL> _print_job ( job ) <EOL> @ handle_error <EOL> def delete_job ( url , user_name , password , id ) : <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> success = uws_client . delete_job ( id ) <EOL> if success : <EOL> print "<STR_LIT>" % ( id ) <EOL> @ handle_error <EOL> def results_job ( url , user_name , password , id , result_id , user_file_base ) : <EOL> def print_progress ( total_size , current ) : <EOL> if total_size : <EOL> sys . stdout . write ( "<STR_LIT>" % current ) <EOL> sys . stdout . flush ( ) <EOL> else : <EOL> sys . stdout . write ( "<STR_LIT>" % current ) <EOL> sys . stdout . flush ( ) <EOL> uws_client = UWS . client . Client ( url = url , user = user_name , password = password ) <EOL> job = uws_client . get_job ( id ) <EOL> if len ( job . results ) > <NUM_LIT:1> and not result_id : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' , '<STR_LIT:U+002C>' . join ( [ result . id for result in job . results ] ) <EOL> file_base = job . job_id <EOL> for parameter in job . parameters : <EOL> if parameter . id == '<STR_LIT>' : <EOL> file_base = parameter . value <EOL> break <EOL> if user_file_base : <EOL> file_base = user_file_base <EOL> retrieved = False <EOL> for result in job . results : <EOL> if not result_id or result_id == result . id : <EOL> filename = file_base + '<STR_LIT:.>' + result . id <EOL> url = str ( result . reference ) <EOL> print "<STR_LIT>" % ( result . id , filename ) <EOL> uws_client . connection . download_file ( url , user_name , password , filename , callback = print_progress ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" % ( filename ) <EOL> retrieved = True <EOL> if not retrieved : <EOL> if result_id : <EOL> print "<STR_LIT>" % ( result_id , job . job_id ) <EOL> else : <EOL> print "<STR_LIT>" % ( job . job_id ) <EOL> print "<STR_LIT>" % ( job . job_id ) <EOL> def _print_job ( job ) : <EOL> rows = [ [ "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> rows . append ( [ "<STR_LIT>" , job . job_id ] ) <EOL> if ( job . run_id ) : <EOL> rows . append ( [ "<STR_LIT>" , job . run_id ] ) <EOL> rows . append ( [ "<STR_LIT>" , job . owner_id ] ) <EOL> rows . append ( [ "<STR_LIT>" , "<STR_LIT:U+002CU+0020>" . join ( job . phase ) ] ) <EOL> if ( job . quote ) : <EOL> rows . append ( [ "<STR_LIT>" , job . quote ] ) <EOL> rows . append ( [ "<STR_LIT>" , job . start_time ] ) <EOL> rows . append ( [ "<STR_LIT>" , job . end_time ] ) <EOL> rows . append ( [ "<STR_LIT>" , job . execution_duration ] ) <EOL> rows . append ( [ "<STR_LIT>" , job . destruction ] ) <EOL> for param in job . parameters : <EOL> rows . append ( [ "<STR_LIT>" + param . id , param . value ] ) <EOL> for result in job . results : <EOL> rows . append ( [ "<STR_LIT>" + result . id , result . reference ] ) <EOL> try : <EOL> if ( job . error_summary ) : <EOL> rows . append ( [ "<STR_LIT>" , "<STR_LIT>" . join ( job . error_summary . messages ) ] ) <EOL> except : <EOL> pass <EOL> for info in job . job_info : <EOL> rows . append ( [ "<STR_LIT>" , unicode ( info ) ] ) <EOL> ( console_width , console_height ) = console . get_terminal_size ( ) <EOL> fields = [ row [ <NUM_LIT:0> ] for row in rows ] <EOL> max_field_len = len ( max ( fields , key = len ) ) <EOL> table = tt . Texttable ( max_width = console_width ) <EOL> table . set_deco ( tt . Texttable . HEADER ) <EOL> table . set_cols_dtype ( [ '<STR_LIT:t>' , '<STR_LIT:t>' ] ) <EOL> table . set_cols_width ( [ max_field_len , console_width - max_field_len - <NUM_LIT:4> ] ) <EOL> table . add_rows ( rows ) <EOL> print table . draw ( ) <EOL> def _check_job_wait_args ( arguments ) : <EOL> wait = arguments . wait <EOL> phase = arguments . phase <EOL> if wait is None and phase is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if phase : <EOL> if phase . upper ( ) in UWS . models . JobPhases . active_phases : <EOL> phase = phase . upper ( ) ; <EOL> else : <EOL> active_phases = '<STR_LIT:U+002CU+0020>' . join ( UWS . models . JobPhases . active_phases ) <EOL> raise RuntimeError ( "<STR_LIT>" + phase + "<STR_LIT>" + active_phases ) <EOL> return wait , phase <EOL> def _check_job_parameter_args ( arguments ) : <EOL> argument_list = { } <EOL> for argument in arguments : <EOL> argument_pair = argument . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> if len ( argument_pair ) != <NUM_LIT:2> : <EOL> raise RuntimeError ( '<STR_LIT>' % ( "<STR_LIT:U+002CU+0020>" . join ( argument_pair ) ) ) <EOL> if argument_pair [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> argument_pair [ <NUM_LIT:0> ] = "<STR_LIT>" <EOL> if argument_pair [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> argument_pair [ <NUM_LIT:0> ] = "<STR_LIT>" <EOL> argument_list [ argument_pair [ <NUM_LIT:0> ] ] = argument_pair [ <NUM_LIT:1> ] <EOL> return argument_list <EOL> def _check_joblist_after ( argument ) : <EOL> return argument <EOL> def _check_joblist_last ( argument ) : <EOL> return argument <EOL> def main ( ) : <EOL> global debug <EOL> parser = cli_parser . build_argparse ( ) <EOL> arguments = parser . parse_args ( ) <EOL> if arguments . dbg : <EOL> debug = True <EOL> if arguments . P : <EOL> if arguments . password : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> arguments . password = getpass . getpass ( "<STR_LIT>" ) <EOL> phases = [ ] <EOL> if arguments . command == "<STR_LIT:list>" : <EOL> if arguments . completed : <EOL> phases . append ( UWS . models . JobPhases . COMPLETED ) <EOL> if arguments . pending : <EOL> phases . append ( UWS . models . JobPhases . PENDING ) <EOL> if arguments . queued : <EOL> phases . append ( UWS . models . JobPhases . QUEUED ) <EOL> if arguments . executing : <EOL> phases . append ( UWS . models . JobPhases . EXECUTING ) <EOL> if arguments . error : <EOL> phases . append ( UWS . models . JobPhases . ERROR ) <EOL> if arguments . aborted : <EOL> phases . append ( UWS . models . JobPhases . ABORTED ) <EOL> if arguments . unknown : <EOL> phases . append ( UWS . models . JobPhases . UNKNOWN ) <EOL> if arguments . held : <EOL> phases . append ( UWS . models . JobPhases . HELD ) <EOL> if arguments . suspended : <EOL> phases . append ( UWS . models . JobPhases . SUSPENDED ) <EOL> if arguments . archived : <EOL> phases . append ( UWS . models . JobPhases . ARCHIVED ) <EOL> after = None <EOL> if arguments . after : <EOL> after = _check_joblist_after ( arguments . after ) <EOL> last = None <EOL> if arguments . last : <EOL> last = _check_joblist_last ( arguments . last ) <EOL> list_jobs ( arguments . host , arguments . user , arguments . password , phases , after , last ) <EOL> if arguments . command == "<STR_LIT>" : <EOL> if arguments . job_command == "<STR_LIT>" : <EOL> wait , phase = _check_job_wait_args ( arguments ) <EOL> show_job ( arguments . host , arguments . user , arguments . password , arguments . id , wait , phase ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> show_phase ( arguments . host , arguments . user , arguments . password , arguments . id ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> job_parameters = _check_job_parameter_args ( arguments . job_parameters ) <EOL> new_job ( arguments . host , arguments . user , arguments . password , job_parameters , arguments . run ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> job_parameters = _check_job_parameter_args ( arguments . job_parameters ) <EOL> set_parameters_job ( arguments . host , arguments . user , arguments . password , arguments . id , job_parameters ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> run_job ( arguments . host , arguments . user , arguments . password , arguments . id ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> abort_job ( arguments . host , arguments . user , arguments . password , arguments . id ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> delete_job ( arguments . host , arguments . user , arguments . password , arguments . id ) <EOL> elif arguments . job_command == "<STR_LIT>" : <EOL> results_job ( arguments . host , arguments . user , arguments . password , arguments . id , arguments . result_id , arguments . file_base ) <EOL> else : <EOL> print "<STR_LIT>" % ( arguments . job_command ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
