<s> """<STR_LIT>""" <EOL> import operator <EOL> import os <EOL> import webapp2 <EOL> from google . appengine . api import app_identity <EOL> from google . appengine . api import datastore_errors <EOL> from google . appengine . api import users <EOL> from google . appengine . ext import deferred <EOL> from google . appengine . ext . datastore_admin import backup_handler <EOL> from google . appengine . ext . datastore_admin import copy_handler <EOL> from google . appengine . ext . datastore_admin import delete_handler <EOL> from google . appengine . ext . datastore_admin import utils <EOL> from google . appengine . ext . db import stats <EOL> from google . appengine . ext . db import metadata <EOL> from google . appengine . ext . webapp import util <EOL> ENTITY_ACTIONS = { <EOL> '<STR_LIT>' : copy_handler . ConfirmCopyHandler . Render , <EOL> '<STR_LIT>' : delete_handler . ConfirmDeleteHandler . Render , <EOL> '<STR_LIT>' : backup_handler . ConfirmBackupHandler . Render , <EOL> } <EOL> BACKUP_ACTIONS = { <EOL> '<STR_LIT>' : backup_handler . ConfirmDeleteBackupHandler . Render , <EOL> '<STR_LIT>' : backup_handler . ConfirmRestoreFromBackupHandler . Render , <EOL> '<STR_LIT>' : backup_handler . BackupInformationHandler . Render , <EOL> } <EOL> PENDING_BACKUP_ACTIONS = { <EOL> '<STR_LIT>' : backup_handler . ConfirmAbortBackupHandler . Render , <EOL> '<STR_LIT>' : backup_handler . BackupInformationHandler . Render , <EOL> } <EOL> GET_ACTIONS = ENTITY_ACTIONS . copy ( ) <EOL> GET_ACTIONS . update ( BACKUP_ACTIONS ) <EOL> GET_ACTIONS . update ( PENDING_BACKUP_ACTIONS ) <EOL> GET_ACTIONS . update ( { '<STR_LIT>' : <EOL> backup_handler . ConfirmBackupImportHandler . Render } ) <EOL> def _GetDatastoreStats ( kinds_list , use_stats_kinds = False ) : <EOL> """<STR_LIT>""" <EOL> global_stat = stats . GlobalStat . all ( ) . fetch ( <NUM_LIT:1> ) <EOL> if not global_stat : <EOL> return _KindsListToTuple ( kinds_list ) <EOL> global_ts = global_stat [ <NUM_LIT:0> ] . timestamp <EOL> kind_stats = stats . KindStat . all ( ) . filter ( '<STR_LIT>' , global_ts ) . fetch ( <NUM_LIT:1000> ) <EOL> if not kind_stats : <EOL> return _KindsListToTuple ( kinds_list ) <EOL> results = { } <EOL> for kind_ent in kind_stats : <EOL> if ( not kind_ent . kind_name . startswith ( '<STR_LIT>' ) <EOL> and ( use_stats_kinds or kind_ent . kind_name in kinds_list ) <EOL> and kind_ent . count > <NUM_LIT:0> ) : <EOL> results [ kind_ent . kind_name ] = _PresentatableKindStats ( kind_ent ) <EOL> utils . CacheStats ( results . values ( ) ) <EOL> for kind_str in kinds_list or [ ] : <EOL> if kind_str not in results : <EOL> results [ kind_str ] = { '<STR_LIT>' : kind_str } <EOL> return ( global_ts , <EOL> sorted ( results . values ( ) , key = lambda x : x [ '<STR_LIT>' ] ) ) <EOL> def _KindsListToTuple ( kinds_list ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' , [ { '<STR_LIT>' : kind } for kind in sorted ( kinds_list ) ] <EOL> def _PresentatableKindStats ( kind_ent ) : <EOL> """<STR_LIT>""" <EOL> count = kind_ent . count <EOL> entity_bytes = kind_ent . entity_bytes <EOL> total_bytes = kind_ent . bytes <EOL> average_bytes = entity_bytes / count <EOL> return { '<STR_LIT>' : kind_ent . kind_name , <EOL> '<STR_LIT:count>' : utils . FormatThousands ( kind_ent . count ) , <EOL> '<STR_LIT>' : utils . GetPrettyBytes ( entity_bytes ) , <EOL> '<STR_LIT>' : entity_bytes , <EOL> '<STR_LIT>' : utils . GetPrettyBytes ( total_bytes ) , <EOL> '<STR_LIT>' : total_bytes , <EOL> '<STR_LIT>' : utils . GetPrettyBytes ( average_bytes ) , <EOL> } <EOL> class RouteByActionHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def ListActions ( self , error = None ) : <EOL> """<STR_LIT>""" <EOL> use_stats_kinds = False <EOL> kinds = [ ] <EOL> try : <EOL> kinds = self . GetKinds ( ) <EOL> if not kinds : <EOL> use_stats_kinds = True <EOL> except datastore_errors . Error : <EOL> use_stats_kinds = True <EOL> last_stats_update , kind_stats = _GetDatastoreStats ( <EOL> kinds , use_stats_kinds = use_stats_kinds ) <EOL> template_params = { <EOL> '<STR_LIT>' : kind_stats , <EOL> '<STR_LIT>' : self . request . path + '<STR_LIT:?>' + self . request . query_string , <EOL> '<STR_LIT>' : last_stats_update , <EOL> '<STR_LIT>' : self . request . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : app_identity . get_application_id ( ) , <EOL> '<STR_LIT>' : self . request . get ( '<STR_LIT>' , None ) is not None , <EOL> '<STR_LIT>' : self . request . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : sorted ( ENTITY_ACTIONS . keys ( ) ) , <EOL> '<STR_LIT>' : sorted ( BACKUP_ACTIONS . keys ( ) ) , <EOL> '<STR_LIT>' : sorted ( PENDING_BACKUP_ACTIONS . keys ( ) ) , <EOL> '<STR_LIT:error>' : error , <EOL> '<STR_LIT>' : self . GetOperations ( active = False ) , <EOL> '<STR_LIT>' : self . GetOperations ( active = True ) , <EOL> '<STR_LIT>' : self . GetPendingBackups ( ) , <EOL> '<STR_LIT>' : self . GetBackups ( ) , <EOL> '<STR_LIT>' : utils . config . MAPREDUCE_PATH + '<STR_LIT>' <EOL> } <EOL> utils . RenderToResponse ( self , '<STR_LIT>' , template_params ) <EOL> def RouteAction ( self , action_dict ) : <EOL> action = self . request . get ( '<STR_LIT:action>' ) <EOL> if not action : <EOL> self . ListActions ( error = self . request . get ( '<STR_LIT:error>' , None ) ) <EOL> elif action not in action_dict : <EOL> error = '<STR_LIT>' % action <EOL> self . ListActions ( error = error ) <EOL> else : <EOL> action_dict [ action ] ( self ) <EOL> def get ( self ) : <EOL> self . RouteAction ( GET_ACTIONS ) <EOL> def post ( self ) : <EOL> self . RouteAction ( GET_ACTIONS ) <EOL> def GetKinds ( self , all_ns = True ) : <EOL> """<STR_LIT>""" <EOL> if all_ns : <EOL> result = self . GetKindsForAllNamespaces ( ) <EOL> else : <EOL> result = self . GetKindsForCurrentNamespace ( ) <EOL> return result <EOL> def GetKindsForAllNamespaces ( self ) : <EOL> """<STR_LIT>""" <EOL> namespace_list = [ ns . namespace_name <EOL> for ns in metadata . Namespace . all ( ) . run ( limit = <NUM_LIT> ) ] <EOL> kind_itr_list = [ metadata . Kind . all ( namespace = ns ) . run ( limit = <NUM_LIT> , <EOL> batch_size = <NUM_LIT> ) <EOL> for ns in namespace_list ] <EOL> kind_name_set = set ( ) <EOL> for kind_itr in kind_itr_list : <EOL> for kind in kind_itr : <EOL> kind_name = kind . kind_name <EOL> if utils . IsKindNameVisible ( kind_name ) : <EOL> kind_name_set . add ( kind . kind_name ) <EOL> kind_name_list = sorted ( kind_name_set ) <EOL> return kind_name_list <EOL> def GetKindsForCurrentNamespace ( self ) : <EOL> """<STR_LIT>""" <EOL> kinds = metadata . Kind . all ( ) . order ( '<STR_LIT>' ) . fetch ( <NUM_LIT> ) <EOL> kind_names = [ ] <EOL> for kind in kinds : <EOL> kind_name = kind . kind_name <EOL> if utils . IsKindNameVisible ( kind_name ) : <EOL> kind_names . append ( kind_name ) <EOL> return kind_names <EOL> def GetOperations ( self , active = False , limit = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> query = utils . DatastoreAdminOperation . all ( ) <EOL> if active : <EOL> query . filter ( '<STR_LIT>' , utils . DatastoreAdminOperation . STATUS_ACTIVE ) <EOL> else : <EOL> query . filter ( '<STR_LIT>' , [ <EOL> utils . DatastoreAdminOperation . STATUS_COMPLETED , <EOL> utils . DatastoreAdminOperation . STATUS_FAILED , <EOL> utils . DatastoreAdminOperation . STATUS_ABORTED ] ) <EOL> operations = query . fetch ( max ( <NUM_LIT> , limit ) if limit else <NUM_LIT:1000> ) <EOL> operations = sorted ( operations , key = operator . attrgetter ( '<STR_LIT>' ) , <EOL> reverse = True ) <EOL> return operations [ : limit ] <EOL> def GetBackups ( self , limit = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> query = backup_handler . BackupInformation . all ( ) <EOL> query . filter ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> backups = query . fetch ( max ( <NUM_LIT> , limit ) if limit else <NUM_LIT:1000> ) <EOL> backups = sorted ( backups , key = operator . attrgetter ( '<STR_LIT>' ) , <EOL> reverse = True ) <EOL> return backups [ : limit ] <EOL> def GetPendingBackups ( self , limit = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> query = backup_handler . BackupInformation . all ( ) <EOL> query . filter ( '<STR_LIT>' , None ) <EOL> backups = query . fetch ( max ( <NUM_LIT> , limit ) if limit else <NUM_LIT:1000> ) <EOL> backups = sorted ( backups , key = operator . attrgetter ( '<STR_LIT>' ) , <EOL> reverse = True ) <EOL> return backups [ : limit ] <EOL> class StaticResourceHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> _BASE_FILE_PATH = os . path . dirname ( __file__ ) <EOL> _RESOURCE_MAP = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def get ( self ) : <EOL> relative_path = self . request . path . split ( utils . config . BASE_PATH + '<STR_LIT:/>' ) [ <NUM_LIT:1> ] <EOL> if relative_path not in self . _RESOURCE_MAP : <EOL> self . response . set_status ( <NUM_LIT> ) <EOL> self . response . out . write ( '<STR_LIT>' ) <EOL> return <EOL> path = os . path . join ( self . _BASE_FILE_PATH , relative_path ) <EOL> self . response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = self . _RESOURCE_MAP [ relative_path ] <EOL> if relative_path == '<STR_LIT>' : <EOL> self . response . out . write ( <EOL> open ( path ) . read ( ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> self . response . out . write ( open ( path ) . read ( ) ) <EOL> class LoginRequiredHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> target = self . request . get ( '<STR_LIT>' ) <EOL> if not target : <EOL> self . error ( <NUM_LIT> ) <EOL> return <EOL> login_url = users . create_login_url ( target ) <EOL> self . redirect ( login_url ) <EOL> def CreateApplication ( ) : <EOL> """<STR_LIT>""" <EOL> return webapp2 . WSGIApplication ( [ <EOL> ( r'<STR_LIT>' % ( utils . config . BASE_PATH , <EOL> delete_handler . ConfirmDeleteHandler . SUFFIX ) , <EOL> delete_handler . ConfirmDeleteHandler ) , <EOL> ( r'<STR_LIT>' % ( utils . config . BASE_PATH , <EOL> delete_handler . DoDeleteHandler . SUFFIX ) , <EOL> delete_handler . DoDeleteHandler ) , <EOL> ( r'<STR_LIT>' % ( utils . config . BASE_PATH , <EOL> utils . MapreduceDoneHandler . SUFFIX ) , <EOL> utils . MapreduceDoneHandler ) , <EOL> ( utils . config . DEFERRED_PATH , deferred . TaskHandler ) ] <EOL> + copy_handler . handlers_list ( utils . config . BASE_PATH ) <EOL> + backup_handler . handlers_list ( utils . config . BASE_PATH ) <EOL> + [ ( r'<STR_LIT>' % utils . config . BASE_PATH , StaticResourceHandler ) , <EOL> ( r'<STR_LIT>' , LoginRequiredHandler ) , <EOL> ( r'<STR_LIT>' , RouteByActionHandler ) ] ) <EOL> APP = CreateApplication ( ) </s>
