00001 """
00002 Helper functions to create SCons builders out of ???
00003 """
00004 from slimproj_core.builders.CreateBuilders import CreateSLIMpyBuilder
00005 from SCons.Script.SConscript import SConsEnvironment
00006 from slimproj_core.builders.my_de_call import DefaultBuilderCall
00007 import inspect
00008
00009 class slim_builder( object ):
00010 """
00011 @ingroup sconsint
00012 This function is for SCons integration with SLIMpy inline inside the SConstruct file.
00013 This is a python decorator function.
00014 \param func must be a function with signature func( target, source, env )
00015
00016 \par Example:
00017 @code
00018 from slimproj import *
00019
00020 @slim_builder
00021 def MyBuilder( target, source, env ):
00022 parse_env( env )
00023
00024 ...function body ...
00025
00026 Execute( )
00027
00028 MyBuilder( 'foo.rsf', 'bar.rsf' )
00029 @endcode
00030 \sa \ref slimpy_base.Environment.InstanceManager.InstanceManager.Execute "Execute"
00031 \sa \ref slimpy_base.setup.ParseEnv.parse_env "parse_env"
00032 \sa \ref slimpy_base.utils.slim_decorators.depends_on "depends_on"
00033 \sa \ref slimproj_core.builders.funtion_decorators.slim_builder_simple "slim_builder_simple"
00034 """
00035 def __new__( cls , func ):
00036
00037 if isinstance(func, SConsEnvironment ):
00038 return object.__new__( cls, func )
00039 else:
00040 return cls.create(func)
00041
00042 def __init__( self, env ):
00043 self.env = env
00044
00045 def __call__( self, func ):
00046 self.create( func, self.env )
00047
00048 @classmethod
00049 def create(cls, func , env=None ):
00050 new_slimpy_builder = CreateSLIMpyBuilder( func.__name__ , func )
00051
00052 if env:
00053 env['BUILDERS'][func.__name__] = new_slimpy_builder
00054
00055 return DefaultBuilderCall( new_slimpy_builder )
00056
00057
00058
00059 hassuffix = lambda src: hasattr(src, 'suffix') and (src.suffix == '.rsf' )
00060 class slim_builder_simple( object ):
00061 """
00062 @ingroup sconsint
00063 This function is for SCons integration with SLIMpy inline inside the SConstruct file.
00064 This is a python decorator function.
00065 \param func must be a function with signature func( vectors, [keywords=...] )
00066 \post the function slim_builder_simple is similar to slim_builder except that the preamble is handled
00067 automatically.
00068 - all of the sources in the SCons call are converted to vectors
00069 - the keyword arguments are tracked as dependencies
00070 - the return value of func may be a vector or a sequence of vectors that are automatically
00071 set as SLIMpy targets
00072
00073 \return a Scons Default Environment Call
00074
00075 \par Example:
00076 @code
00077 from slimproj import *
00078
00079 @slim_builder_simple
00080 def MyBuilder( vectors, [keyword arguments] ):
00081
00082 ...function body ...
00083
00084 return results
00085
00086 MyBuilder( 'foo.rsf', 'bar.rsf' )
00087 @endcode
00088
00089 \sa \ref slimpy_base.utils.slim_decorators.depends_on "depends_on"
00090 \sa \ref slimproj_core.builders.funtion_decorators.slim_builder "slim_builder"
00091 """
00092 def __new__( cls , func ):
00093
00094 if isinstance(func, SConsEnvironment ):
00095 return object.__new__( cls, func )
00096 else:
00097 return cls.create(func)
00098
00099 def __init__( self, env ):
00100 self.env = env
00101
00102 def __call__( self, func ):
00103 self.create( func, self.env )
00104
00105 @classmethod
00106 def create(cls, func , env=None ):
00107
00108 args, varargs, varkw, defaults = inspect.getargspec(func)
00109
00110 if defaults is None:
00111 kwargs = []
00112 else:
00113 kwargs = args[-len(defaults):]
00114
00115 newbuild = new_builder( kwargs,varkw, func )
00116 new_slimpy_builder = CreateSLIMpyBuilder( func.__name__ , newbuild, depends_on=kwargs )
00117
00118 if env:
00119 env['BUILDERS'][func.__name__] = new_slimpy_builder
00120
00121 return DefaultBuilderCall( new_slimpy_builder )
00122
00123 class new_builder( object ):
00124
00125 def __init__(self, kwargs,varkw, func ):
00126 self.kwargs =kwargs
00127 self.varkw = varkw
00128 self.func = func
00129
00130 def __call__( self, target, source, env ):
00131
00132 from SLIMpy.setup import parse_env
00133 from SLIMpy import vector,Execute
00134
00135 env = parse_env( env )
00136
00137 kwdict = {}
00138 for karg in self.kwargs:
00139 if karg in env:
00140 kwdict[karg] = env[karg]
00141
00142 vectors = [vector(src) for src in source if hassuffix(src)]
00143
00144 if self.varkw:
00145 result = self.func( vectors, **env )
00146 else:
00147 result = self.func( vectors, **kwdict )
00148
00149 if isinstance(result, (list,tuple) ):
00150 if not len(result) <= len(target):
00151 raise Exception("The number of results returned from function '%s' "
00152 "must be less than or equal to the number of SCons targets given:\n"
00153 "results = %s\n"
00154 "targets = %s" %( self.func.__name__, result, [str(tgt) for tgt in target]) )
00155
00156 for i,res in enumerate(result):
00157 res.setName( target[i] )
00158 elif result is None:
00159 pass
00160 else:
00161 result.setName( target[0] )
00162
00163 Execute( )
00164
00165 return
00166