<s> """<STR_LIT>""" <EOL> import sys <EOL> import setup_path <EOL> from utils import admin_api_tool_errors <EOL> from utils import auth_helper <EOL> from utils import cmd_utils <EOL> from utils import common_flags <EOL> from utils import file_manager <EOL> from utils import log_utils <EOL> from utils import report_utils <EOL> _REPORT_USERS_ORGS_FILE_NAME = '<STR_LIT>' <EOL> _REPORT_ORGS_BY_ACTIVE_FILE_NAME = '<STR_LIT>' <EOL> _REPORT_ORGS_BY_OU_FILE_NAME = '<STR_LIT>' <EOL> _REPORT_BY_ACTIVE_HEADER = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _REPORT_BY_ORG_HEADER = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _REPORT_FILTER_FIELDS = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> FILE_MANAGER = file_manager . FILE_MANAGER <EOL> def GenerateUserReport ( flags ) : <EOL> """<STR_LIT>""" <EOL> if FILE_MANAGER . FileExists ( _REPORT_USERS_ORGS_FILE_NAME ) and not flags . force : <EOL> print '<STR_LIT>' <EOL> return <EOL> print '<STR_LIT>' <EOL> arg_list = [ <EOL> '<STR_LIT>' % _REPORT_USERS_ORGS_FILE_NAME , <EOL> '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( _REPORT_FILTER_FIELDS ) , <EOL> ] <EOL> for flag_value , flag_string in [ <EOL> ( flags . apps_domain , '<STR_LIT>' % flags . apps_domain ) , <EOL> ( flags . force , '<STR_LIT>' ) , <EOL> ( flags . verbose , '<STR_LIT>' ) ] : <EOL> if flag_value : <EOL> arg_list . append ( flag_string ) <EOL> try : <EOL> cmd_utils . RunPyCmd ( '<STR_LIT>' , arg_list ) <EOL> except admin_api_tool_errors . AdminAPIToolCmdError as e : <EOL> log_utils . LogError ( '<STR_LIT>' , e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def _ReadAndCountUsersOrgsCSV ( ) : <EOL> """<STR_LIT>""" <EOL> csv_rows = FILE_MANAGER . ReadCsvFile ( _REPORT_USERS_ORGS_FILE_NAME ) <EOL> if csv_rows [ <NUM_LIT:0> ] != _REPORT_FILTER_FIELDS : <EOL> log_utils . LogError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> FILE_MANAGER . BuildFullPathToFileName ( <EOL> _REPORT_USERS_ORGS_FILE_NAME ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> active_user_count = report_utils . Counter ( ) <EOL> suspended_user_count = report_utils . Counter ( ) <EOL> for org , suspended in csv_rows [ <NUM_LIT:1> : ] : <EOL> if suspended == '<STR_LIT:True>' : <EOL> suspended_user_count . Increment ( org ) <EOL> else : <EOL> active_user_count . Increment ( org ) <EOL> return active_user_count , suspended_user_count <EOL> def SummarizeUserOrgReport ( ) : <EOL> """<STR_LIT>""" <EOL> active_user_counter , suspended_user_counter = _ReadAndCountUsersOrgsCSV ( ) <EOL> list_of_org_data_sorted_by_active_user_descending = [ ] <EOL> for org , active_user_count in active_user_counter . FilterAndSortMostCommon ( ) : <EOL> suspended_user_count = suspended_user_counter . data . get ( org , <NUM_LIT:0> ) <EOL> list_of_org_data_sorted_by_active_user_descending . append ( <EOL> ( active_user_count , suspended_user_count , org ) ) <EOL> org_unit_names = set ( active_user_counter . data . keys ( ) + <EOL> suspended_user_counter . data . keys ( ) ) <EOL> list_of_org_data_sorted_by_org_unit_name_alphabetically = [ ] <EOL> for org in sorted ( org_unit_names ) : <EOL> active_user_count = active_user_counter . data . get ( org , <NUM_LIT:0> ) <EOL> suspended_user_count = suspended_user_counter . data . get ( org , <NUM_LIT:0> ) <EOL> list_of_org_data_sorted_by_org_unit_name_alphabetically . append ( <EOL> ( org , active_user_count , suspended_user_count ) ) <EOL> return ( list_of_org_data_sorted_by_active_user_descending , <EOL> list_of_org_data_sorted_by_org_unit_name_alphabetically ) <EOL> def PrintOrgCounts ( orgs_by_active , orgs_by_ou ) : <EOL> """<STR_LIT>""" <EOL> print report_utils . BORDER <EOL> print '<STR_LIT:U+002C>' . join ( _REPORT_BY_ACTIVE_HEADER ) <EOL> for active , suspended , org in orgs_by_active : <EOL> print '<STR_LIT>' % ( active , suspended , org ) <EOL> print report_utils . BORDER <EOL> print '<STR_LIT:U+002C>' . join ( _REPORT_BY_ORG_HEADER ) <EOL> for org , active , suspended in orgs_by_ou : <EOL> print '<STR_LIT>' % ( org , active , suspended ) <EOL> print report_utils . BORDER <EOL> def WriteOrgCounts ( orgs_by_active , orgs_by_ou , flags ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> filename_path = FILE_MANAGER . WriteCSVFile ( _REPORT_ORGS_BY_ACTIVE_FILE_NAME , <EOL> orgs_by_active , <EOL> _REPORT_BY_ACTIVE_HEADER , <EOL> overwrite_ok = flags . force ) <EOL> print '<STR_LIT>' % ( <EOL> filename_path ) <EOL> filename_path = FILE_MANAGER . WriteCSVFile ( _REPORT_ORGS_BY_OU_FILE_NAME , <EOL> orgs_by_ou , <EOL> _REPORT_BY_ORG_HEADER , <EOL> overwrite_ok = flags . force ) <EOL> print '<STR_LIT>' % ( <EOL> filename_path ) <EOL> except admin_api_tool_errors . AdminAPIToolFileError : <EOL> log_utils . LogError ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def AddFlags ( arg_parser ) : <EOL> """<STR_LIT>""" <EOL> common_flags . DefineAppsDomainFlagWithDefault ( arg_parser ) <EOL> common_flags . DefineForceFlagWithDefaultFalse ( arg_parser ) <EOL> common_flags . DefineVerboseFlagWithDefaultFalse ( arg_parser ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> def main ( argv ) : <EOL> """<STR_LIT>""" <EOL> flags = common_flags . ParseFlags ( argv , '<STR_LIT>' , <EOL> AddFlags ) <EOL> if flags . csv : <EOL> FILE_MANAGER . ExitIfCannotOverwriteFile ( _REPORT_ORGS_BY_ACTIVE_FILE_NAME , <EOL> overwrite_ok = flags . force ) <EOL> FILE_MANAGER . ExitIfCannotOverwriteFile ( _REPORT_ORGS_BY_OU_FILE_NAME , <EOL> overwrite_ok = flags . force ) <EOL> GenerateUserReport ( flags ) <EOL> orgs_by_active , orgs_by_ou = SummarizeUserOrgReport ( ) <EOL> PrintOrgCounts ( orgs_by_active , orgs_by_ou ) <EOL> if flags . csv : <EOL> WriteOrgCounts ( orgs_by_active , orgs_by_ou , flags ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
