00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 from SCons.Script import Builder,Action,ARGUMENTS
00022 from slimproj_core.builders.BuilderFunctions import DEFAULT_PROFILES
00023
00024 class Profiler( object ):
00025
00026 def __init__(self,name):
00027 self.name = name
00028 self.act = None
00029 self._built = False
00030
00031 def __str__(self):
00032 return "Profiler('%s.hotshot')" %self.name
00033
00034 def set_action(self, act):
00035
00036 self.act = act
00037
00038 def set_env(self,target,source,env):
00039
00040 self.target = target[:]
00041 self.source = source[:]
00042 self.env = env.Clone()
00043
00044
00045 def profile(self):
00046
00047
00048 if self._built:
00049
00050 return
00051 action = Action( self.build, "Profile '%s' [%s]" %( self.act.__name__, self.name) )
00052 builder = Builder( action=action,
00053 emitter=self.emitter,
00054 suffix='.hotshot' )
00055
00056
00057 builder( self.env , self.name )
00058
00059 self._built = True
00060
00061 def emitter(self,target,source,env):
00062
00063
00064 DEFAULT_PROFILES.append( target[0] )
00065
00066 source = self.source
00067
00068
00069 env.Alias( 'profile', target )
00070 return target,source
00071
00072 def build(self,target,source,env):
00073
00074 import hotshot
00075
00076 env['runtype'] = 'dryrun'
00077 env['verbose'] = ARGUMENTS.get('verbose',0)
00078 env['logfile'] = ARGUMENTS.get('log',None)
00079 env['callbacks'] = []
00080
00081 prof = hotshot.Profile( str( target[0] ) )
00082
00083 prof.runcall( self.act, self.target, source, env )
00084 prof.close()
00085