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

Bill Deegan bill at baddogconsulting.com
Thu Sep 21 12:50:44 EDT 2017


For the time being, don't extend Environment by inheritance.

It's likely to break as the class structure is not part of the published
API.

-Bill

On Thu, Sep 21, 2017 at 11:09 AM, Gary Granger <granger at ucar.edu> wrote:

> 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
> >
>
> _______________________________________________
> Scons-users mailing list
> Scons-users at scons.org
> https://pairlist4.pair.net/mailman/listinfo/scons-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20170921/cac134be/attachment.html>


More information about the Scons-users mailing list