[Scons-users] extending build environments (the OO-way)

Gary Granger granger at ucar.edu
Thu Sep 21 11:09:07 EDT 2017


Here's something about the current solution to this we use, but I don't
know if it would be considered a "best practice".  You're right that the
methods on the Environment class which are builders are not treated like
regular methods.  So to "wrap" a builder method, you can wrap the actual
builder instance and replace that instance with your own, something like
this:

from SCons.Builder import BuilderBase
from SCons.Builder import _null

class MyProgramBuilder(BuilderBase):
    def __init__(self, builder):
        BuilderBase.__init__(self, 
                             action=builder.action,
                             emitter=builder.emitter,
                             prefix=builder.prefix,
                             suffix=builder.suffix,
                             src_suffix=builder.src_suffix,
                             src_builder=builder.src_builder)

    def __call__(self, env, target=None, source=None, chdir=_null, **kw):
        ret = BuilderBase.__call__(self, env, target, source, chdir, **kw)
	print("Registered program %s" % (str(source[0]))
        return ret

...

env['BUILDERS']['Program'] = MyProgramBuilder(env['BUILDERS']['Program'])

It could be a little fragile because the wrapper passes all the
parameters up to BuilderBase, and the parameters needed by a Program
builder instance could change.  Anyway, maybe that helps, or maybe
someone knows a better way.

Gary

On 09/19/2017 03:20 AM, Julius Ziegler wrote:
> Dear all,
>
> I am trying to extend what an environment does by inheritance.
>
> Here is an example of what I try to achive. Expected result is that
> whenever I schedule build of a "Program" with a  MyEnv, a message gets
> printed to stdout:
>
> class MyEnv( Environment ):
>   def Program( self, *args, **kwargs ):
>     print "REGISTERED PROGRAM TARGET", args[0]
>     super().Program( *args, **kwargs )
>
> ...but this code seems to do nothing. It looks like the original
> Environment overloads the class attribute access or something like
> this. I am not a hardcore python programmer and it is really hard for
> me to figure out what is going on.
>
> Is there a best practice to achieve something similar?
>
> Thanks!
> Julius
>



More information about the Scons-users mailing list