<s> import sys <EOL> import collections <EOL> import cgi <EOL> import os . path <EOL> import json <EOL> import clastic <EOL> from . . import context <EOL> def listmodules ( sort_col = <NUM_LIT:0> ) : <EOL> total , rows = _listmodules ( sort_col ) <EOL> trows = [ ] <EOL> for name , count , cum_count in rows : <EOL> href = '<STR_LIT>' . format ( cgi . escape ( name ) ) <EOL> trows . append ( '<STR_LIT>' . format ( <EOL> href , str ( count ) , str ( cum_count ) ) ) <EOL> return clastic . Response ( <EOL> _LIST_MODULES_TEMPLATE . format ( total , '<STR_LIT:\n>' . join ( trows ) ) , mimetype = "<STR_LIT>" ) <EOL> def listmodules_json ( ) : <EOL> total , rows = _listmodules ( <NUM_LIT:1> ) <EOL> data = json . dumps ( { "<STR_LIT>" : total , "<STR_LIT>" : rows } ) <EOL> data . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return clastic . Response ( data , mimetype = "<STR_LIT:application/json>" ) <EOL> def showmodule ( module_name ) : <EOL> lines = _showmodule ( module_name ) <EOL> rows = '<STR_LIT:\n>' . join ( <EOL> [ '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( * e ) for e in lines ] ) <EOL> return clastic . Response ( <EOL> _RENDER_MODULE_TEMPLATE . format ( rows ) , mimetype = "<STR_LIT>" ) <EOL> def showmodule_txt ( module_name ) : <EOL> lines = _showmodule ( module_name ) <EOL> body = '<STR_LIT>' . join ( [ '<STR_LIT>' . format ( e [ <NUM_LIT:1> ] , e [ <NUM_LIT:2> ] ) for e in lines ] ) <EOL> return clastic . Response ( body , mimetype = "<STR_LIT>" ) <EOL> def get_hotspots ( n = <NUM_LIT:1000> ) : <EOL> file_line_samples = { } <EOL> file_module_map = _make_file_module_name_map ( ) <EOL> ctx = context . get_context ( ) <EOL> total = <NUM_LIT:0> <EOL> if ctx . sampling : <EOL> for key , count in ctx . profiler . live_data_copy ( ) . items ( ) : <EOL> if key [ <NUM_LIT:2> ] is not None : <EOL> continue <EOL> fname = key [ <NUM_LIT:0> ] . co_filename <EOL> name = file_module_map . get ( fname , fname ) <EOL> file_line_samples [ ( name , key [ <NUM_LIT:1> ] ) ] = count <EOL> total += count <EOL> vals = sorted ( <EOL> file_line_samples . items ( ) , key = lambda v : v [ <NUM_LIT:1> ] , reverse = True ) [ : n ] <EOL> vals = [ ( line_info , "<STR_LIT>" . format ( samples * <NUM_LIT> / total ) ) <EOL> for line_info , samples in vals ] <EOL> return clastic . Response ( <EOL> '<STR_LIT:\n>' . join ( [ repr ( e ) for e in vals ] ) , mimetype = "<STR_LIT>" ) <EOL> def _make_file_module_name_map ( ) : <EOL> files_modules = { } <EOL> for name , mod in sys . modules . items ( ) : <EOL> if mod is None or not hasattr ( mod , '<STR_LIT>' ) : <EOL> continue <EOL> fname = mod . __file__ <EOL> if fname . endswith ( '<STR_LIT>' ) : <EOL> fname = fname [ : - <NUM_LIT:1> ] <EOL> files_modules [ fname ] = name <EOL> return files_modules <EOL> def _showmodule ( module_name ) : <EOL> module = sys . modules [ module_name ] <EOL> if not hasattr ( module , '<STR_LIT>' ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" . format ( module_name ) ) <EOL> if module . __file__ . endswith ( "<STR_LIT>" ) : <EOL> fname = module . __file__ <EOL> else : <EOL> if not module . __file__ . endswith ( "<STR_LIT>" ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( <EOL> module . __file__ , module_name ) ) <EOL> fname = module . __file__ [ : - <NUM_LIT:1> ] <EOL> if not os . path . exists ( fname ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( <EOL> fname , module_name ) ) <EOL> leaf_count , branch_count = _get_samples_by_line ( fname ) <EOL> lines = [ ] <EOL> with open ( fname ) as f : <EOL> for i , line in enumerate ( f ) : <EOL> lines . append ( ( <EOL> i + <NUM_LIT:1> , cgi . escape ( line ) , <EOL> leaf_count [ i + <NUM_LIT:1> ] , branch_count [ i + <NUM_LIT:1> ] ) ) <EOL> return lines <EOL> def _listmodules ( sort_col ) : <EOL> filename_leaf_samples , filename_branch_samples , total = _get_samples_by_file ( ) <EOL> file_module_map = _make_file_module_name_map ( ) <EOL> rows = [ ] <EOL> for fname , name in file_module_map . items ( ) : <EOL> count = - <NUM_LIT:1> <EOL> cum_count = - <NUM_LIT:1> <EOL> if filename_leaf_samples : <EOL> count = filename_leaf_samples [ fname ] <EOL> cum_count = filename_branch_samples [ fname ] <EOL> rows . append ( ( name , count , cum_count ) ) <EOL> rows . sort ( key = lambda e : e [ sort_col ] , reverse = sort_col != <NUM_LIT:0> ) <EOL> return total , rows <EOL> def _get_samples_by_file ( ) : <EOL> ctx = context . get_context ( ) <EOL> filename_leaf_samples = collections . defaultdict ( int ) <EOL> filename_branch_samples = collections . defaultdict ( int ) <EOL> total = <NUM_LIT:0> <EOL> if ctx . sampling : <EOL> leaf_samples = { } <EOL> branch_samples = { } <EOL> for key , count in ctx . profiler . live_data_copy ( ) . items ( ) : <EOL> if key [ <NUM_LIT:2> ] is None : <EOL> leaf_samples [ key ] = count <EOL> total += count <EOL> else : <EOL> branch_samples [ key ] = count <EOL> for key , count in leaf_samples . items ( ) : <EOL> filename_leaf_samples [ key [ <NUM_LIT:0> ] . co_filename ] += count <EOL> for key , count in branch_samples . items ( ) : <EOL> filename_branch_samples [ key [ <NUM_LIT:0> ] . co_filename ] += count <EOL> return filename_leaf_samples , filename_branch_samples , total <EOL> def _get_samples_by_line ( filename ) : <EOL> ctx = context . get_context ( ) <EOL> line_leaf_samples = collections . defaultdict ( int ) <EOL> line_branch_samples = collections . defaultdict ( int ) <EOL> if ctx . sampling : <EOL> for key , count in ctx . profiler . live_data_copy ( ) . items ( ) : <EOL> if key [ <NUM_LIT:0> ] . co_filename != filename : <EOL> continue <EOL> if key [ <NUM_LIT:2> ] is None : <EOL> line_leaf_samples [ key [ <NUM_LIT:1> ] ] = count <EOL> else : <EOL> line_branch_samples [ key [ <NUM_LIT:1> ] ] = count <EOL> return line_leaf_samples , line_branch_samples <EOL> _LIST_MODULES_TEMPLATE = '''<STR_LIT>''' <EOL> _RENDER_MODULE_TEMPLATE = '''<STR_LIT>''' </s>
