<s> from gerrymander . client import ClientLive <EOL> from gerrymander . client import ClientCaching <EOL> from gerrymander . operations import OperationQuery <EOL> from gerrymander . operations import OperationWatch <EOL> from gerrymander . reports import ReportOutput <EOL> from gerrymander . reports import ReportPatchReviewStats <EOL> from gerrymander . reports import ReportPatchReviewRate <EOL> from gerrymander . reports import ReportOpenReviewStats <EOL> from gerrymander . reports import ReportChanges <EOL> from gerrymander . reports import ReportToDoListMine <EOL> from gerrymander . reports import ReportToDoListOthers <EOL> from gerrymander . reports import ReportToDoListAnyones <EOL> from gerrymander . reports import ReportToDoListNoones <EOL> from gerrymander . reports import ReportToDoListApprovable <EOL> from gerrymander . reports import ReportToDoListExpirable <EOL> from gerrymander . format import format_color <EOL> from gerrymander . model import ModelEventCommentAdd <EOL> from gerrymander . model import ModelEventPatchCreate <EOL> from gerrymander . model import ModelEventChangeMerge <EOL> from gerrymander . model import ModelEventChangeAbandon <EOL> from gerrymander . model import ModelEventChangeRestore <EOL> from gerrymander . model import ModelApproval <EOL> from gerrymander . pager import start_pager , stop_pager <EOL> from gerrymander . git import get_remote_info <EOL> import getpass <EOL> import os <EOL> import logging <EOL> import sys <EOL> import argparse <EOL> import textwrap <EOL> try : <EOL> import configparser <EOL> except : <EOL> import ConfigParser as configparser <EOL> class CommandConfig ( object ) : <EOL> def __init__ ( self , filename ) : <EOL> self . filename = os . path . expanduser ( filename ) <EOL> self . config = configparser . ConfigParser ( ) <EOL> self . config . read ( [ self . filename ] ) <EOL> if self . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . set_gerrit_from_remote ( ) <EOL> def set_gerrit_from_remote ( self ) : <EOL> remote = self . get_option_string ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not remote : <EOL> return <EOL> user , host , port = get_remote_info ( remote ) <EOL> if host : <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT>' , host ) <EOL> if user : <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT:username>' , user ) <EOL> if port : <EOL> self . config . set ( '<STR_LIT>' , '<STR_LIT:port>' , port ) <EOL> def has_option ( self , section , name ) : <EOL> return self . config . has_option ( section , name ) <EOL> def get_option_string ( self , section , name , defvalue = None ) : <EOL> if not self . config . has_option ( section , name ) : <EOL> return defvalue <EOL> return self . config . get ( section , name ) <EOL> def get_option_int ( self , section , name , defvalue = None ) : <EOL> if not self . config . has_option ( section , name ) : <EOL> return defvalue <EOL> return int ( self . config . get ( section , name ) ) <EOL> def get_option_list ( self , section , name , defvalue = None ) : <EOL> if not self . config . has_option ( section , name ) : <EOL> return defvalue <EOL> value = self . config . get ( section , name ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_option_bool ( self , section , name , defvalue = None ) : <EOL> if not self . config . has_option ( section , name ) : <EOL> return defvalue <EOL> value = self . config . get ( section , name ) . strip ( ) <EOL> if value . lower ( ) in [ "<STR_LIT:true>" , "<STR_LIT:yes>" ] : <EOL> return True <EOL> return False <EOL> def get_server_username ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT:username>" ) : <EOL> return None <EOL> return self . config . get ( "<STR_LIT>" , "<STR_LIT:username>" ) <EOL> def get_server_hostname ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return "<STR_LIT>" <EOL> return self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def get_server_port ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT:port>" ) : <EOL> return <NUM_LIT> <EOL> return self . config . get ( "<STR_LIT>" , "<STR_LIT:port>" ) <EOL> def get_server_keyfile ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return None <EOL> return self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def get_cache_longlifetime ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return <NUM_LIT> <EOL> return int ( self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def get_cache_shortlifetime ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return <NUM_LIT> <EOL> return int ( self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def get_cache_directory ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return os . path . expanduser ( "<STR_LIT>" ) <EOL> return os . path . expanduser ( self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> def get_organization_groups ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return [ ] <EOL> value = self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_organization_teams ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return [ ] <EOL> value = self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_organization_bots ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return [ ] <EOL> value = self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_group_projects ( self , groupname ) : <EOL> section = "<STR_LIT>" + groupname <EOL> if not self . config . has_option ( section , "<STR_LIT>" ) : <EOL> return [ ] <EOL> value = self . config . get ( section , "<STR_LIT>" ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_group_team_members ( self , groupname , teamname ) : <EOL> section = "<STR_LIT>" + groupname <EOL> key = "<STR_LIT>" + teamname <EOL> if not self . config . has_option ( section , key ) : <EOL> return [ ] <EOL> value = self . config . get ( section , key ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_command_aliases ( self ) : <EOL> if not self . config . has_option ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return [ ] <EOL> value = self . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return list ( map ( lambda x : x . strip ( ) , value . split ( "<STR_LIT:U+002C>" ) ) ) <EOL> def get_command_alias_basecmd ( self , aliasname ) : <EOL> section = "<STR_LIT>" + aliasname <EOL> return self . config . get ( section , "<STR_LIT>" ) <EOL> def get_command_alias_help ( self , aliasname ) : <EOL> section = "<STR_LIT>" + aliasname <EOL> return self . config . get ( section , "<STR_LIT>" ) <EOL> class Command ( object ) : <EOL> def __init__ ( self , name , help ) : <EOL> super ( Command , self ) . __init__ ( ) <EOL> self . name = name <EOL> self . help = help <EOL> self . pager = True <EOL> def add_option ( self , parser , config , * args , ** kwargs ) : <EOL> if args [ <NUM_LIT:0> ] [ <NUM_LIT:0> : <NUM_LIT:1> ] == "<STR_LIT:->" : <EOL> if args [ <NUM_LIT:0> ] [ <NUM_LIT:0> : <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> name = args [ <NUM_LIT:0> ] [ <NUM_LIT:2> : ] <EOL> else : <EOL> name = args [ <NUM_LIT:1> ] [ <NUM_LIT:2> : ] <EOL> else : <EOL> name = args [ <NUM_LIT:0> ] <EOL> section = "<STR_LIT>" + self . name <EOL> if config . has_option ( section , name ) : <EOL> defvalue = kwargs [ "<STR_LIT:default>" ] <EOL> if type ( defvalue ) == list : <EOL> kwargs [ "<STR_LIT:default>" ] = config . get_option_list ( section , name ) <EOL> elif type ( defvalue ) == int : <EOL> kwargs [ "<STR_LIT:default>" ] = config . get_option_int ( section , name ) <EOL> elif type ( defvalue ) == bool : <EOL> kwargs [ "<STR_LIT:default>" ] = config . get_option_bool ( section , name ) <EOL> else : <EOL> kwargs [ "<STR_LIT:default>" ] = config . get_option_string ( section , name ) <EOL> return parser . add_argument ( * args , ** kwargs ) <EOL> def add_options ( self , parser , config ) : <EOL> pass <EOL> def get_client ( self , config , options ) : <EOL> return ClientLive ( config . get_server_hostname ( ) , <EOL> config . get_server_port ( ) , <EOL> config . get_server_username ( ) , <EOL> config . get_server_keyfile ( ) ) <EOL> def run ( self , config , client , options ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def execute ( self , config , options ) : <EOL> if self . pager : <EOL> start_pager ( ) <EOL> try : <EOL> client = self . get_client ( config , options ) <EOL> self . run ( config , client , options ) <EOL> finally : <EOL> if self . pager : <EOL> stop_pager ( ) <EOL> class CommandCaching ( Command ) : <EOL> def __init__ ( self , name , help ) : <EOL> super ( CommandCaching , self ) . __init__ ( name , help ) <EOL> self . longcache = False <EOL> def set_long_cache ( self , longcache ) : <EOL> self . longcache = longcache <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandCaching , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> def get_client ( self , config , options ) : <EOL> if options . no_cache : <EOL> return ClientLive ( config . get_server_hostname ( ) , <EOL> config . get_server_port ( ) , <EOL> config . get_server_username ( ) , <EOL> config . get_server_keyfile ( ) ) <EOL> else : <EOL> if self . longcache : <EOL> return ClientCaching ( config . get_server_hostname ( ) , <EOL> config . get_server_port ( ) , <EOL> config . get_server_username ( ) , <EOL> config . get_server_keyfile ( ) , <EOL> os . path . join ( config . get_cache_directory ( ) , "<STR_LIT>" ) , <EOL> config . get_cache_longlifetime ( ) , <EOL> options . refresh ) <EOL> else : <EOL> return ClientCaching ( config . get_server_hostname ( ) , <EOL> config . get_server_port ( ) , <EOL> config . get_server_username ( ) , <EOL> config . get_server_keyfile ( ) , <EOL> os . path . join ( config . get_cache_directory ( ) , "<STR_LIT>" ) , <EOL> config . get_cache_shortlifetime ( ) , <EOL> options . refresh ) <EOL> class CommandProject ( Command ) : <EOL> def __init__ ( self , name , help ) : <EOL> super ( CommandProject , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandProject , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , "<STR_LIT>" , default = [ ] , <EOL> action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , "<STR_LIT>" , default = [ ] , <EOL> action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> def get_projects ( self , config , options , requireOne = False ) : <EOL> count = <NUM_LIT:0> <EOL> if len ( options . project ) > <NUM_LIT:0> : <EOL> count = count + <NUM_LIT:1> <EOL> if len ( options . group ) > <NUM_LIT:0> : <EOL> count = count + <NUM_LIT:1> <EOL> if options . all_groups : <EOL> count = count + <NUM_LIT:1> <EOL> if count > <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if count == <NUM_LIT:0> and requireOne : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if len ( options . project ) == <NUM_LIT:0> : <EOL> projects = [ ] <EOL> if options . all_groups : <EOL> groups = config . get_organization_groups ( ) <EOL> else : <EOL> groups = options . group <EOL> for group in groups : <EOL> projects . extend ( config . get_group_projects ( group ) ) <EOL> return projects <EOL> else : <EOL> return options . project <EOL> class CommandWatch ( CommandProject ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandWatch , self ) . __init__ ( name , help ) <EOL> self . pager = False <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandWatch , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def wrap_text ( message , indent = "<STR_LIT>" , width = <NUM_LIT> ) : <EOL> lines = textwrap . wrap ( message , width ) <EOL> return "<STR_LIT:\n>" . join ( map ( lambda x : indent + x , lines ) ) <EOL> @ staticmethod <EOL> def format_comment ( comment , user , usecolor ) : <EOL> if comment == "<STR_LIT>" : <EOL> return <EOL> print ( "<STR_LIT>" % ( format_color ( user . name , <EOL> usecolor , <EOL> styles = [ "<STR_LIT>" ] ) , <EOL> user . username ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( CommandComments . wrap_text ( comment , "<STR_LIT:U+0020>" ) ) <EOL> @ staticmethod <EOL> def format_approvals ( approvals ) : <EOL> bits = [ ] <EOL> for approval in approvals : <EOL> if approval . action == ModelApproval . ACTION_WORKFLOW and approval . value > <NUM_LIT:0> : <EOL> bits . append ( "<STR_LIT>" ) <EOL> elif approval . action == ModelApproval . ACTION_REVIEWED : <EOL> if approval . value > <NUM_LIT:0> : <EOL> bits . append ( "<STR_LIT>" + str ( approval . value ) ) <EOL> elif approval . value < <NUM_LIT:0> : <EOL> bits . append ( "<STR_LIT:R>" + str ( approval . value ) ) <EOL> else : <EOL> bits . append ( "<STR_LIT>" ) <EOL> elif approval . action == ModelApproval . ACTION_VERIFIED : <EOL> if approval . value > <NUM_LIT:0> : <EOL> bits . append ( "<STR_LIT>" + str ( approval . value ) ) <EOL> elif approval . value < <NUM_LIT:0> : <EOL> bits . append ( "<STR_LIT>" + str ( approval . value ) ) <EOL> else : <EOL> bits . append ( "<STR_LIT>" ) <EOL> return "<STR_LIT:U+002C>" . join ( bits ) <EOL> @ staticmethod <EOL> def format_event ( event , bots , projects , usecolor ) : <EOL> if event . user is None or event . change is None : <EOL> return <EOL> if event . is_user_in_list ( bots ) : <EOL> return <EOL> if len ( projects ) > <NUM_LIT:0> and event . change . project not in projects : <EOL> return <EOL> change = event . change <EOL> print ( format_color ( "<STR_LIT>" % ( change . url , change . id ) , <EOL> usecolor , <EOL> fg = "<STR_LIT>" , <EOL> styles = [ "<STR_LIT>" ] ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % change . project ) <EOL> print ( "<STR_LIT>" % change . subject ) <EOL> if type ( event ) == ModelEventChangeRestore : <EOL> print ( "<STR_LIT>" % format_color ( "<STR_LIT>" , styles = [ "<STR_LIT>" ] ) ) <EOL> elif type ( event ) == ModelEventChangeAbandon : <EOL> print ( "<STR_LIT>" % format_color ( "<STR_LIT>" , styles = [ "<STR_LIT>" ] ) ) <EOL> elif type ( event ) == ModelEventChangeMerge : <EOL> print ( "<STR_LIT>" % format_color ( "<STR_LIT>" , styles = [ "<STR_LIT>" ] ) ) <EOL> elif type ( event ) == ModelEventCommentAdd : <EOL> print ( "<STR_LIT>" % format_color ( "<STR_LIT>" , styles = [ "<STR_LIT>" ] ) ) <EOL> if len ( event . approvals ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % <EOL> format_color ( CommandWatch . format_approvals ( event . approvals ) , <EOL> usecolor , <EOL> fg = "<STR_LIT>" , <EOL> styles = [ "<STR_LIT>" ] ) ) <EOL> elif type ( event ) == ModelEventPatchCreate : <EOL> print ( "<STR_LIT>" % format_color ( "<STR_LIT>" , styles = [ "<STR_LIT>" ] ) ) <EOL> if type ( event ) == ModelEventCommentAdd : <EOL> print ( "<STR_LIT>" ) <EOL> CommandWatch . format_comment ( event . comment , event . user , usecolor ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> def run ( self , config , client , options ) : <EOL> watch = OperationWatch ( client ) <EOL> if options . all : <EOL> bots = [ ] <EOL> else : <EOL> bots = config . get_organization_bots ( ) <EOL> projects = self . get_projects ( config , options ) <EOL> def cb ( event ) : <EOL> self . format_event ( event , <EOL> bots , <EOL> projects , <EOL> options . color ) <EOL> return watch . run ( cb ) <EOL> class CommandReport ( Command ) : <EOL> def __init__ ( self , name , help ) : <EOL> super ( CommandReport , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandReport , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , "<STR_LIT>" , default = ReportOutput . DISPLAY_MODE_TEXT , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> def get_report ( self , config , client , options ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def run ( self , config , client , options ) : <EOL> report = self . get_report ( config , client , options ) <EOL> report . display ( options . mode ) <EOL> class CommandReportTable ( CommandReport ) : <EOL> def __init__ ( self , name , help ) : <EOL> super ( CommandReportTable , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandReportTable , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> self . sort_option = self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = [ ] , <EOL> action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> def run ( self , config , client , options ) : <EOL> report = self . get_report ( config , client , options ) <EOL> if options . limit is not None : <EOL> report . set_data_limit ( int ( options . limit ) ) <EOL> if options . sort is not None : <EOL> reverse = False <EOL> offset = options . sort . find ( "<STR_LIT::>" ) <EOL> if offset != - <NUM_LIT:1> : <EOL> if options . sort [ offset + <NUM_LIT:1> : ] == "<STR_LIT>" : <EOL> reverse = True <EOL> options . sort = options . sort [ <NUM_LIT:0> : offset ] <EOL> report . set_sort_column ( options . sort , reverse ) <EOL> if len ( options . field ) > <NUM_LIT:0> : <EOL> for col in report . get_columns ( ) : <EOL> col . visible = False <EOL> for key in options . field : <EOL> name = key <EOL> offset = name . find ( "<STR_LIT::>" ) <EOL> truncate = None <EOL> if offset != - <NUM_LIT:1> : <EOL> truncate = int ( name [ offset + <NUM_LIT:1> : ] ) <EOL> name = name [ <NUM_LIT:0> : offset ] <EOL> col = report . get_column ( name ) <EOL> if col is None : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> if truncate is not None : <EOL> col . truncate = truncate <EOL> col . visible = True <EOL> report . display ( options . mode ) <EOL> class CommandPatchReviewStats ( CommandProject , CommandCaching , CommandReportTable ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandPatchReviewStats , self ) . __init__ ( name , help ) <EOL> self . teams = { } <EOL> self . set_long_cache ( True ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandPatchReviewStats , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = <NUM_LIT:30> , <EOL> help = "<STR_LIT>" ) <EOL> def get_report ( self , config , client , options ) : <EOL> if options . all_groups : <EOL> groups = config . get_organization_groups ( ) <EOL> else : <EOL> groups = options . group <EOL> teams = { } <EOL> for team in config . get_organization_teams ( ) : <EOL> teams [ team ] = [ ] <EOL> for group in groups : <EOL> users = config . get_group_team_members ( group , team ) <EOL> teams [ team ] . extend ( users ) <EOL> return ReportPatchReviewStats ( client , <EOL> self . get_projects ( config , options , True ) , <EOL> int ( options . days ) , <EOL> teams , <EOL> usecolor = options . color ) <EOL> def run ( self , config , client , options ) : <EOL> return super ( CommandPatchReviewStats , self ) . run ( config , client , options ) <EOL> class CommandPatchReviewRate ( CommandProject , CommandCaching , CommandReportTable ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandPatchReviewRate , self ) . __init__ ( name , help ) <EOL> self . teams = { } <EOL> self . set_long_cache ( True ) <EOL> def get_report ( self , config , client , options ) : <EOL> if options . all_groups : <EOL> groups = config . get_organization_groups ( ) <EOL> else : <EOL> groups = options . group <EOL> teams = { } <EOL> for team in config . get_organization_teams ( ) : <EOL> teams [ team ] = [ ] <EOL> for group in groups : <EOL> users = config . get_group_team_members ( group , team ) <EOL> teams [ team ] . extend ( users ) <EOL> return ReportPatchReviewRate ( client , <EOL> self . get_projects ( config , options , True ) , <EOL> teams , <EOL> usecolor = options . color ) <EOL> def run ( self , config , client , options ) : <EOL> return super ( CommandPatchReviewRate , self ) . run ( config , client , options ) <EOL> class CommandOpenReviewStats ( CommandProject , CommandCaching , CommandReportTable ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandOpenReviewStats , self ) . __init__ ( name , help ) <EOL> self . teams = { } <EOL> self . set_long_cache ( True ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandOpenReviewStats , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = <NUM_LIT:7> , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> def get_report ( self , config , client , options ) : <EOL> return ReportOpenReviewStats ( client , <EOL> self . get_projects ( config , options , True ) , <EOL> options . branch , <EOL> options . topic , <EOL> int ( options . days ) , <EOL> usecolor = options . color ) <EOL> def run ( self , config , client , options ) : <EOL> if options . limit is None : <EOL> options . limit = <NUM_LIT:5> <EOL> return super ( CommandOpenReviewStats , self ) . run ( config , client , options ) <EOL> class CommandChanges ( CommandProject , CommandCaching , CommandReportTable ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandChanges , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandChanges , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT:file>" , default = [ ] , nargs = "<STR_LIT:*>" , <EOL> help = "<STR_LIT>" ) <EOL> self . sort_option . choices = [ c . key for c in ReportChanges . COLUMNS ] <EOL> def get_report ( self , config , client , options ) : <EOL> return ReportChanges ( client , <EOL> self . get_projects ( config , options ) , <EOL> status = options . status , <EOL> reviewers = options . reviewer , <EOL> branches = options . branch , <EOL> topics = options . topic , <EOL> messages = options . message , <EOL> owners = options . owner , <EOL> approvals = options . approval , <EOL> rawquery = options . rawquery , <EOL> files = options . file , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandToDo ( CommandProject , CommandCaching , CommandReportTable ) : <EOL> def __init__ ( self , name , help ) : <EOL> super ( CommandToDo , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandToDo , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT:file>" , default = [ ] , nargs = "<STR_LIT:*>" , <EOL> help = "<STR_LIT>" ) <EOL> class CommandToDoMine ( CommandToDo ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandToDoMine , self ) . __init__ ( name , help ) <EOL> def get_report ( self , config , client , options ) : <EOL> username = config . get_server_username ( ) <EOL> if username is None : <EOL> username = getpass . getuser ( ) <EOL> return ReportToDoListMine ( client , <EOL> username = username , <EOL> projects = self . get_projects ( config , options ) , <EOL> branches = options . branch , <EOL> files = options . file , <EOL> topics = options . topic , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandToDoOthers ( CommandToDo ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandToDoOthers , self ) . __init__ ( name , help ) <EOL> def get_report ( self , config , client , options ) : <EOL> username = config . get_server_username ( ) <EOL> if username is None : <EOL> username = getpass . getuser ( ) <EOL> return ReportToDoListOthers ( client , <EOL> username = username , <EOL> projects = self . get_projects ( config , options ) , <EOL> branches = options . branch , <EOL> files = options . file , <EOL> topics = options . topic , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandToDoAnyones ( CommandToDo ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandToDoAnyones , self ) . __init__ ( name , help ) <EOL> def get_report ( self , config , client , options ) : <EOL> username = config . get_server_username ( ) <EOL> if username is None : <EOL> username = getpass . getuser ( ) <EOL> return ReportToDoListAnyones ( client , <EOL> username = username , <EOL> bots = config . get_organization_bots ( ) , <EOL> projects = self . get_projects ( config , options ) , <EOL> branches = options . branch , <EOL> files = options . file , <EOL> topics = options . topic , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandToDoNoones ( CommandToDo ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandToDoNoones , self ) . __init__ ( name , help ) <EOL> def get_report ( self , config , client , options ) : <EOL> username = config . get_server_username ( ) <EOL> if username is None : <EOL> username = getpass . getuser ( ) <EOL> return ReportToDoListNoones ( client , <EOL> username = username , <EOL> bots = config . get_organization_bots ( ) , <EOL> projects = self . get_projects ( config , options ) , <EOL> branches = options . branch , <EOL> files = options . file , <EOL> topics = options . topic , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandToDoApprovable ( CommandToDo ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandToDoApprovable , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandToDoApprovable , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> def get_report ( self , config , client , options ) : <EOL> username = config . get_server_username ( ) <EOL> if username is None : <EOL> username = getpass . getuser ( ) <EOL> return ReportToDoListApprovable ( client , <EOL> username = username , <EOL> strict = options . strict , <EOL> projects = self . get_projects ( config , options ) , <EOL> branches = options . branch , <EOL> files = options . file , <EOL> topics = options . topic , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandToDoExpirable ( CommandToDo ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandToDoExpirable , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandToDoExpirable , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> def get_report ( self , config , client , options ) : <EOL> return ReportToDoListExpirable ( client , <EOL> age = int ( options . age ) , <EOL> projects = self . get_projects ( config , options ) , <EOL> branches = options . branch , <EOL> files = options . file , <EOL> topics = options . topic , <EOL> usecolor = options . color , <EOL> deps = options . deps ) <EOL> class CommandComments ( CommandCaching ) : <EOL> def __init__ ( self , name = "<STR_LIT>" , help = "<STR_LIT>" ) : <EOL> super ( CommandComments , self ) . __init__ ( name , help ) <EOL> def add_options ( self , parser , config ) : <EOL> super ( CommandComments , self ) . add_options ( parser , config ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> self . add_option ( parser , config , <EOL> "<STR_LIT>" , default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def wrap_text ( message , indent = "<STR_LIT>" , width = <NUM_LIT> ) : <EOL> lines = textwrap . wrap ( message , width ) <EOL> return "<STR_LIT:\n>" . join ( map ( lambda x : indent + x , lines ) ) <EOL> @ staticmethod <EOL> def format_comments ( allcomments , bots , usecolor ) : <EOL> comments = [ ] <EOL> for comment in allcomments : <EOL> if not comment . is_reviewer_in_list ( bots ) : <EOL> comments . append ( comment ) <EOL> if len ( comments ) == <NUM_LIT:0> : <EOL> print ( format_color ( "<STR_LIT>" , <EOL> usecolor , <EOL> fg = "<STR_LIT>" ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> for comment in comments : <EOL> if comment . file is not None : <EOL> print ( "<STR_LIT>" % <EOL> ( format_color ( comment . reviewer . name , <EOL> usecolor , <EOL> styles = [ "<STR_LIT>" ] ) , <EOL> comment . reviewer . username , <EOL> comment . file , <EOL> comment . line ) ) <EOL> else : <EOL> print ( "<STR_LIT>" % <EOL> ( format_color ( comment . reviewer . name , <EOL> usecolor , <EOL> styles = [ "<STR_LIT>" ] ) , <EOL> comment . reviewer . username ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( CommandComments . wrap_text ( comment . message , "<STR_LIT:U+0020>" ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def format_change ( change , bots , usecolor , currentpatch , patchnum ) : <EOL> print ( format_color ( "<STR_LIT>" % ( change . url , change . id ) , <EOL> usecolor , <EOL> fg = "<STR_LIT>" , <EOL> styles = [ "<STR_LIT>" ] ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % change . subject ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> patches = change . patches <EOL> if currentpatch : <EOL> patches = patches [ - <NUM_LIT:1> : ] <EOL> elif patchnum : <EOL> patches = patches [ patchnum - <NUM_LIT:1> : patchnum ] <EOL> for patch in patches : <EOL> print ( format_color ( "<STR_LIT>" % ( patch . number , patch . revision ) , <EOL> usecolor , <EOL> fg = "<STR_LIT>" , <EOL> styles = [ "<STR_LIT>" ] ) ) <EOL> print ( "<STR_LIT>" ) <EOL> comments = [ ] <EOL> comments . extend ( patch . comments ) <EOL> prefix = "<STR_LIT>" % patch . number <EOL> abandoned = <NUM_LIT:0> <EOL> for comment in change . comments : <EOL> if comment . message . startswith ( prefix ) : <EOL> comments . append ( comment ) <EOL> if comment . message . startswith ( prefix + "<STR_LIT>" ) : <EOL> abandoned = patch . number <EOL> elif comment . message . startswith ( "<STR_LIT>" ) and abandoned == patch . number : <EOL> comments . append ( comment ) <EOL> CommandComments . format_comments ( comments , bots , usecolor ) <EOL> def run ( self , config , client , options ) : <EOL> change = options . change <EOL> query = OperationQuery ( client , <EOL> { <EOL> "<STR_LIT>" : [ change ] , <EOL> } , <EOL> patches = OperationQuery . PATCHES_ALL , <EOL> approvals = True , <EOL> files = True , <EOL> comments = True ) <EOL> if options . all : <EOL> bots = [ ] <EOL> else : <EOL> bots = config . get_organization_bots ( ) <EOL> def mycb ( change ) : <EOL> self . format_change ( change , bots , options . color , options . current , int ( options . patch ) ) <EOL> query . run ( mycb , limit = <NUM_LIT:1> ) <EOL> class CommandTool ( object ) : <EOL> def __init__ ( self ) : <EOL> self . commands = { } <EOL> def add_options ( self , parser ) : <EOL> parser . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , default = os . path . expanduser ( "<STR_LIT>" ) , <EOL> help = ( "<STR_LIT>" % <EOL> os . path . expanduser ( "<STR_LIT>" ) ) ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , <EOL> help = "<STR_LIT>" , <EOL> action = "<STR_LIT:store_true>" ) <EOL> def add_command ( self , subparser , config , cmdclass , name = None , help = None ) : <EOL> cmd = cmdclass ( ) <EOL> if name is not None : <EOL> cmd . name = name <EOL> if help is not None : <EOL> cmd . help = help <EOL> parser = subparser . add_parser ( cmd . name , help = cmd . help ) <EOL> cmd . add_options ( parser , config ) <EOL> parser . set_defaults ( func = cmd . execute ) <EOL> self . commands [ cmd . name ] = cmd <EOL> def add_default_commands ( self , subparser , config ) : <EOL> self . add_command ( subparser , config , CommandWatch ) <EOL> self . add_command ( subparser , config , CommandToDoNoones ) <EOL> self . add_command ( subparser , config , CommandToDoAnyones ) <EOL> self . add_command ( subparser , config , CommandToDoMine ) <EOL> self . add_command ( subparser , config , CommandToDoOthers ) <EOL> self . add_command ( subparser , config , CommandToDoApprovable ) <EOL> self . add_command ( subparser , config , CommandToDoExpirable ) <EOL> self . add_command ( subparser , config , CommandPatchReviewStats ) <EOL> self . add_command ( subparser , config , CommandPatchReviewRate ) <EOL> self . add_command ( subparser , config , CommandOpenReviewStats ) <EOL> self . add_command ( subparser , config , CommandChanges ) <EOL> self . add_command ( subparser , config , CommandComments ) <EOL> def add_config_commands ( self , subparser , config ) : <EOL> aliases = config . get_command_aliases ( ) <EOL> for alias in aliases : <EOL> basecmd = config . get_command_alias_basecmd ( alias ) <EOL> help = config . get_command_alias_help ( alias ) <EOL> if basecmd not in self . commands : <EOL> raise Exception ( "<STR_LIT>" % basecmd ) <EOL> klass = type ( self . commands [ basecmd ] ) <EOL> self . add_command ( subparser , config , klass , alias , help ) <EOL> def get_config ( self , options ) : <EOL> return CommandConfig ( options . config ) <EOL> def execute ( self , argv ) : <EOL> miniparser = argparse . ArgumentParser ( add_help = False ) <EOL> miniparser . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , default = os . path . expanduser ( "<STR_LIT>" ) , <EOL> help = ( "<STR_LIT>" % <EOL> os . path . expanduser ( "<STR_LIT>" ) ) ) <EOL> options , remaining = miniparser . parse_known_args ( argv ) <EOL> config = CommandConfig ( options . config ) <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> self . add_options ( parser ) <EOL> subparser = parser . add_subparsers ( ) <EOL> subparser . required = True <EOL> subparser . dest = "<STR_LIT>" <EOL> self . add_default_commands ( subparser , config ) <EOL> self . add_config_commands ( subparser , config ) <EOL> options = parser . parse_args ( argv ) <EOL> level = logging . WARNING <EOL> if options . debug : <EOL> level = logging . DEBUG <EOL> elif options . quiet : <EOL> level = logging . ERROR <EOL> logging . basicConfig ( level = level , <EOL> format = '<STR_LIT>' , <EOL> stream = sys . stderr ) <EOL> options . func ( config , options ) </s>
