<s> import gc <EOL> import os <EOL> import sys <EOL> import datetime <EOL> import traceback <EOL> import collections <EOL> from collections import defaultdict <EOL> import clastic <EOL> from clastic import Response <EOL> from clastic . render import BasicRender , TabularRender <EOL> from clastic . static import StaticFileRoute <EOL> from support import ll <EOL> from support import context <EOL> from support import cache <EOL> from support . meta_service import stats <EOL> from support . meta_service import codeview <EOL> CUR_PATH = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> SUPPORT_PATH = os . path . dirname ( CUR_PATH ) <EOL> ASSETS_PATH = SUPPORT_PATH + '<STR_LIT>' <EOL> FAVICON_PATH = ASSETS_PATH + '<STR_LIT>' <EOL> def create_meta_app ( additional_routes = None ) : <EOL> render = BasicRender ( tabular_render = TabularRender ( table_type = MetaTable ) ) <EOL> ma = clastic . meta . MetaApplication ( page_title = '<STR_LIT>' ) <EOL> routes = [ <EOL> ( '<STR_LIT:/>' , ma ) , <EOL> ( '<STR_LIT>' , get_warnings , render ) , <EOL> ( '<STR_LIT>' , get_warnings , render ) , <EOL> ( '<STR_LIT>' , get_pytypes , render ) , <EOL> ( '<STR_LIT>' , get_pytypes_len ) , <EOL> ( '<STR_LIT>' , dump_id , render ) , <EOL> ( '<STR_LIT>' , get_context_dict , render ) , <EOL> ( '<STR_LIT>' , get_thread_stacks , render ) , <EOL> ( '<STR_LIT>' , get_psutil_data , render ) , <EOL> ( '<STR_LIT>' , get_logs , render ) , <EOL> ( '<STR_LIT>' , get_web_logs , render ) , <EOL> ( '<STR_LIT>' , set_level , render ) , <EOL> ( '<STR_LIT>' , get_greenlets , render ) , <EOL> ( '<STR_LIT>' , get_connections , render ) , <EOL> ( '<STR_LIT>' , reset_stats , render ) , <EOL> ( '<STR_LIT>' , stats . get_stats , render ) , <EOL> ( '<STR_LIT>' , stats . get_stats , render ) , <EOL> ( '<STR_LIT>' , stats . statgraphs ) , <EOL> ( '<STR_LIT>' , stats . statgraphs ) , <EOL> ( '<STR_LIT>' , get_recent_tcp , render ) , <EOL> ( '<STR_LIT>' , get_recent_tcp , render ) , <EOL> ( '<STR_LIT>' , get_recent , render ) , <EOL> ( '<STR_LIT>' , get_recent , render ) , <EOL> ( '<STR_LIT>' , get_recent , render ) , <EOL> ( '<STR_LIT>' , get_sampro_data , render ) , <EOL> ( '<STR_LIT>' , get_environment , render ) , <EOL> ( '<STR_LIT>' , codeview . listmodules ) , <EOL> ( '<STR_LIT>' , codeview . listmodules ) , <EOL> ( '<STR_LIT>' , codeview . listmodules_json ) , <EOL> ( '<STR_LIT>' , codeview . showmodule ) , <EOL> ( '<STR_LIT>' , codeview . showmodule_txt ) , <EOL> ( '<STR_LIT>' , codeview . get_hotspots ) , <EOL> ( '<STR_LIT>' , get_connection_mgr , render ) , <EOL> ( '<STR_LIT>' , get_fd_info , rt_json_render_basic ) , <EOL> ( '<STR_LIT>' , get_one_fd_info , rt_json_render_basic ) , <EOL> ( '<STR_LIT>' , view_obj ) , <EOL> ( '<STR_LIT>' , view_obj ) , <EOL> StaticFileRoute ( '<STR_LIT>' , FAVICON_PATH ) <EOL> ] + ( additional_routes or [ ] ) <EOL> app = clastic . Application ( routes ) <EOL> return app <EOL> def rt_json_render_basic ( request , context , _route ) : <EOL> import json <EOL> rjd = clastic . render_json_dev <EOL> new_context = json . loads ( rjd . json_encoder . encode ( context ) ) <EOL> return clastic . render_basic ( request = request , <EOL> context = new_context , <EOL> _route = _route ) <EOL> class MetaTable ( clastic . render . Table ) : <EOL> def get_cell_html ( self , value ) : <EOL> inner = super ( MetaTable , self ) . get_cell_html ( value ) <EOL> if not gc . is_tracked ( value ) : <EOL> return inner <EOL> return '<STR_LIT>' . format ( id ( value ) , inner ) <EOL> def get_config_dict ( ) : <EOL> '<STR_LIT>' <EOL> ctx = context . get_context ( ) <EOL> data = [ ] <EOL> keys_handled_directly = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> for k in ctx . __dict__ : <EOL> if k not in keys_handled_directly : <EOL> data . append ( [ k , getattr ( ctx , k ) ] ) <EOL> return dict ( [ ( e [ <NUM_LIT:0> ] , e [ <NUM_LIT:1> : ] ) for e in data ] ) <EOL> def get_environment ( ) : <EOL> return dict ( ( k , os . environ [ k ] ) for k in os . environ if "<STR_LIT>" not in k ) <EOL> def get_context_dict ( ) : <EOL> data = get_config_dict ( ) <EOL> return data <EOL> def get_thread_stacks ( ) : <EOL> return dict ( [ ( k , traceback . format_stack ( v ) ) <EOL> for k , v in sys . _current_frames ( ) . items ( ) ] ) <EOL> def get_connections ( ) : <EOL> ctx = context . get_context ( ) <EOL> ret = { } <EOL> for model in ctx . connection_mgr . server_models . values ( ) : <EOL> ret [ model . address ] = { <EOL> "<STR_LIT>" : datetime . datetime . fromtimestamp ( <EOL> int ( model . last_error ) ) . strftime ( '<STR_LIT>' ) , <EOL> "<STR_LIT>" : [ repr ( s ) for s in model . active_connections . values ( ) ] <EOL> } <EOL> return ret <EOL> def get_pytypes ( ) : <EOL> answer = defaultdict ( lambda : <NUM_LIT:0> ) <EOL> try : <EOL> for ob in gc . get_objects ( ) : <EOL> answer [ repr ( type ( ob ) ) ] += <NUM_LIT:1> <EOL> except : <EOL> pass <EOL> return answer <EOL> def get_pytypes_len ( ) : <EOL> return Response ( get_pytypes_len_gen ( ) , mimetype = "<STR_LIT:application/json>" ) <EOL> def get_pytypes_len_gen ( ) : <EOL> try : <EOL> yield "<STR_LIT>" <EOL> def get_total_size ( obj , n = <NUM_LIT:0> ) : <EOL> if n > <NUM_LIT:10> : <EOL> return <NUM_LIT:0> <EOL> obj_len = <NUM_LIT:0> <EOL> try : <EOL> if callable ( getattr ( obj , '<STR_LIT>' , False ) ) : <EOL> obj_len += int ( obj . __len__ ( ) ) <EOL> except : <EOL> pass <EOL> for a in gc . get_referents ( obj ) : <EOL> obj_len += get_total_size ( a , n + <NUM_LIT:1> ) <EOL> return obj_len <EOL> for ob in gc . get_objects ( ) : <EOL> if getattr ( ob , '<STR_LIT>' , False ) : <EOL> yield "<STR_LIT>" + repr ( type ( ob ) ) + "<STR_LIT::>" + str ( id ( ob ) ) + "<STR_LIT>" + str ( len ( ob ) ) + "<STR_LIT:U+002CU+0020>" + str ( get_total_size ( ob ) ) + "<STR_LIT>" <EOL> except Exception as e : <EOL> yield "<STR_LIT>" + repr ( e ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) + "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> def dump_id ( in_id ) : <EOL> answer = { } <EOL> try : <EOL> def get_total_size ( obj , n = <NUM_LIT:0> ) : <EOL> if n > <NUM_LIT:10> : <EOL> return <NUM_LIT:0> <EOL> obj_len = <NUM_LIT:0> <EOL> obj_len += getattr ( ob , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> for a in gc . get_referents ( obj ) : <EOL> obj_len += get_total_size ( a , n + <NUM_LIT:1> ) <EOL> return obj_len <EOL> for ob in gc . get_objects ( ) : <EOL> if id ( ob ) == int ( in_id ) : <EOL> answer [ '<STR_LIT>' ] = ob <EOL> answer [ '<STR_LIT>' ] = repr ( ob ) <EOL> answer [ '<STR_LIT>' ] = dir ( ob ) <EOL> answer [ '<STR_LIT>' ] = get_total_size ( ob ) <EOL> except Exception as e : <EOL> answer [ '<STR_LIT:error>' ] = repr ( e ) <EOL> return answer <EOL> def get_greenlets ( ) : <EOL> try : <EOL> import traceback <EOL> from greenlet import greenlet <EOL> answer = [ ] <EOL> for ob in gc . get_objects ( ) : <EOL> if not isinstance ( ob , greenlet ) : <EOL> continue <EOL> if not ob : <EOL> continue <EOL> answer . append ( traceback . format_stack ( ob . gr_frame ) ) <EOL> except Exception : <EOL> from gevent . hub import _get_hub <EOL> answer . append ( [ _get_hub ( ) . loop . activecnt ] ) <EOL> return answer <EOL> def get_psutil_data ( ) : <EOL> answer = { } <EOL> try : <EOL> import psutil <EOL> import os <EOL> answer = psutil . Process ( os . getpid ( ) ) . as_dict ( ) <EOL> except Exception as e : <EOL> answer = { '<STR_LIT:error>' : repr ( e ) } <EOL> return answer <EOL> def get_one_fd_info ( fd ) : <EOL> import traceback <EOL> all_fd_info = get_fd_info ( ) <EOL> fd_info = all_fd_info [ fd ] <EOL> fd_info [ '<STR_LIT>' ] = [ ] <EOL> for obj in fd_info [ '<STR_LIT>' ] : <EOL> frames = get_frames_local_to ( obj ) <EOL> for name , frame in frames : <EOL> fd_info [ '<STR_LIT>' ] . append ( <EOL> ( obj , name , traceback . format_stack ( frame ) ) ) <EOL> return fd_info <EOL> def get_fd_info ( ) : <EOL> '''<STR_LIT>''' <EOL> import psutil <EOL> fd_info = collections . defaultdict ( <EOL> lambda : { '<STR_LIT>' : [ ] , '<STR_LIT>' : { } , '<STR_LIT>' : [ ] , '<STR_LIT>' : { } } ) <EOL> for obj in gc . get_objects ( ) : <EOL> try : <EOL> fd = None <EOL> if hasattr ( obj , '<STR_LIT>' ) and callable ( obj . fileno ) : <EOL> fd = obj . fileno ( ) <EOL> except : <EOL> pass <EOL> if isinstance ( fd , ( int , long ) ) : <EOL> fd_info [ fd ] [ '<STR_LIT>' ] . append ( obj ) <EOL> ctx = context . get_context ( ) <EOL> socks = [ ] <EOL> if ctx . connection_mgr : <EOL> for model in ctx . connection_mgr . server_models . values ( ) : <EOL> socks . extend ( model . active_connections ) <EOL> if ctx . client_sockets : <EOL> socks . extend ( ctx . client_sockets ) <EOL> if ctx . server_group : <EOL> socks . extend ( ctx . server_group . socks . values ( ) ) <EOL> for sock in socks : <EOL> fd_info [ sock . fileno ( ) ] [ '<STR_LIT>' ] . append ( sock ) <EOL> process = psutil . Process ( ) <EOL> for f in process . get_open_files ( ) : <EOL> if f . fd == - <NUM_LIT:1> : <EOL> continue <EOL> fd_info [ f . fd ] [ '<STR_LIT>' ] [ '<STR_LIT:path>' ] = f . path <EOL> for conn in process . get_connections ( kind = '<STR_LIT:all>' ) : <EOL> if conn . fd == - <NUM_LIT:1> : <EOL> continue <EOL> fd_info [ conn . fd ] [ '<STR_LIT>' ] . update ( vars ( conn ) ) <EOL> del fd_info [ conn . fd ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> proc_path = "<STR_LIT>" + str ( os . getpid ( ) ) <EOL> if not os . path . exists ( proc_path ) : <EOL> return dict ( fd_info ) <EOL> for fd in os . listdir ( proc_path + "<STR_LIT>" ) : <EOL> try : <EOL> inode = os . readlink ( proc_path + "<STR_LIT>" + fd ) <EOL> except OSError : <EOL> continue <EOL> else : <EOL> fd = int ( fd ) <EOL> fd_info [ fd ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = inode <EOL> return dict ( fd_info ) <EOL> def get_frames_local_to ( obj ) : <EOL> '''<STR_LIT>''' <EOL> import types <EOL> refs = gc . get_referrers ( obj ) <EOL> frame_refs = [ ] <EOL> for f in refs : <EOL> if not isinstance ( f , types . FrameType ) : <EOL> continue <EOL> if f . f_code . co_filename == __file__ : <EOL> continue <EOL> for k , v in f . f_locals . items ( ) : <EOL> if v is obj : <EOL> frame_refs . append ( ( k , f ) ) <EOL> return frame_refs <EOL> def get_connection_mgr ( ) : <EOL> connection_mgr = context . get_context ( ) . connection_mgr <EOL> server_models , sockpools = { } , { } <EOL> for k in connection_mgr . server_models : <EOL> server_model = connection_mgr . server_models [ k ] <EOL> server_models [ repr ( k ) ] = { <EOL> "<STR_LIT:info>" : repr ( server_model ) , <EOL> "<STR_LIT>" : [ s . fileno ( ) for s in server_model . active_connections . keys ( ) ] <EOL> } <EOL> for prot in connection_mgr . sockpools : <EOL> for sock_type in connection_mgr . sockpools [ prot ] : <EOL> sockpool = connection_mgr . sockpools [ prot ] [ sock_type ] <EOL> sockpools [ repr ( ( prot , sock_type ) ) ] = { <EOL> "<STR_LIT:info>" : repr ( sockpool ) , <EOL> "<STR_LIT>" : map ( repr , sockpool . free_socks_by_addr . keys ( ) ) <EOL> } <EOL> return { '<STR_LIT>' : server_models , '<STR_LIT>' : sockpools } <EOL> def get_logs ( ) : <EOL> return ll . log_msgs <EOL> def get_web_logs ( ) : <EOL> sgrp = context . get_context ( ) . server_group <EOL> if not sgrp : <EOL> raise EnvironmentError ( "<STR_LIT>" ) <EOL> result = [ ] <EOL> for serv in sgrp . servers : <EOL> if hasattr ( serv , "<STR_LIT>" ) : <EOL> result . extend ( serv . log . msgs ) <EOL> return result <EOL> def get_sampro_data ( ) : <EOL> processed = defaultdict ( int ) <EOL> profiler = context . get_context ( ) . profiler <EOL> if profiler is None : <EOL> return "<STR_LIT>" <EOL> for k , v in context . get_context ( ) . profiler . live_data_copy ( ) . iteritems ( ) : <EOL> code , lineno , parentcode = k <EOL> if parentcode is None : <EOL> key = ( code . co_name + "<STR_LIT:(>" + code . co_filename + <EOL> "<STR_LIT:U+002CU+0020>" + str ( code . co_firstlineno ) + "<STR_LIT:)>" ) <EOL> processed [ key ] += v <EOL> processed = reversed ( sorted ( [ ( v , k ) for k , v in processed . items ( ) ] ) ) <EOL> return dict ( processed ) <EOL> def get_recent ( thing1 = None , thing2 = None ) : <EOL> ctx = context . get_context ( ) <EOL> if thing1 is None : <EOL> return [ k for k in ctx . recent . keys ( ) ] <EOL> if thing2 is None : <EOL> if isinstance ( ctx . recent [ thing1 ] , ( dict , cache . Cache ) ) : <EOL> return dict ( [ ( repr ( k ) , list ( v ) ) <EOL> for k , v in ctx . recent [ thing1 ] . items ( ) ] ) <EOL> else : <EOL> return list ( ctx . recent [ thing1 ] ) <EOL> else : <EOL> if isinstance ( ctx . recent [ thing1 ] , ( dict , cache . Cache ) ) : <EOL> return dict ( [ ( repr ( k ) , list ( v ) ) for k , v in <EOL> ctx . recent [ thing1 ] . items ( ) if thing2 in str ( k ) ] ) <EOL> else : <EOL> return '<STR_LIT>' + thing1 + '<STR_LIT>' <EOL> def get_recent_tcp ( thing2 = None ) : <EOL> return get_recent ( thing1 = '<STR_LIT>' , thing2 = thing2 ) <EOL> def get_warnings ( path = None ) : <EOL> warns = context . get_context ( ) . get_warnings ( ) <EOL> if path : <EOL> path_segs = path . split ( '<STR_LIT:.>' ) <EOL> for seg in path_segs : <EOL> if seg in warns : <EOL> warns = warns [ seg ] <EOL> else : <EOL> return "<STR_LIT>" <EOL> warns = _dict_map ( warns , _transform ) <EOL> return warns <EOL> def _transform ( v ) : <EOL> try : <EOL> return dict ( v ) <EOL> except : <EOL> pass <EOL> try : <EOL> return list ( v ) <EOL> except : <EOL> pass <EOL> return repr ( v ) <EOL> def _dict_map ( data , transform , recurse = lambda k , v : isinstance ( v , dict ) ) : <EOL> transformed = { } <EOL> stack = [ ] <EOL> stack . append ( ( data , transformed ) ) <EOL> while stack : <EOL> src , dst = stack . pop ( ) <EOL> for k , v in src . items ( ) : <EOL> if recurse ( k , v ) : <EOL> child_val = { } <EOL> stack . append ( ( v , child_val ) ) <EOL> else : <EOL> child_val = transform ( v ) <EOL> dst [ k ] = child_val <EOL> return transformed <EOL> def set_level ( level ) : <EOL> ll . set_log_level ( int ( level ) ) <EOL> return ll . get_log_level ( ) <EOL> def reset_stats ( ) : <EOL> import faststat <EOL> context . get_context ( ) . stats = defaultdict ( faststat . Stats ) <EOL> return "<STR_LIT:OK>" <EOL> def view_obj ( request , obj_id = None ) : <EOL> import obj_browser <EOL> if obj_id is None : <EOL> return clastic . redirect ( <EOL> request . path + '<STR_LIT>' . format ( id ( context . get_context ( ) ) ) ) <EOL> for obj in gc . get_objects ( ) : <EOL> if id ( obj ) == obj_id : <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" . format ( obj_id ) ) <EOL> path , _ , _ = request . path . rpartition ( '<STR_LIT:/>' ) <EOL> return clastic . Response ( <EOL> obj_browser . render_html ( <EOL> obj , lambda id : path + '<STR_LIT>' . format ( id ) ) , <EOL> mimetype = "<STR_LIT>" ) </s>
