<s> """<STR_LIT>""" <EOL> import argparse <EOL> from contextlib import closing <EOL> import copy <EOL> import inspect <EOL> import itertools <EOL> import os <EOL> import shlex <EOL> import sys <EOL> import time <EOL> import tempfile <EOL> import textwrap <EOL> import argcomplete <EOL> from argcomplete . completers import FilesCompleter , ChoicesCompleter <EOL> from codalab . bundles import ( <EOL> get_bundle_subclass , <EOL> UPLOADED_TYPES , <EOL> ) <EOL> from codalab . bundles . make_bundle import MakeBundle <EOL> from codalab . bundles . uploaded_bundle import UploadedBundle <EOL> from codalab . bundles . run_bundle import RunBundle <EOL> from codalab . common import ( <EOL> precondition , <EOL> State , <EOL> PermissionError , <EOL> UsageError , <EOL> CODALAB_VERSION <EOL> ) <EOL> from codalab . lib import ( <EOL> metadata_util , <EOL> file_util , <EOL> path_util , <EOL> zip_util , <EOL> spec_util , <EOL> worksheet_util , <EOL> cli_util , <EOL> formatting , <EOL> ui_actions , <EOL> ) <EOL> from codalab . objects . permission import permission_str , group_permissions_str <EOL> from codalab . objects . work_manager import Worker <EOL> from codalab . machines . remote_machine import RemoteMachine <EOL> from codalab . machines . local_machine import LocalMachine <EOL> from codalab . client . local_bundle_client import LocalBundleClient <EOL> from codalab . server . rpc_file_handle import RPCFileHandle <EOL> from codalab . lib . formatting import contents_str <EOL> from codalab . lib . completers import ( <EOL> CodaLabCompleter , <EOL> WorksheetsCompleter , <EOL> BundlesCompleter , <EOL> AddressesCompleter , <EOL> GroupsCompleter , <EOL> UnionCompleter , <EOL> NullCompleter , <EOL> TargetsCompleter , <EOL> require_not_headless , <EOL> ) <EOL> from codalab . lib . bundle_store import ( <EOL> MultiDiskBundleStore <EOL> ) <EOL> GLOBAL_SPEC_FORMAT = "<STR_LIT>" <EOL> ADDRESS_SPEC_FORMAT = "<STR_LIT>" <EOL> TARGET_SPEC_FORMAT = '<STR_LIT>' % ( os . sep , ) <EOL> ALIASED_TARGET_SPEC_FORMAT = '<STR_LIT>' + TARGET_SPEC_FORMAT <EOL> BUNDLE_SPEC_FORMAT = '<STR_LIT>' <EOL> GLOBAL_BUNDLE_SPEC_FORMAT = '<STR_LIT>' <EOL> WORKSHEET_SPEC_FORMAT = GLOBAL_SPEC_FORMAT <EOL> GROUP_SPEC_FORMAT = '<STR_LIT>' <EOL> PERMISSION_SPEC_FORMAT = '<STR_LIT>' <EOL> UUID_POST_FUNC = '<STR_LIT>' <EOL> BUNDLE_COMMANDS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:info>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> WORKSHEET_COMMANDS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> GROUP_AND_PERMISSION_COMMANDS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> OTHER_COMMANDS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:status>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class ArgumentError ( Exception ) : <EOL> pass <EOL> class CodaLabArgumentParser ( argparse . ArgumentParser ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . cli = kwargs . pop ( '<STR_LIT>' ) <EOL> argparse . ArgumentParser . __init__ ( self , * args , ** kwargs ) <EOL> def print_help ( self , out_file = None ) : <EOL> if out_file is None : <EOL> out_file = self . cli . stdout <EOL> self . _print_message ( self . format_help ( ) , out_file ) <EOL> def error ( self , message ) : <EOL> self . print_usage ( self . cli . stderr ) <EOL> if self . cli . headless : <EOL> raise ArgumentError ( message ) <EOL> else : <EOL> self . exit ( <NUM_LIT:2> , '<STR_LIT>' % ( self . prog , message ) ) <EOL> class AliasedSubParsersAction ( argparse . _SubParsersAction ) : <EOL> """<STR_LIT>""" <EOL> class _AliasedPseudoAction ( argparse . Action ) : <EOL> def __init__ ( self , name , aliases = None , help = None ) : <EOL> dest = name <EOL> sup = super ( AliasedSubParsersAction . _AliasedPseudoAction , self ) <EOL> sup . __init__ ( option_strings = [ ] , dest = dest , help = help ) <EOL> def add_parser ( self , name , ** kwargs ) : <EOL> aliases = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> parser = super ( AliasedSubParsersAction , self ) . add_parser ( name , ** kwargs ) <EOL> if '<STR_LIT>' in os . environ : <EOL> return parser <EOL> for alias in aliases : <EOL> self . _name_parser_map [ alias ] = parser <EOL> if '<STR_LIT>' in kwargs : <EOL> help = kwargs . pop ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> self . _choices_actions . pop ( ) <EOL> pseudo_action = self . _AliasedPseudoAction ( name , aliases , help ) <EOL> self . _choices_actions . append ( pseudo_action ) <EOL> return parser <EOL> class Commands ( object ) : <EOL> """<STR_LIT>""" <EOL> commands = { } <EOL> class Argument ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> class Command ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , aliases , help , arguments , function ) : <EOL> self . name = name <EOL> self . aliases = aliases <EOL> self . help = help if isinstance ( help , list ) else [ help ] <EOL> self . arguments = arguments <EOL> self . function = function <EOL> @ classmethod <EOL> def command ( cls , name , aliases = ( ) , help = '<STR_LIT>' , arguments = ( ) ) : <EOL> """<STR_LIT>""" <EOL> def register_command ( function ) : <EOL> cls . commands [ name ] = cls . Command ( name , aliases , help , arguments , function ) <EOL> return function <EOL> return register_command <EOL> @ classmethod <EOL> def help_text ( cls , verbose ) : <EOL> def command_name ( command ) : <EOL> name = command <EOL> aliases = cls . commands [ command ] . aliases <EOL> if aliases : <EOL> name += '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( list ( aliases ) ) <EOL> return name <EOL> available_other_commands = filter ( <EOL> lambda command : command in cls . commands , OTHER_COMMANDS ) <EOL> indent = <NUM_LIT:2> <EOL> max_length = max ( <EOL> len ( command_name ( command ) ) for command in itertools . chain ( <EOL> BUNDLE_COMMANDS , <EOL> WORKSHEET_COMMANDS , <EOL> GROUP_AND_PERMISSION_COMMANDS , <EOL> available_other_commands ) <EOL> ) <EOL> def command_help_text ( command ) : <EOL> name = command_name ( command ) <EOL> command_obj = cls . commands [ command ] <EOL> def render_args ( arguments ) : <EOL> table = [ ] <EOL> for arg in arguments : <EOL> if len ( arg . args ) == <NUM_LIT:1> : <EOL> table . append ( [ arg . args [ <NUM_LIT:0> ] , arg . kwargs [ '<STR_LIT>' ] ] ) <EOL> else : <EOL> table . append ( [ arg . args [ <NUM_LIT:0> ] + '<STR_LIT:U+002CU+0020>' + arg . args [ <NUM_LIT:1> ] , arg . kwargs [ '<STR_LIT>' ] ] ) <EOL> if len ( table ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> width = max ( len ( row [ <NUM_LIT:0> ] ) for row in table ) <EOL> return [ ( '<STR_LIT:U+0020>' * ( indent * <NUM_LIT:2> ) ) + '<STR_LIT>' ] + [ ( '<STR_LIT:U+0020>' * ( indent * <NUM_LIT:3> ) + '<STR_LIT>' + str ( width ) + '<STR_LIT>' ) % ( row [ <NUM_LIT:0> ] , row [ <NUM_LIT:1> ] ) for row in table ] + [ '<STR_LIT>' ] <EOL> if verbose : <EOL> return '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * indent , <EOL> name , <EOL> '<STR_LIT:\n>' . join ( ( '<STR_LIT:U+0020>' * ( indent * <NUM_LIT:2> ) ) + line for line in command_obj . help ) , <EOL> '<STR_LIT:\n>' . join ( render_args ( command_obj . arguments ) ) <EOL> ) <EOL> else : <EOL> return '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' * indent , <EOL> name , <EOL> '<STR_LIT:U+0020>' * ( indent + max_length - len ( name ) ) , <EOL> command_obj . help [ <NUM_LIT:0> ] , <EOL> ) <EOL> def command_group_help_text ( commands ) : <EOL> return '<STR_LIT:\n>' . join ( [ command_help_text ( command ) for command in commands ] ) <EOL> return textwrap . dedent ( """<STR_LIT>""" ) . format ( <EOL> bundle_commands = command_group_help_text ( BUNDLE_COMMANDS ) , <EOL> worksheet_commands = command_group_help_text ( WORKSHEET_COMMANDS ) , <EOL> group_and_permission_commands = command_group_help_text ( GROUP_AND_PERMISSION_COMMANDS ) , <EOL> other_commands = command_group_help_text ( available_other_commands ) , <EOL> ) . strip ( ) <EOL> @ classmethod <EOL> def build_parser ( cls , cli ) : <EOL> """<STR_LIT>""" <EOL> parser = CodaLabArgumentParser ( prog = '<STR_LIT>' , cli = cli , add_help = False , formatter_class = argparse . RawTextHelpFormatter ) <EOL> parser . register ( '<STR_LIT:action>' , '<STR_LIT>' , AliasedSubParsersAction ) <EOL> subparsers = parser . add_subparsers ( dest = '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> for command in cls . commands . itervalues ( ) : <EOL> help = '<STR_LIT:\n>' . join ( command . help ) <EOL> subparser = subparsers . add_parser ( command . name , cli = cli , help = help , description = help , aliases = command . aliases , add_help = True , formatter_class = argparse . RawTextHelpFormatter ) <EOL> for argument in command . arguments : <EOL> argument_kwargs = argument . kwargs . copy ( ) <EOL> completer = argument_kwargs . pop ( '<STR_LIT>' , None ) <EOL> argument = subparser . add_argument ( * argument . args , ** argument_kwargs ) <EOL> if completer is not None : <EOL> completer_class = completer if inspect . isclass ( completer ) else completer . __class__ <EOL> if issubclass ( completer_class , CodaLabCompleter ) : <EOL> completer = completer ( cli ) <EOL> argument . completer = completer <EOL> elif cli . headless and '<STR_LIT>' not in argument_kwargs : <EOL> argument . completer = NullCompleter <EOL> subparser . set_defaults ( function = command . function ) <EOL> return parser <EOL> @ staticmethod <EOL> def metadata_arguments ( bundle_subclasses ) : <EOL> """<STR_LIT>""" <EOL> arguments = [ ] <EOL> added_keys = set ( ) <EOL> for bundle_subclass in bundle_subclasses : <EOL> help_suffix = '<STR_LIT>' % ( bundle_subclass . BUNDLE_TYPE , ) if bundle_subclass . BUNDLE_TYPE else '<STR_LIT>' <EOL> for spec in bundle_subclass . get_user_defined_metadata ( ) : <EOL> if spec . key not in added_keys : <EOL> added_keys . add ( spec . key ) <EOL> args = [ ] <EOL> if spec . short_key : <EOL> args . append ( '<STR_LIT>' % spec . short_key ) <EOL> args . append ( '<STR_LIT>' % spec . key . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) ) <EOL> kwargs = { <EOL> '<STR_LIT>' : metadata_util . metadata_key_to_argument ( spec . key ) , <EOL> '<STR_LIT>' : spec . description + help_suffix , <EOL> } <EOL> if spec . completer is not None : <EOL> kwargs [ '<STR_LIT>' ] = spec . completer <EOL> if issubclass ( spec . type , list ) : <EOL> kwargs [ '<STR_LIT:type>' ] = str <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:*>' <EOL> kwargs [ '<STR_LIT>' ] = spec . metavar <EOL> elif issubclass ( spec . type , basestring ) : <EOL> kwargs [ '<STR_LIT:type>' ] = str <EOL> kwargs [ '<STR_LIT>' ] = spec . metavar <EOL> elif spec . type is bool : <EOL> kwargs [ '<STR_LIT:action>' ] = '<STR_LIT:store_true>' <EOL> else : <EOL> kwargs [ '<STR_LIT:type>' ] = spec . type <EOL> kwargs [ '<STR_LIT>' ] = spec . metavar <EOL> arguments . append ( Commands . Argument ( * args , ** kwargs ) ) <EOL> return tuple ( arguments ) <EOL> class BundleCLI ( object ) : <EOL> def __init__ ( self , manager , headless = False , stdout = sys . stdout , stderr = sys . stderr ) : <EOL> self . manager = manager <EOL> self . verbose = manager . cli_verbose <EOL> self . headless = headless <EOL> self . stdout = stdout <EOL> self . stderr = stderr <EOL> def exit ( self , message , error_code = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> precondition ( error_code , '<STR_LIT>' ) <EOL> print >> self . stderr , message <EOL> sys . exit ( error_code ) <EOL> @ staticmethod <EOL> def simple_bundle_str ( info ) : <EOL> return '<STR_LIT>' % ( contents_str ( info . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:name>' ) ) , info [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def simple_worksheet_str ( info ) : <EOL> return '<STR_LIT>' % ( contents_str ( info . get ( '<STR_LIT:name>' ) ) , info [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def simple_user_str ( info ) : <EOL> return '<STR_LIT>' % ( contents_str ( info . get ( '<STR_LIT:name>' ) ) , info [ '<STR_LIT:id>' ] ) <EOL> @ staticmethod <EOL> def get_worksheet_bundles ( worksheet_info ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for ( bundle_info , subworksheet_info , value_obj , item_type ) in worksheet_info [ '<STR_LIT>' ] : <EOL> if item_type == worksheet_util . TYPE_BUNDLE : <EOL> result . append ( bundle_info ) <EOL> return result <EOL> @ staticmethod <EOL> def parse_target ( client , worksheet_uuid , target_spec ) : <EOL> """<STR_LIT>""" <EOL> if os . sep in target_spec : <EOL> bundle_spec , subpath = tuple ( target_spec . split ( os . sep , <NUM_LIT:1> ) ) <EOL> else : <EOL> bundle_spec , subpath = target_spec , '<STR_LIT>' <EOL> bundle_uuid = worksheet_util . get_bundle_uuid ( client , worksheet_uuid , bundle_spec ) <EOL> return ( bundle_uuid , subpath ) <EOL> def parse_key_targets ( self , client , worksheet_uuid , items ) : <EOL> """<STR_LIT>""" <EOL> targets = [ ] <EOL> for item in items : <EOL> if '<STR_LIT::>' in item : <EOL> ( key , target ) = item . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if key == '<STR_LIT>' : <EOL> key = target <EOL> else : <EOL> ( key , target ) = ( '<STR_LIT>' , item ) <EOL> if key in targets : <EOL> if key : <EOL> raise UsageError ( '<STR_LIT>' % ( key , ) ) <EOL> else : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> targets . append ( ( key , self . parse_target ( client , worksheet_uuid , target ) ) ) <EOL> return targets <EOL> def print_table ( self , columns , row_dicts , post_funcs = { } , justify = { } , show_header = True , indent = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> rows = [ columns ] <EOL> for row_dict in row_dicts : <EOL> row = [ ] <EOL> for col in columns : <EOL> cell = row_dict . get ( col ) <EOL> func = post_funcs . get ( col ) <EOL> if func : <EOL> cell = worksheet_util . apply_func ( func , cell ) <EOL> if cell is None : <EOL> cell = contents_str ( cell ) <EOL> row . append ( cell ) <EOL> rows . append ( row ) <EOL> lengths = [ max ( len ( str ( value ) ) for value in col ) for col in zip ( * rows ) ] <EOL> for ( i , row ) in enumerate ( rows ) : <EOL> row_strs = [ ] <EOL> for ( j , value ) in enumerate ( row ) : <EOL> length = lengths [ j ] <EOL> padding = ( length - len ( str ( value ) ) ) * '<STR_LIT:U+0020>' <EOL> if justify . get ( columns [ j ] , - <NUM_LIT:1> ) < <NUM_LIT:0> : <EOL> row_strs . append ( str ( value ) + padding ) <EOL> else : <EOL> row_strs . append ( padding + str ( value ) ) <EOL> if show_header or i > <NUM_LIT:0> : <EOL> print >> self . stdout , indent + '<STR_LIT:U+0020>' . join ( row_strs ) <EOL> if i == <NUM_LIT:0> : <EOL> print >> self . stdout , indent + ( sum ( lengths ) + <NUM_LIT:2> * ( len ( columns ) - <NUM_LIT:1> ) ) * '<STR_LIT:->' <EOL> def parse_spec ( self , spec ) : <EOL> """<STR_LIT>""" <EOL> tokens = spec . split ( '<STR_LIT>' ) <EOL> if len ( tokens ) == <NUM_LIT:1> : <EOL> address = self . manager . session ( ) [ '<STR_LIT:address>' ] <EOL> spec = tokens [ <NUM_LIT:0> ] <EOL> else : <EOL> address = self . manager . apply_alias ( tokens [ <NUM_LIT:0> ] ) <EOL> spec = tokens [ <NUM_LIT:1> ] <EOL> return ( self . manager . client ( address ) , spec ) <EOL> def parse_client_worksheet_uuid ( self , spec ) : <EOL> """<STR_LIT>""" <EOL> if not spec or spec == worksheet_util . CURRENT_WORKSHEET : <EOL> client , worksheet_uuid = self . manager . get_current_worksheet_uuid ( ) <EOL> else : <EOL> client_is_explicit = spec_util . client_is_explicit ( spec ) <EOL> client , spec = self . parse_spec ( spec ) <EOL> if client_is_explicit : <EOL> base_worksheet_uuid = None <EOL> else : <EOL> _ , base_worksheet_uuid = self . manager . get_current_worksheet_uuid ( ) <EOL> worksheet_uuid = worksheet_util . get_worksheet_uuid ( client , base_worksheet_uuid , spec ) <EOL> return ( client , worksheet_uuid ) <EOL> @ staticmethod <EOL> def get_missing_metadata ( bundle_subclass , args , initial_metadata = None ) : <EOL> """<STR_LIT>""" <EOL> if not initial_metadata : <EOL> initial_metadata = { <EOL> spec . key : getattr ( args , metadata_util . metadata_key_to_argument ( spec . key ) ) <EOL> for spec in bundle_subclass . get_user_defined_metadata ( ) <EOL> } <EOL> if not getattr ( args , '<STR_LIT>' , True ) : <EOL> return metadata_util . fill_missing_metadata ( bundle_subclass , args , initial_metadata ) <EOL> else : <EOL> return metadata_util . request_missing_metadata ( bundle_subclass , initial_metadata ) <EOL> EDIT_ARGUMENTS = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) <EOL> WAIT_ARGUMENTS = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) <EOL> MIMIC_ARGUMENTS = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , default = <NUM_LIT:10> , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) + WAIT_ARGUMENTS <EOL> @ staticmethod <EOL> def collapse_bare_command ( argv ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> i = argv . index ( '<STR_LIT>' ) <EOL> argv = argv [ <NUM_LIT:0> : i ] + [ '<STR_LIT:U+0020>' . join ( argv [ i + <NUM_LIT:1> : ] ) ] <EOL> except : <EOL> pass <EOL> return argv <EOL> def complete_command ( self , command ) : <EOL> """<STR_LIT>""" <EOL> parser = Commands . build_parser ( self ) <EOL> cf = argcomplete . CompletionFinder ( parser ) <EOL> cword_prequote , cword_prefix , _ , comp_words , first_colon_pos = argcomplete . split_line ( command , len ( command ) ) <EOL> clean = lambda s : shlex . split ( s . strip ( ) ) [ <NUM_LIT:0> ] if s else '<STR_LIT>' <EOL> return map ( clean , cf . _get_completions ( comp_words , cword_prefix , cword_prequote , first_colon_pos ) ) <EOL> def do_command ( self , argv , stdout = None , stderr = None ) : <EOL> parser = Commands . build_parser ( self ) <EOL> argcomplete . autocomplete ( parser ) <EOL> argv = self . collapse_bare_command ( argv ) <EOL> args = parser . parse_args ( argv ) <EOL> command_fn = lambda : args . function ( self , args ) <EOL> if self . verbose >= <NUM_LIT:2> : <EOL> structured_result = command_fn ( ) <EOL> else : <EOL> try : <EOL> if False : <EOL> import hotshot <EOL> import hotshot . stats <EOL> prof_path = '<STR_LIT>' <EOL> prof = hotshot . Profile ( prof_path ) <EOL> prof . runcall ( command_fn ) <EOL> prof . close ( ) <EOL> stats = hotshot . stats . load ( prof_path ) <EOL> stats . sort_stats ( '<STR_LIT:time>' , '<STR_LIT>' ) <EOL> stats . print_stats ( <NUM_LIT:20> ) <EOL> else : <EOL> structured_result = command_fn ( ) <EOL> except PermissionError , e : <EOL> if self . headless : <EOL> raise e <EOL> self . exit ( e . message ) <EOL> except UsageError , e : <EOL> if self . headless : <EOL> raise e <EOL> self . exit ( '<STR_LIT>' % ( e . __class__ . __name__ , e ) ) <EOL> return structured_result <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:?>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_help_command ( self , args ) : <EOL> print >> self . stdout , '<STR_LIT>' % CODALAB_VERSION <EOL> if args . command : <EOL> self . do_command ( [ args . command , '<STR_LIT>' ] ) <EOL> return <EOL> print >> self . stdout , Commands . help_text ( args . verbose ) <EOL> @ Commands . command ( <EOL> '<STR_LIT:status>' , <EOL> aliases = ( '<STR_LIT>' , ) , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> def do_status_command ( self , args ) : <EOL> if not self . headless : <EOL> print >> self . stdout , "<STR_LIT>" % self . manager . codalab_home <EOL> print >> self . stdout , "<STR_LIT>" % self . manager . session_name ( ) <EOL> address = self . manager . session ( ) [ '<STR_LIT:address>' ] <EOL> print >> self . stdout , "<STR_LIT>" % address <EOL> state = self . manager . state [ '<STR_LIT>' ] . get ( address , { } ) <EOL> if '<STR_LIT:username>' in state : <EOL> print >> self . stdout , "<STR_LIT>" % state [ '<STR_LIT:username>' ] <EOL> client , worksheet_uuid = self . manager . get_current_worksheet_uuid ( ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , False ) <EOL> print >> self . stdout , "<STR_LIT>" % self . simple_worksheet_str ( worksheet_info ) <EOL> print >> self . stdout , "<STR_LIT>" % self . simple_user_str ( client . user_info ( None ) ) <EOL> user_info = client . get_user_info ( None ) <EOL> print >> self . stdout , "<STR_LIT>" % formatting . ratio_str ( formatting . duration_str , user_info [ '<STR_LIT>' ] , user_info [ '<STR_LIT>' ] ) <EOL> print >> self . stdout , "<STR_LIT>" % formatting . ratio_str ( formatting . size_str , user_info [ '<STR_LIT>' ] , user_info [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> def do_logout_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> client = self . manager . current_client ( ) <EOL> self . manager . logout ( client ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT:name>' , help = '<STR_LIT>' , nargs = '<STR_LIT:?>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:?>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> ) , <EOL> ) <EOL> def do_alias_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> aliases = self . manager . config [ '<STR_LIT>' ] <EOL> if args . name : <EOL> instance = aliases . get ( args . name ) <EOL> if args . remove : <EOL> del aliases [ args . name ] <EOL> self . manager . save_config ( ) <EOL> elif args . instance : <EOL> aliases [ args . name ] = args . instance <EOL> self . manager . save_config ( ) <EOL> else : <EOL> print >> self . stdout , args . name + '<STR_LIT>' + formatting . verbose_contents_str ( instance ) <EOL> else : <EOL> for name , instance in aliases . items ( ) : <EOL> print >> self . stdout , name + '<STR_LIT>' + instance <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT>' , ) , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT:path>' , help = '<STR_LIT>' , nargs = '<STR_LIT:*>' , completer = require_not_headless ( FilesCompleter ( ) ) ) , <EOL> Commands . Argument ( '<STR_LIT:-c>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:*>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) + Commands . metadata_arguments ( [ UploadedBundle ] + [ get_bundle_subclass ( bundle_type ) for bundle_type in UPLOADED_TYPES ] ) <EOL> + EDIT_ARGUMENTS , <EOL> ) <EOL> def do_upload_command ( self , args ) : <EOL> if self . headless and not args . path and args . contents is None : <EOL> return ui_actions . serialize ( [ ui_actions . Upload ( ) ] ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> for path in args . path : <EOL> if path_util . path_is_url ( path ) : <EOL> pass <EOL> elif self . headless : <EOL> raise UsageError ( "<STR_LIT>" % path ) <EOL> else : <EOL> path_util . check_isvalid ( path_util . normalize ( path ) , '<STR_LIT>' ) <EOL> if args . contents is not None : <EOL> if not args . md_name : <EOL> args . md_name = '<STR_LIT>' <EOL> tmp_path = tempfile . mkstemp ( ) [ <NUM_LIT:1> ] <EOL> f = open ( tmp_path , '<STR_LIT:w>' ) <EOL> print >> f , args . contents <EOL> f . close ( ) <EOL> args . path . append ( tmp_path ) <EOL> sources = [ path_util . normalize ( path ) for path in args . path ] <EOL> bundle_type = '<STR_LIT>' <EOL> bundle_subclass = get_bundle_subclass ( bundle_type ) <EOL> metadata = self . get_missing_metadata ( bundle_subclass , args , initial_metadata = { } ) <EOL> if not args . pack and zip_util . path_is_archive ( metadata [ '<STR_LIT:name>' ] ) : <EOL> metadata [ '<STR_LIT:name>' ] = zip_util . strip_archive_ext ( metadata [ '<STR_LIT:name>' ] ) <EOL> bundle_subclass . construct ( owner_id = <NUM_LIT:0> , metadata = metadata ) . validate ( ) <EOL> info = { '<STR_LIT>' : bundle_type , '<STR_LIT>' : metadata } <EOL> uuid = client . upload_bundle ( <EOL> sources = sources , <EOL> follow_symlinks = args . follow_symlinks , <EOL> exclude_patterns = args . exclude_patterns , <EOL> git = args . git , <EOL> unpack = not args . pack , <EOL> remove_sources = ( args . contents is not None ) , <EOL> info = info , <EOL> worksheet_uuid = worksheet_uuid , <EOL> add_to_worksheet = True , <EOL> ) <EOL> print >> self . stdout , uuid <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT>' , ) , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = TARGET_SPEC_FORMAT , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_download_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> target = self . parse_target ( client , worksheet_uuid , args . target_spec ) <EOL> bundle_uuid , subpath = target <EOL> info = client . get_bundle_info ( bundle_uuid ) <EOL> if args . output_path : <EOL> local_path = args . output_path <EOL> else : <EOL> local_path = info [ '<STR_LIT>' ] . get ( '<STR_LIT:name>' , '<STR_LIT>' ) if subpath == '<STR_LIT>' else os . path . basename ( subpath ) <EOL> final_path = os . path . join ( os . getcwd ( ) , local_path ) <EOL> if os . path . exists ( final_path ) : <EOL> print >> self . stdout , '<STR_LIT>' % local_path <EOL> return <EOL> target_info = client . get_target_info ( target , <NUM_LIT:0> ) <EOL> if target_info is None : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> if target_info [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> client . download_directory ( target , final_path ) <EOL> elif target_info [ '<STR_LIT:type>' ] == '<STR_LIT:file>' : <EOL> client . download_file ( target , final_path ) <EOL> elif target_info [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> print >> self . stdout , '<STR_LIT>' % ( self . simple_bundle_str ( info ) , subpath , final_path ) <EOL> def copy_bundle ( self , source_client , source_bundle_uuid , dest_client , dest_worksheet_uuid , copy_dependencies , add_to_worksheet ) : <EOL> """<STR_LIT>""" <EOL> if copy_dependencies : <EOL> source_info = source_client . get_bundle_info ( source_bundle_uuid ) <EOL> for dep in source_info [ '<STR_LIT>' ] : <EOL> self . copy_bundle ( source_client , dep [ '<STR_LIT>' ] , dest_client , dest_worksheet_uuid , False , add_to_worksheet ) <EOL> self . copy_bundle ( source_client , source_bundle_uuid , dest_client , dest_worksheet_uuid , False , add_to_worksheet ) <EOL> return <EOL> bundle = None <EOL> try : <EOL> bundle = dest_client . get_bundle_info ( source_bundle_uuid ) <EOL> except : <EOL> pass <EOL> if bundle : <EOL> if add_to_worksheet : <EOL> dest_client . add_worksheet_item ( dest_worksheet_uuid , worksheet_util . bundle_item ( source_bundle_uuid ) ) <EOL> return <EOL> source_info = source_client . get_bundle_info ( source_bundle_uuid ) <EOL> if source_info is None : <EOL> print >> self . stdout , '<STR_LIT>' % source_bundle_uuid <EOL> return <EOL> source_desc = self . simple_bundle_str ( source_info ) <EOL> if source_info [ '<STR_LIT:state>' ] not in [ State . READY , State . FAILED ] : <EOL> print >> self . stdout , '<STR_LIT>' % ( source_desc , source_info [ '<STR_LIT:state>' ] ) <EOL> return <EOL> print >> self . stdout , "<STR_LIT>" % source_desc <EOL> target = ( source_bundle_uuid , '<STR_LIT>' ) <EOL> target_info = source_client . get_target_info ( target , <NUM_LIT:0> ) <EOL> source = None <EOL> dest_file_uuid = None <EOL> try : <EOL> if target_info is not None and target_info [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> filename_suffix = '<STR_LIT>' <EOL> if isinstance ( source_client , LocalBundleClient ) : <EOL> source = source_client . open_tarred_gzipped_directory ( target ) <EOL> else : <EOL> source = RPCFileHandle ( <EOL> source_client . open_tarred_gzipped_directory ( target ) , <EOL> source_client . proxy , finalize_on_close = True ) <EOL> elif target_info is not None and target_info [ '<STR_LIT:type>' ] == '<STR_LIT:file>' : <EOL> filename_suffix = '<STR_LIT>' <EOL> if isinstance ( source_client , LocalBundleClient ) : <EOL> source = source_client . open_gzipped_file ( target ) <EOL> else : <EOL> source = RPCFileHandle ( <EOL> source_client . open_gzipped_file ( target ) , <EOL> source_client . proxy , finalize_on_close = True ) <EOL> if source is not None : <EOL> if isinstance ( dest_client , LocalBundleClient ) : <EOL> dest_path = tempfile . mkstemp ( filename_suffix ) [ <NUM_LIT:1> ] <EOL> dest = open ( dest_path , '<STR_LIT:wb>' ) <EOL> else : <EOL> dest_file_uuid = dest_client . open_temp_file ( '<STR_LIT>' + filename_suffix ) <EOL> dest = RPCFileHandle ( dest_file_uuid , dest_client . proxy ) <EOL> with closing ( dest ) : <EOL> file_util . copy ( <EOL> source , <EOL> dest , <EOL> autoflush = False , <EOL> print_status = '<STR_LIT>' % ( source_bundle_uuid , source_client . address , dest_client . address ) ) <EOL> if source is None : <EOL> sources = [ None ] <EOL> elif isinstance ( dest_client , LocalBundleClient ) : <EOL> sources = [ dest_path ] <EOL> else : <EOL> sources = [ dest_file_uuid ] <EOL> if isinstance ( dest_client , LocalBundleClient ) : <EOL> result = dest_client . upload_bundle ( <EOL> sources = sources , <EOL> follow_symlinks = False , <EOL> exclude_patterns = None , <EOL> git = False , <EOL> unpack = True , <EOL> remove_sources = True , <EOL> info = source_info , <EOL> worksheet_uuid = dest_worksheet_uuid , <EOL> add_to_worksheet = add_to_worksheet , <EOL> ) <EOL> else : <EOL> result = dest_client . finish_upload_bundle ( <EOL> sources , <EOL> True , <EOL> source_info , <EOL> dest_worksheet_uuid , <EOL> add_to_worksheet ) <EOL> return result <EOL> except : <EOL> if source is not None : <EOL> source . close ( ) <EOL> if dest_file_uuid is not None : <EOL> dest_client . finalize_file ( dest_file_uuid ) <EOL> raise <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = ALIASED_TARGET_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) + Commands . metadata_arguments ( [ MakeBundle ] ) + EDIT_ARGUMENTS , <EOL> ) <EOL> def do_make_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> targets = self . parse_key_targets ( client , worksheet_uuid , args . target_spec ) <EOL> metadata = self . get_missing_metadata ( MakeBundle , args ) <EOL> print >> self . stdout , client . derive_bundle ( '<STR_LIT>' , targets , None , metadata , worksheet_uuid ) <EOL> def wait ( self , client , args , uuid ) : <EOL> info_args = argparse . Namespace ( ) <EOL> info_args . worksheet_spec = args . worksheet_spec <EOL> info_args . verbose = args . verbose <EOL> info_args . bundle_spec = [ uuid ] <EOL> info_args . field = None <EOL> info_args . raw = False <EOL> if args . wait : <EOL> self . follow_targets ( client , uuid , [ ] ) <EOL> self . do_info_command ( info_args ) <EOL> if args . tail : <EOL> self . follow_targets ( client , uuid , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if args . verbose : <EOL> self . do_info_command ( info_args ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = ALIASED_TARGET_SPEC_FORMAT , nargs = '<STR_LIT:*>' , completer = TargetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , help = '<STR_LIT>' , completer = NullCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) + Commands . metadata_arguments ( [ RunBundle ] ) + EDIT_ARGUMENTS + WAIT_ARGUMENTS , <EOL> ) <EOL> def do_run_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> args . target_spec , args . command = cli_util . desugar_command ( args . target_spec , args . command ) <EOL> targets = self . parse_key_targets ( client , worksheet_uuid , args . target_spec ) <EOL> metadata = self . get_missing_metadata ( RunBundle , args ) <EOL> uuid = client . derive_bundle ( '<STR_LIT>' , targets , args . command , metadata , worksheet_uuid ) <EOL> print >> self . stdout , uuid <EOL> self . wait ( client , args , uuid ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT:e>' , ) , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % spec_util . NAME_REGEX . pattern ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_edit_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuid = worksheet_util . get_bundle_uuid ( client , worksheet_uuid , args . bundle_spec ) <EOL> info = client . get_bundle_info ( bundle_uuid ) <EOL> bundle_subclass = get_bundle_subclass ( info [ '<STR_LIT>' ] ) <EOL> metadata = info [ '<STR_LIT>' ] <EOL> new_metadata = copy . deepcopy ( metadata ) <EOL> is_new_metadata_updated = False <EOL> if args . name : <EOL> new_metadata [ '<STR_LIT:name>' ] = args . name <EOL> is_new_metadata_updated = True <EOL> if args . description : <EOL> new_metadata [ '<STR_LIT:description>' ] = args . description <EOL> is_new_metadata_updated = True <EOL> if not is_new_metadata_updated and not self . headless and metadata == new_metadata : <EOL> new_metadata = self . get_missing_metadata ( bundle_subclass , args , new_metadata ) <EOL> if metadata != new_metadata : <EOL> client . update_bundle_metadata ( bundle_uuid , new_metadata ) <EOL> print >> self . stdout , "<STR_LIT>" % ( bundle_uuid ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT>' , ) , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , type = int ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_detach_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> args . bundle_spec = spec_util . expand_specs ( args . bundle_spec ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundle_spec ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , True ) <EOL> items = worksheet_info [ '<STR_LIT>' ] <EOL> indices = [ None ] * len ( items ) <EOL> uuid2index = { } <EOL> for i , item in reversed ( list ( enumerate ( items ) ) ) : <EOL> ( bundle_info , subworksheet_info , value_obj , item_type ) = item <EOL> if item_type == worksheet_util . TYPE_BUNDLE : <EOL> uuid = bundle_info [ '<STR_LIT>' ] <EOL> indices [ i ] = uuid2index [ uuid ] = uuid2index . get ( uuid , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> new_items = [ ] <EOL> for i , item in enumerate ( items ) : <EOL> ( bundle_info , subworksheet_info , value_obj , item_type ) = item <EOL> detach = False <EOL> if item_type == worksheet_util . TYPE_BUNDLE : <EOL> uuid = bundle_info [ '<STR_LIT>' ] <EOL> if uuid in bundle_uuids : <EOL> if args . index == None : <EOL> if uuid2index [ uuid ] != <NUM_LIT:1> : <EOL> raise UsageError ( '<STR_LIT>' % uuid ) <EOL> detach = True <EOL> else : <EOL> if args . index > uuid2index [ uuid ] : <EOL> raise UsageError ( '<STR_LIT>' % ( uuid , uuid2index [ uuid ] , args . index ) ) <EOL> if args . index == indices [ i ] : <EOL> detach = True <EOL> if not detach : <EOL> new_items . append ( item ) <EOL> client . update_worksheet_items ( worksheet_info , new_items ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_rm_command ( self , args ) : <EOL> args . bundle_spec = spec_util . expand_specs ( args . bundle_spec ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundle_spec ) <EOL> deleted_uuids = client . delete_bundles ( bundle_uuids , args . force , args . recursive , args . data_only , args . dry_run ) <EOL> if args . dry_run : <EOL> print >> self . stdout , '<STR_LIT>' <EOL> bundle_infos = client . get_bundle_infos ( deleted_uuids ) <EOL> bundle_info_list = [ bundle_infos [ uuid ] for uuid in deleted_uuids if uuid in bundle_infos ] <EOL> self . print_bundle_info_list ( bundle_info_list , uuid_only = False , print_ref = False ) <EOL> else : <EOL> for uuid in deleted_uuids : <EOL> print >> self . stdout , uuid <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT:s>' , ) , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:+>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_search_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = client . search_bundle_uuids ( worksheet_uuid , args . keywords ) <EOL> if not isinstance ( bundle_uuids , list ) : <EOL> print >> self . stdout , bundle_uuids <EOL> return <EOL> bundle_infos = client . get_bundle_infos ( bundle_uuids ) <EOL> bundle_info_list = [ bundle_infos [ uuid ] for uuid in bundle_uuids if uuid in bundle_infos ] <EOL> reference_map = self . create_reference_map ( '<STR_LIT>' , bundle_info_list ) <EOL> if args . uuid_only : <EOL> bundle_info_list = [ { '<STR_LIT>' : uuid } for uuid in bundle_uuids ] <EOL> if len ( bundle_info_list ) > <NUM_LIT:0> : <EOL> self . print_bundle_info_list ( bundle_info_list , uuid_only = args . uuid_only , print_ref = False ) <EOL> if args . append : <EOL> for bundle_uuid in bundle_uuids : <EOL> client . add_worksheet_item ( worksheet_uuid , worksheet_util . bundle_item ( bundle_uuid ) ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , False ) <EOL> print >> self . stdout , '<STR_LIT>' % ( len ( bundle_uuids ) , self . worksheet_str ( worksheet_info ) ) <EOL> return { <EOL> '<STR_LIT>' : reference_map <EOL> } <EOL> def create_structured_info_map ( self , structured_info_list ) : <EOL> """<STR_LIT>""" <EOL> return dict ( structured_info_list ) <EOL> def create_reference_map ( self , info_type , info_list ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> worksheet_util . apply_func ( UUID_POST_FUNC , info [ '<STR_LIT>' ] ) : { <EOL> '<STR_LIT:type>' : info_type , <EOL> '<STR_LIT>' : info [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : info . get ( '<STR_LIT>' , info ) . get ( '<STR_LIT:name>' , None ) <EOL> } for info in info_list <EOL> } <EOL> @ Commands . command ( <EOL> name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_ls_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , True , True ) <EOL> bundle_info_list = self . get_worksheet_bundles ( worksheet_info ) <EOL> if not args . uuid_only : <EOL> print >> self . stdout , self . _worksheet_description ( worksheet_info ) <EOL> if len ( bundle_info_list ) > <NUM_LIT:0> : <EOL> self . print_bundle_info_list ( bundle_info_list , args . uuid_only , print_ref = True ) <EOL> reference_map = self . create_reference_map ( '<STR_LIT>' , bundle_info_list ) <EOL> return self . create_structured_info_map ( [ ( '<STR_LIT>' , reference_map ) ] ) <EOL> def _worksheet_description ( self , worksheet_info ) : <EOL> fields = [ <EOL> ( '<STR_LIT>' , self . worksheet_str ( worksheet_info ) ) , <EOL> ( '<STR_LIT>' , formatting . verbose_contents_str ( worksheet_info [ '<STR_LIT:title>' ] ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( worksheet_info [ '<STR_LIT>' ] ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' % ( worksheet_info [ '<STR_LIT>' ] , worksheet_info [ '<STR_LIT>' ] ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' % ( group_permissions_str ( worksheet_info [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' if worksheet_info [ '<STR_LIT>' ] else '<STR_LIT>' ) ) <EOL> ] <EOL> return '<STR_LIT:\n>' . join ( '<STR_LIT>' % ( k , v ) for k , v in fields ) <EOL> def print_bundle_info_list ( self , bundle_info_list , uuid_only , print_ref ) : <EOL> """<STR_LIT>""" <EOL> if uuid_only : <EOL> for bundle_info in bundle_info_list : <EOL> print >> self . stdout , bundle_info [ '<STR_LIT>' ] <EOL> else : <EOL> def get ( i , info , col ) : <EOL> if col == '<STR_LIT>' : <EOL> return '<STR_LIT>' + str ( len ( bundle_info_list ) - i ) <EOL> else : <EOL> return info . get ( col , info . get ( '<STR_LIT>' , { } ) . get ( col ) ) <EOL> columns = ( ( '<STR_LIT>' , ) if print_ref else ( ) ) + ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' ) <EOL> post_funcs = { '<STR_LIT>' : UUID_POST_FUNC , '<STR_LIT>' : '<STR_LIT:date>' , '<STR_LIT>' : '<STR_LIT:size>' } <EOL> justify = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> bundle_dicts = [ <EOL> { col : get ( i , info , col ) for col in columns } <EOL> for i , info in enumerate ( bundle_info_list ) <EOL> ] <EOL> self . print_table ( columns , bundle_dicts , post_funcs = post_funcs , justify = justify ) <EOL> @ Commands . command ( <EOL> '<STR_LIT:info>' , <EOL> aliases = ( '<STR_LIT:i>' , ) , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_info_command ( self , args ) : <EOL> args . bundle_spec = spec_util . expand_specs ( args . bundle_spec ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundle_spec ) <EOL> for i , bundle_uuid in enumerate ( bundle_uuids ) : <EOL> info = client . get_bundle_info ( bundle_uuid , args . verbose , args . verbose , args . verbose ) <EOL> if info is None : <EOL> raise UsageError ( '<STR_LIT>' % bundle_uuid ) <EOL> if args . field : <EOL> values = [ ] <EOL> for genpath in args . field . split ( '<STR_LIT:U+002C>' ) : <EOL> if worksheet_util . is_file_genpath ( genpath ) : <EOL> value = contents_str ( worksheet_util . interpret_file_genpath ( client , { } , bundle_uuid , genpath , None ) ) <EOL> else : <EOL> value = worksheet_util . interpret_genpath ( info , genpath ) <EOL> values . append ( value ) <EOL> print >> self . stdout , '<STR_LIT:\t>' . join ( map ( str , values ) ) <EOL> else : <EOL> if i > <NUM_LIT:0> : <EOL> print <EOL> self . print_basic_info ( client , info , args . raw ) <EOL> if args . verbose : <EOL> self . print_children ( info ) <EOL> self . print_host_worksheets ( info ) <EOL> self . print_permissions ( info ) <EOL> self . print_contents ( client , info ) <EOL> if self . headless and not ( args . field or args . raw or args . verbose ) : <EOL> return ui_actions . serialize ( [ ui_actions . OpenBundle ( uuid ) for uuid in bundle_uuids ] ) <EOL> @ staticmethod <EOL> def key_value_str ( key , value ) : <EOL> return '<STR_LIT>' % ( key , formatting . verbose_contents_str ( unicode ( value ) if value is not None else None ) ) <EOL> def print_basic_info ( self , client , info , raw ) : <EOL> """<STR_LIT>""" <EOL> metadata = info [ '<STR_LIT>' ] <EOL> lines = [ ] <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:state>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not raw : <EOL> if key not in info : continue <EOL> lines . append ( self . key_value_str ( key , info . get ( key ) ) ) <EOL> cls = get_bundle_subclass ( info [ '<STR_LIT>' ] ) <EOL> for key , value in worksheet_util . get_formatted_metadata ( cls , metadata , raw ) : <EOL> lines . append ( self . key_value_str ( key , value ) ) <EOL> standard_keys = set ( spec . key for spec in cls . METADATA_SPECS ) <EOL> for key , value in metadata . items ( ) : <EOL> if key in standard_keys : continue <EOL> lines . append ( self . key_value_str ( key , value ) ) <EOL> def display_dependencies ( label , deps ) : <EOL> lines . append ( label + '<STR_LIT::>' ) <EOL> for dep in deps : <EOL> child = dep [ '<STR_LIT>' ] <EOL> parent = path_util . safe_join ( contents_str ( dep [ '<STR_LIT>' ] ) + '<STR_LIT:(>' + dep [ '<STR_LIT>' ] + '<STR_LIT:)>' , dep [ '<STR_LIT>' ] ) <EOL> lines . append ( '<STR_LIT>' % ( child , parent ) ) <EOL> if info [ '<STR_LIT>' ] : <EOL> deps = info [ '<STR_LIT>' ] <EOL> display_dependencies ( '<STR_LIT>' , deps ) <EOL> print >> self . stdout , '<STR_LIT:\n>' . join ( lines ) <EOL> def print_children ( self , info ) : <EOL> print >> self . stdout , '<STR_LIT>' <EOL> for child in info [ '<STR_LIT>' ] : <EOL> print >> self . stdout , "<STR_LIT>" % self . simple_bundle_str ( child ) <EOL> def print_host_worksheets ( self , info ) : <EOL> print >> self . stdout , '<STR_LIT>' <EOL> for host_worksheet_info in info [ '<STR_LIT>' ] : <EOL> print >> self . stdout , "<STR_LIT>" % self . simple_worksheet_str ( host_worksheet_info ) <EOL> def print_permissions ( self , info ) : <EOL> print >> self . stdout , '<STR_LIT>' % permission_str ( info [ '<STR_LIT>' ] ) <EOL> print >> self . stdout , '<STR_LIT>' <EOL> print >> self . stdout , '<STR_LIT>' % group_permissions_str ( info [ '<STR_LIT>' ] ) <EOL> def print_contents ( self , client , info ) : <EOL> def wrap ( string ) : <EOL> return '<STR_LIT>' + string + '<STR_LIT>' <EOL> print >> self . stdout , wrap ( '<STR_LIT>' ) <EOL> bundle_uuid = info [ '<STR_LIT>' ] <EOL> info = self . print_target_info ( client , ( bundle_uuid , '<STR_LIT>' ) , decorate = True ) <EOL> if info is not None and info [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> for item in info [ '<STR_LIT>' ] : <EOL> if item [ '<STR_LIT:name>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> print >> self . stdout , wrap ( item [ '<STR_LIT:name>' ] ) <EOL> self . print_target_info ( client , ( bundle_uuid , item [ '<STR_LIT:name>' ] ) , decorate = True ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = TARGET_SPEC_FORMAT , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_cat_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> target = self . parse_target ( client , worksheet_uuid , args . target_spec ) <EOL> self . print_target_info ( client , target , decorate = False , fail_if_not_exist = True ) <EOL> def print_target_info ( self , client , target , decorate , maxlines = <NUM_LIT:10> , fail_if_not_exist = False ) : <EOL> info = client . get_target_info ( target , <NUM_LIT:1> ) <EOL> info_type = info . get ( '<STR_LIT:type>' ) if info is not None else None <EOL> if info_type is None : <EOL> if fail_if_not_exist : <EOL> raise UsageError ( '<STR_LIT>' % target ) <EOL> else : <EOL> print >> self . stdout , formatting . verbose_contents_str ( None ) <EOL> if info_type == '<STR_LIT:file>' : <EOL> if decorate : <EOL> import base64 <EOL> for line in client . head_target ( target , maxlines ) : <EOL> print >> self . stdout , formatting . verbose_contents_str ( base64 . b64decode ( line ) ) , <EOL> else : <EOL> client . cat_target ( target , self . stdout ) <EOL> def size ( x ) : <EOL> t = x . get ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> if t == '<STR_LIT:file>' : <EOL> return formatting . size_str ( x [ '<STR_LIT:size>' ] ) <EOL> elif t == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return t <EOL> if info_type == '<STR_LIT>' : <EOL> contents = [ <EOL> { <EOL> '<STR_LIT:name>' : x [ '<STR_LIT:name>' ] + ( '<STR_LIT>' + x [ '<STR_LIT>' ] if '<STR_LIT>' in x else '<STR_LIT>' ) , <EOL> '<STR_LIT:size>' : size ( x ) , <EOL> '<STR_LIT>' : oct ( x [ '<STR_LIT>' ] ) if '<STR_LIT>' in x else '<STR_LIT>' <EOL> } <EOL> for x in info [ '<STR_LIT>' ] <EOL> ] <EOL> contents = sorted ( contents , key = lambda r : r [ '<STR_LIT:name>' ] ) <EOL> self . print_table ( ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:size>' ) , contents , justify = { '<STR_LIT:size>' : <NUM_LIT:1> } , indent = '<STR_LIT>' ) <EOL> if info_type == '<STR_LIT>' : <EOL> print >> self . stdout , '<STR_LIT>' + info [ '<STR_LIT>' ] <EOL> return info <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = TARGET_SPEC_FORMAT , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_wait_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> target = self . parse_target ( client , worksheet_uuid , args . target_spec ) <EOL> ( bundle_uuid , subpath ) = target <EOL> subpaths = [ ] <EOL> if args . tail : <EOL> if subpath == '<STR_LIT>' : <EOL> subpaths = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> subpaths = [ subpath ] <EOL> state = self . follow_targets ( client , bundle_uuid , subpaths ) <EOL> if state != State . READY : <EOL> self . exit ( state ) <EOL> print >> self . stdout , bundle_uuid <EOL> def follow_targets ( self , client , bundle_uuid , subpaths ) : <EOL> """<STR_LIT>""" <EOL> subpath_is_file = [ None ] * len ( subpaths ) <EOL> subpath_offset = [ None ] * len ( subpaths ) <EOL> SLEEP_PERIOD = <NUM_LIT:1.0> <EOL> while True : <EOL> info = client . get_bundle_info ( bundle_uuid ) <EOL> if info [ '<STR_LIT:state>' ] in ( State . RUNNING , State . READY , State . FAILED ) : <EOL> break <EOL> time . sleep ( SLEEP_PERIOD ) <EOL> info = None <EOL> run_finished = False <EOL> while True : <EOL> if not run_finished : <EOL> info = client . get_bundle_info ( bundle_uuid ) <EOL> run_finished = info [ '<STR_LIT:state>' ] in ( State . READY , State . FAILED ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( subpaths ) ) : <EOL> if subpath_is_file [ i ] is None : <EOL> target_info = client . get_target_info ( ( bundle_uuid , subpaths [ i ] ) , <NUM_LIT:0> ) <EOL> if target_info is not None : <EOL> if target_info [ '<STR_LIT:type>' ] == '<STR_LIT:file>' : <EOL> subpath_is_file [ i ] = True <EOL> subpath_offset [ i ] = max ( target_info [ '<STR_LIT:size>' ] - <NUM_LIT:64> , <NUM_LIT:0> ) <EOL> else : <EOL> subpath_is_file [ i ] = False <EOL> if not subpath_is_file [ i ] : <EOL> continue <EOL> while True : <EOL> READ_LENGTH = <NUM_LIT> <EOL> result = client . read_file_section ( ( bundle_uuid , subpaths [ i ] ) , subpath_offset [ i ] , READ_LENGTH ) <EOL> if not result : <EOL> break <EOL> subpath_offset [ i ] += len ( result ) <EOL> self . stdout . write ( result ) <EOL> if len ( result ) < READ_LENGTH : <EOL> break <EOL> self . stdout . flush ( ) <EOL> if run_finished : <EOL> break <EOL> time . sleep ( SLEEP_PERIOD ) <EOL> return info [ '<STR_LIT:state>' ] <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> ) + MIMIC_ARGUMENTS , <EOL> ) <EOL> def do_mimic_command ( self , args ) : <EOL> self . mimic ( args ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> ) + MIMIC_ARGUMENTS , <EOL> ) <EOL> def do_macro_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> if not args . name : args . name = '<STR_LIT>' <EOL> args . bundles = [ args . macro_name + '<STR_LIT>' + str ( i + <NUM_LIT:1> ) for i in range ( len ( args . bundles ) ) ] + [ args . macro_name + '<STR_LIT>' ] + args . bundles <EOL> self . mimic ( args ) <EOL> def add_mimic_args ( self , parser ) : <EOL> self . add_wait_args ( parser ) <EOL> def mimic ( self , args ) : <EOL> """<STR_LIT>""" <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundles ) <EOL> n = len ( bundle_uuids ) / <NUM_LIT:2> <EOL> if len ( bundle_uuids ) % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> old_inputs = bundle_uuids [ <NUM_LIT:0> : n ] <EOL> old_output = None <EOL> new_inputs = bundle_uuids [ n : ] <EOL> else : <EOL> old_inputs = bundle_uuids [ <NUM_LIT:0> : n ] <EOL> old_output = bundle_uuids [ n ] <EOL> new_inputs = bundle_uuids [ n + <NUM_LIT:1> : ] <EOL> plan = client . mimic ( <EOL> old_inputs , old_output , new_inputs , args . name , <EOL> worksheet_uuid , args . depth , args . shadow , args . dry_run ) <EOL> for ( old , new ) in plan : <EOL> print >> self . stderr , '<STR_LIT>' % ( self . simple_bundle_str ( old ) , self . simple_bundle_str ( new ) ) <EOL> if len ( plan ) > <NUM_LIT:0> : <EOL> new_uuid = plan [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> self . wait ( client , args , new_uuid ) <EOL> print >> self . stdout , new_uuid <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_kill_command ( self , args ) : <EOL> args . bundle_spec = spec_util . expand_specs ( args . bundle_spec ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundle_spec ) <EOL> for bundle_uuid in bundle_uuids : <EOL> print >> self . stdout , bundle_uuid <EOL> client . kill_bundles ( bundle_uuids ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = TARGET_SPEC_FORMAT , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT:string>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_write_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> target = self . parse_target ( client , worksheet_uuid , args . target_spec ) <EOL> client . write_targets ( [ target ] , args . string ) <EOL> print >> self . stdout , target [ <NUM_LIT:0> ] <EOL> def worksheet_str ( self , worksheet_info ) : <EOL> return '<STR_LIT>' % ( self . manager . session ( ) [ '<STR_LIT:address>' ] , worksheet_info [ '<STR_LIT:name>' ] , worksheet_info [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT:name>' , help = '<STR_LIT>' % spec_util . NAME_REGEX . pattern ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_new_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> uuid = client . new_worksheet ( args . name ) <EOL> print >> self . stdout , uuid <EOL> if self . headless : <EOL> return ui_actions . serialize ( [ <EOL> ui_actions . OpenWorksheet ( uuid ) <EOL> ] ) <EOL> ITEM_DESCRIPTION = textwrap . dedent ( """<STR_LIT>""" ) . format ( GLOBAL_BUNDLE_SPEC_FORMAT , WORKSHEET_SPEC_FORMAT ) . strip ( ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , choices = ( '<STR_LIT:text>' , '<STR_LIT>' , '<STR_LIT>' ) , metavar = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = ITEM_DESCRIPTION , nargs = '<STR_LIT:+>' , completer = UnionCompleter ( WorksheetsCompleter , BundlesCompleter ) ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> ) , <EOL> ) <EOL> def do_add_command ( self , args ) : <EOL> curr_client , curr_worksheet_uuid = self . manager . get_current_worksheet_uuid ( ) <EOL> dest_client , dest_worksheet_uuid = self . parse_client_worksheet_uuid ( args . dest_worksheet ) <EOL> if args . item_type != '<STR_LIT>' and args . copy_dependencies : <EOL> raise UsageError ( "<STR_LIT>" ) <EOL> if args . item_type == '<STR_LIT:text>' : <EOL> for item_spec in args . item_spec : <EOL> if item_spec . startswith ( '<STR_LIT:%>' ) : <EOL> dest_client . add_worksheet_item ( dest_worksheet_uuid , worksheet_util . directive_item ( item_spec [ <NUM_LIT:1> : ] . strip ( ) ) ) <EOL> else : <EOL> dest_client . add_worksheet_item ( dest_worksheet_uuid , worksheet_util . markup_item ( item_spec ) ) <EOL> elif args . item_type == '<STR_LIT>' : <EOL> for bundle_spec in args . item_spec : <EOL> source_client , source_spec = self . parse_spec ( bundle_spec ) <EOL> base_worksheet_uuid = curr_worksheet_uuid if source_client is curr_client else None <EOL> source_bundle_uuid = worksheet_util . get_bundle_uuid ( source_client , base_worksheet_uuid , source_spec ) <EOL> self . copy_bundle ( source_client , source_bundle_uuid , dest_client , dest_worksheet_uuid , copy_dependencies = args . copy_dependencies , add_to_worksheet = True ) <EOL> elif args . item_type == '<STR_LIT>' : <EOL> for worksheet_spec in args . item_spec : <EOL> source_client , worksheet_spec = self . parse_spec ( worksheet_spec ) <EOL> if source_client is not dest_client : <EOL> raise UsageError ( "<STR_LIT>" ) <EOL> base_worksheet_uuid = curr_worksheet_uuid if source_client is curr_client else None <EOL> subworksheet_uuid = worksheet_util . get_worksheet_uuid ( source_client , base_worksheet_uuid , worksheet_spec ) <EOL> dest_client . add_worksheet_item ( dest_worksheet_uuid , worksheet_util . subworksheet_item ( subworksheet_uuid ) ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT:w>' , ) , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , nargs = '<STR_LIT:?>' , completer = UnionCompleter ( AddressesCompleter , WorksheetsCompleter ) ) , <EOL> ) , <EOL> ) <EOL> def do_work_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , False ) <EOL> if args . worksheet_spec : <EOL> if args . uuid_only : <EOL> print >> self . stdout , worksheet_info [ '<STR_LIT>' ] <EOL> return self . change_current_worksheet ( client , worksheet_uuid , verbose = ( not args . uuid_only ) ) <EOL> else : <EOL> if worksheet_info : <EOL> if args . uuid_only : <EOL> print >> self . stdout , worksheet_info [ '<STR_LIT>' ] <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' % ( self . worksheet_str ( worksheet_info ) ) <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' <EOL> def change_current_worksheet ( self , client , worksheet_uuid , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> if worksheet_uuid is None : <EOL> worksheet_uuid = client . get_worksheet_uuid ( None , '<STR_LIT>' ) <EOL> if self . headless : <EOL> return ui_actions . serialize ( [ ui_actions . OpenWorksheet ( worksheet_uuid ) ] ) <EOL> self . manager . set_current_worksheet_uuid ( client , worksheet_uuid ) <EOL> if verbose : <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , False ) <EOL> print >> self . stdout , '<STR_LIT>' % ( self . worksheet_str ( worksheet_info ) ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT>' , ) , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , nargs = '<STR_LIT:?>' , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % spec_util . NAME_REGEX . pattern ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:*>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , completer = require_not_headless ( FilesCompleter ( directories = False ) ) ) , <EOL> ) , <EOL> ) <EOL> def do_wedit_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , True ) <EOL> if args . name != None or args . title != None or args . tags != None or args . owner_spec != None or args . freeze : <EOL> info = { } <EOL> if args . name != None : <EOL> info [ '<STR_LIT:name>' ] = args . name <EOL> if args . title != None : <EOL> info [ '<STR_LIT:title>' ] = args . title <EOL> if args . tags != None : <EOL> info [ '<STR_LIT>' ] = args . tags <EOL> if args . owner_spec != None : <EOL> info [ '<STR_LIT>' ] = args . owner_spec <EOL> if args . freeze : <EOL> info [ '<STR_LIT>' ] = True <EOL> client . update_worksheet_metadata ( worksheet_uuid , info ) <EOL> print >> self . stdout , '<STR_LIT>' % ( worksheet_info [ '<STR_LIT:name>' ] , worksheet_info [ '<STR_LIT>' ] ) <EOL> else : <EOL> if self . headless : <EOL> return ui_actions . serialize ( [ ui_actions . SetEditMode ( True ) ] ) <EOL> if args . file : <EOL> lines = [ line . rstrip ( ) for line in open ( args . file ) . readlines ( ) ] <EOL> else : <EOL> lines = worksheet_util . request_lines ( worksheet_info , client ) <EOL> new_items , commands = worksheet_util . parse_worksheet_form ( lines , client , worksheet_info [ '<STR_LIT>' ] ) <EOL> client . update_worksheet_items ( worksheet_info , new_items ) <EOL> print >> self . stdout , '<STR_LIT>' % ( worksheet_info [ '<STR_LIT:name>' ] , worksheet_info [ '<STR_LIT>' ] ) <EOL> rm_bundle_uuids = [ ] <EOL> rest_commands = [ ] <EOL> for command in commands : <EOL> if command [ <NUM_LIT:0> ] == '<STR_LIT>' and len ( command ) == <NUM_LIT:2> : <EOL> rm_bundle_uuids . append ( command [ <NUM_LIT:1> ] ) <EOL> else : <EOL> rest_commands . append ( command ) <EOL> commands = rest_commands <EOL> if len ( rm_bundle_uuids ) > <NUM_LIT:0> : <EOL> commands . append ( [ '<STR_LIT>' ] + rm_bundle_uuids ) <EOL> for command in commands : <EOL> spec = client . address + '<STR_LIT>' + worksheet_uuid <EOL> if command [ <NUM_LIT:0> ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> command . append ( spec ) <EOL> else : <EOL> command . extend ( [ '<STR_LIT>' , spec ] ) <EOL> print >> self . stdout , '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( command ) <EOL> self . do_command ( command ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT:p>' , ) , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , nargs = '<STR_LIT:?>' , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_print_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> worksheet_info = client . get_worksheet_info ( worksheet_uuid , True ) <EOL> if args . raw : <EOL> lines = worksheet_util . get_worksheet_lines ( worksheet_info ) <EOL> for line in lines : <EOL> print >> self . stdout , line <EOL> else : <EOL> print >> self . stdout , self . _worksheet_description ( worksheet_info ) <EOL> interpreted = worksheet_util . interpret_items ( worksheet_util . get_default_schemas ( ) , worksheet_info [ '<STR_LIT>' ] ) <EOL> self . display_interpreted ( client , worksheet_info , interpreted ) <EOL> def display_interpreted ( self , client , worksheet_info , interpreted ) : <EOL> for item in interpreted [ '<STR_LIT>' ] : <EOL> mode = item [ '<STR_LIT>' ] <EOL> data = item [ '<STR_LIT>' ] <EOL> properties = item [ '<STR_LIT>' ] <EOL> print >> self . stdout , '<STR_LIT>' <EOL> if mode == '<STR_LIT>' or mode == '<STR_LIT>' : <EOL> if mode == '<STR_LIT>' : <EOL> maxlines = properties . get ( '<STR_LIT>' ) <EOL> if maxlines : <EOL> maxlines = int ( maxlines ) <EOL> try : <EOL> self . print_target_info ( client , data , decorate = True , maxlines = maxlines ) <EOL> except UsageError , e : <EOL> print >> self . stdout , '<STR_LIT>' , e <EOL> else : <EOL> print >> self . stdout , data <EOL> elif mode == '<STR_LIT>' or mode == '<STR_LIT>' : <EOL> header , contents = data <EOL> contents = worksheet_util . interpret_genpath_table_contents ( client , contents ) <EOL> self . print_table ( header , contents , show_header = ( mode == '<STR_LIT>' ) , indent = '<STR_LIT:U+0020>' ) <EOL> elif mode == '<STR_LIT:html>' or mode == '<STR_LIT:image>' or mode == '<STR_LIT>' : <EOL> print >> self . stdout , '<STR_LIT:[>' + mode + '<STR_LIT:]>' <EOL> elif mode == '<STR_LIT>' : <EOL> search_interpreted = worksheet_util . interpret_search ( client , worksheet_info [ '<STR_LIT>' ] , data ) <EOL> self . display_interpreted ( client , worksheet_info , search_interpreted ) <EOL> elif mode == '<STR_LIT>' : <EOL> wsearch_interpreted = worksheet_util . interpret_wsearch ( client , data ) <EOL> self . display_interpreted ( client , worksheet_info , wsearch_interpreted ) <EOL> elif mode == '<STR_LIT>' : <EOL> print >> self . stdout , '<STR_LIT>' + self . simple_worksheet_str ( data ) + '<STR_LIT:]>' <EOL> else : <EOL> raise UsageError ( '<STR_LIT>' % mode ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> aliases = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , nargs = '<STR_LIT:*>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = ADDRESS_SPEC_FORMAT , completer = AddressesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> ) , <EOL> ) <EOL> def do_wls_command ( self , args ) : <EOL> if args . address : <EOL> address = self . manager . apply_alias ( args . address ) <EOL> client = self . manager . client ( address ) <EOL> else : <EOL> client = self . manager . current_client ( ) <EOL> worksheet_dicts = client . search_worksheets ( args . keywords ) <EOL> if args . uuid_only : <EOL> for row in worksheet_dicts : <EOL> print >> self . stdout , row [ '<STR_LIT>' ] <EOL> else : <EOL> if worksheet_dicts : <EOL> for row in worksheet_dicts : <EOL> row [ '<STR_LIT>' ] = '<STR_LIT>' % ( row [ '<STR_LIT>' ] , row [ '<STR_LIT>' ] ) <EOL> row [ '<STR_LIT>' ] = group_permissions_str ( row [ '<STR_LIT>' ] ) <EOL> post_funcs = { '<STR_LIT>' : UUID_POST_FUNC } <EOL> self . print_table ( ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) , worksheet_dicts , post_funcs ) <EOL> reference_map = self . create_reference_map ( '<STR_LIT>' , worksheet_dicts ) <EOL> return self . create_structured_info_map ( [ ( '<STR_LIT>' , reference_map ) ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_wrm_command ( self , args ) : <EOL> delete_current = False <EOL> current_client , current_worksheet = self . manager . get_current_worksheet_uuid ( ) <EOL> for worksheet_spec in args . worksheet_spec : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( worksheet_spec ) <EOL> if ( client , worksheet_uuid ) == ( current_client , current_worksheet ) : <EOL> delete_current = True <EOL> client . delete_worksheet ( worksheet_uuid , args . force ) <EOL> if delete_current : <EOL> return self . change_current_worksheet ( current_client , None , verbose = True ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> ) , <EOL> ) <EOL> def do_wadd_command ( self , args ) : <EOL> ( source_client , source_worksheet_uuid ) = self . parse_client_worksheet_uuid ( args . source_worksheet_spec ) <EOL> source_items = source_client . get_worksheet_info ( source_worksheet_uuid , True ) [ '<STR_LIT>' ] <EOL> ( dest_client , dest_worksheet_uuid ) = self . parse_client_worksheet_uuid ( args . dest_worksheet_spec ) <EOL> dest_worksheet_info = dest_client . get_worksheet_info ( dest_worksheet_uuid , True ) <EOL> dest_items = [ ] if args . replace else dest_worksheet_info [ '<STR_LIT>' ] <EOL> dest_client . update_worksheet_items ( dest_worksheet_info , dest_items + source_items ) <EOL> for item in source_items : <EOL> ( source_bundle_info , source_worksheet_info , value_obj , item_type ) = item <EOL> if item_type == worksheet_util . TYPE_BUNDLE : <EOL> self . copy_bundle ( source_client , source_bundle_info [ '<STR_LIT>' ] , dest_client , dest_worksheet_uuid , copy_dependencies = False , add_to_worksheet = False ) <EOL> print >> self . stdout , '<STR_LIT>' % ( len ( source_items ) , dest_worksheet_uuid ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_gls_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> group_dicts = client . list_groups ( ) <EOL> if group_dicts : <EOL> for row in group_dicts : <EOL> row [ '<STR_LIT>' ] = '<STR_LIT>' % ( row [ '<STR_LIT>' ] , row [ '<STR_LIT>' ] ) <EOL> self . print_table ( ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , group_dicts ) <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT:name>' , help = '<STR_LIT>' % spec_util . NAME_REGEX . pattern ) , <EOL> ) , <EOL> ) <EOL> def do_gnew_command ( self , args ) : <EOL> client = self . manager . current_client ( ) <EOL> group_dict = client . new_group ( args . name ) <EOL> print >> self . stdout , '<STR_LIT>' % ( group_dict [ '<STR_LIT:name>' ] , group_dict [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % GROUP_SPEC_FORMAT , completer = GroupsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_grm_command ( self , args ) : <EOL> client = self . manager . current_client ( ) <EOL> group_dict = client . rm_group ( args . group_spec ) <EOL> print >> self . stdout , '<STR_LIT>' % ( group_dict [ '<STR_LIT:name>' ] , group_dict [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % GROUP_SPEC_FORMAT , completer = GroupsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_ginfo_command ( self , args ) : <EOL> client = self . manager . current_client ( ) <EOL> group_dict = client . group_info ( args . group_spec ) <EOL> members = group_dict [ '<STR_LIT>' ] <EOL> for row in members : <EOL> row [ '<STR_LIT:user>' ] = '<STR_LIT>' % ( row [ '<STR_LIT>' ] , row [ '<STR_LIT>' ] ) <EOL> print >> self . stdout , '<STR_LIT>' % ( group_dict [ '<STR_LIT:name>' ] , group_dict [ '<STR_LIT>' ] ) <EOL> self . print_table ( ( '<STR_LIT:user>' , '<STR_LIT>' ) , group_dict [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % GROUP_SPEC_FORMAT , completer = GroupsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_uadd_command ( self , args ) : <EOL> client = self . manager . current_client ( ) <EOL> user_info = client . add_user ( args . user_spec , args . group_spec , args . admin ) <EOL> if '<STR_LIT>' in user_info : <EOL> print >> self . stdout , '<STR_LIT>' % ( user_info [ '<STR_LIT>' ] , <EOL> user_info [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:to>' if user_info [ '<STR_LIT>' ] == '<STR_LIT>' else '<STR_LIT>' , <EOL> user_info [ '<STR_LIT>' ] ) <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' % ( user_info [ '<STR_LIT:name>' ] , user_info [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' % GROUP_SPEC_FORMAT , completer = GroupsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_urm_command ( self , args ) : <EOL> client = self . manager . current_client ( ) <EOL> user_info = client . rm_user ( args . user_spec , args . group_spec ) <EOL> if user_info is None : <EOL> print >> self . stdout , '<STR_LIT>' % ( user_info [ '<STR_LIT:name>' ] , user_info [ '<STR_LIT>' ] ) <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' % ( user_info [ '<STR_LIT:name>' ] , user_info [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = GROUP_SPEC_FORMAT , completer = GroupsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = PERMISSION_SPEC_FORMAT , completer = ChoicesCompleter ( [ '<STR_LIT:none>' , '<STR_LIT>' , '<STR_LIT:all>' ] ) ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_perm_command ( self , args ) : <EOL> args . bundle_spec = spec_util . expand_specs ( args . bundle_spec ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundle_spec ) <EOL> result = client . set_bundles_perm ( bundle_uuids , args . group_spec , args . permission_spec ) <EOL> print >> self . stdout , "<STR_LIT>" % ( result [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> permission_str ( result [ '<STR_LIT>' ] ) , len ( bundle_uuids ) ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = GROUP_SPEC_FORMAT , completer = GroupsCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = PERMISSION_SPEC_FORMAT ) , <EOL> ) , <EOL> ) <EOL> def do_wperm_command ( self , args ) : <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> result = client . set_worksheet_perm ( worksheet_uuid , args . group_spec , args . permission_spec ) <EOL> print >> self . stdout , "<STR_LIT>" % ( result [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> permission_str ( result [ '<STR_LIT>' ] ) , result [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , result [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = BUNDLE_SPEC_FORMAT , nargs = '<STR_LIT:+>' , completer = BundlesCompleter ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % WORKSHEET_SPEC_FORMAT , completer = WorksheetsCompleter ) , <EOL> ) , <EOL> ) <EOL> def do_chown_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> args . bundle_spec = spec_util . expand_specs ( args . bundle_spec ) <EOL> client , worksheet_uuid = self . parse_client_worksheet_uuid ( args . worksheet_spec ) <EOL> bundle_uuids = worksheet_util . get_bundle_uuids ( client , worksheet_uuid , args . bundle_spec ) <EOL> client . chown_bundles ( bundle_uuids , args . user_spec ) <EOL> for uuid in bundle_uuids : <EOL> print >> self . stdout , uuid <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , help = '<STR_LIT>' , default = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' , type = int , default = None ) , <EOL> Commands . Argument ( '<STR_LIT>' , type = int , help = '<STR_LIT>' , default = <NUM_LIT:1> ) , <EOL> ) , <EOL> ) <EOL> def do_work_manager_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> worker_config = self . manager . config [ '<STR_LIT>' ] <EOL> if args . worker_type == '<STR_LIT>' : <EOL> machine = LocalMachine ( ) <EOL> elif args . worker_type in worker_config : <EOL> machine = RemoteMachine ( worker_config [ args . worker_type ] ) <EOL> else : <EOL> print >> self . stdout , '<STR_LIT>' + args . worker_type + '<STR_LIT>' + '<STR_LIT>' + self . manager . config_path <EOL> print >> self . stdout , '<STR_LIT>' + str ( map ( str , worker_config . keys ( ) ) ) <EOL> return <EOL> client = self . manager . local_client ( ) <EOL> worker = Worker ( client . bundle_store , client . model , machine , client . auth_handler ) <EOL> worker . run_loop ( args . num_iterations , args . sleep_time ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT:-c>' , '<STR_LIT>' , dest = '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , type = int , default = <NUM_LIT:0> ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , type = int , default = <NUM_LIT:20> ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_events_command ( self , args ) : <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> self . _fail_if_not_local ( '<STR_LIT>' ) <EOL> client = self . manager . current_client ( ) <EOL> query_info = { <EOL> '<STR_LIT:user>' : args . user , '<STR_LIT>' : args . match_command , '<STR_LIT:args>' : args . args , '<STR_LIT>' : args . uuid , <EOL> '<STR_LIT:count>' : args . count , '<STR_LIT>' : args . group_by <EOL> } <EOL> info = client . get_events_log_info ( query_info , args . offset , args . limit ) <EOL> if '<STR_LIT>' in info : <EOL> for row in info [ '<STR_LIT>' ] : <EOL> print >> self . stdout , '<STR_LIT:\t>' . join ( map ( str , list ( row ) ) ) <EOL> if '<STR_LIT>' in info : <EOL> for event in info [ '<STR_LIT>' ] : <EOL> row = [ <EOL> event . end_time . strftime ( '<STR_LIT>' ) if event . end_time != None else '<STR_LIT>' , <EOL> '<STR_LIT>' % event . duration if event . duration != None else '<STR_LIT>' , <EOL> '<STR_LIT>' % ( event . user_name , event . user_id ) , <EOL> event . command , event . args ] <EOL> print >> self . stdout , '<STR_LIT:\t>' . join ( row ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = [ <EOL> '<STR_LIT>' , <EOL> ] , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_uedit_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> client = self . manager . current_client ( ) <EOL> user_info = client . get_user_info ( args . user_id ) <EOL> if args . time_quota is not None : <EOL> user_info [ '<STR_LIT>' ] = formatting . parse_duration ( args . time_quota ) <EOL> if args . disk_quota is not None : <EOL> user_info [ '<STR_LIT>' ] = formatting . parse_size ( args . disk_quota ) <EOL> client . update_user_info ( user_info ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_reset_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> self . _fail_if_headless ( '<STR_LIT>' ) <EOL> self . _fail_if_not_local ( '<STR_LIT>' ) <EOL> if not args . commit : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> client = self . manager . current_client ( ) <EOL> print >> self . stdout , '<STR_LIT>' <EOL> client . bundle_store . reset ( ) <EOL> print >> self . stdout , '<STR_LIT>' <EOL> client . model . _reset ( ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> def do_server_command ( self , args ) : <EOL> raise UsageError ( '<STR_LIT>' ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT:name>' , <EOL> help = '<STR_LIT>' , ) , <EOL> Commands . Argument ( '<STR_LIT:path>' , <EOL> help = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) , ) , <EOL> ) , <EOL> ) <EOL> def do_add_partition_command ( self , args ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( self . manager . bundle_store ( ) , MultiDiskBundleStore ) : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . manager . bundle_store ( ) . add_partition ( args . path , args . name ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> ) , <EOL> ) <EOL> def do_rm_partition_command ( self , args ) : <EOL> if not isinstance ( self . manager . bundle_store ( ) , MultiDiskBundleStore ) : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . manager . bundle_store ( ) . rm_partition ( args . partition ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( ) , <EOL> ) <EOL> def do_ls_partitions_command ( self , _ ) : <EOL> if not isinstance ( self . manager . bundle_store ( ) , MultiDiskBundleStore ) : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . manager . bundle_store ( ) . ls_partitions ( ) <EOL> @ Commands . command ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> arguments = ( <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> Commands . Argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) , <EOL> ) , <EOL> ) <EOL> def do_bs_health_check ( self , args ) : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> self . manager . bundle_store ( ) . health_check ( self . manager . current_client ( ) . model , args . force , args . data_hash , args . repair ) <EOL> def _fail_if_headless ( self , message ) : <EOL> if self . headless : <EOL> raise UsageError ( '<STR_LIT>' % message ) <EOL> def _fail_if_not_local ( self , message ) : <EOL> from codalab . client . local_bundle_client import LocalBundleClient <EOL> if not isinstance ( self . manager . current_client ( ) , LocalBundleClient ) : <EOL> raise UsageError ( '<STR_LIT>' % message ) </s>
