[Scons-users] Using a custom Node class

Greg Ward greg at gerg.ca
Thu Jul 12 15:08:33 EDT 2012


On 28 June 2012, I said:

> I recently figured out how to write a custom Node class to override

> the default signature behaviour. (Specifically, hashing Java .jar

> files is too sensitive, since the same bytecode can yield different

> signatures due to file timestamps changing. So instead I open up the

> jar and hash the contents of each file in it. Works pretty well.)

>

> But I'm unclear on the best way to *use* my new class. Currently I'm

> just doing this:

>

> jarfile = env.File('$build_dir/java/%s.jar' % name)

> jarfile.__class__ = JarNode

>

> Rationale: env.File() is not the constructor, it's a factory method

> that does other magic stuff. I don't want to repeat that stuff, I just

> want env.File() to do it for me. And then I change the new File object

> to a JarNode object. (JarNode is a subclass of SCons.Node.FS.File, so

> this seems like a pretty safe transformation.) It's gross, but it

> works.

>

> Is there a cleaner way to do this?


I figured out a slightly less gross way to do this. First, I define my
custom Node class:

class JarNode(Node.FS.File):
[...implementation omitted...]

Then I define a little function that does what I want:

# similar to what env.File() ultimately does
def _jarnode(env, name, directory=None, create=True):
return env.fs._lookup(env.subst(name), directory, JarNode, create)

and then I attach that as an Environment method:

env.AddMethod(_jarnode, 'JarNode')

My _jarnode() function is not quite as powerful as env.File() in that
it doesn't take lists of filenames. Oh well, I don't need it to. Works
for me.

Greg


More information about the Scons-users mailing list