How do I run some code after every build in scons?

旧街凉风 提交于 2019-12-01 05:40:11

问题


I'm looking for a way to register somthing like an end-build callback in scons. For example, I'm doing something like this right now:

def print_build_summary():
    failures = SCons.Script.GetBuildFailures()
    notifyExe = 'notify-send '
    if len(failures) > 0:
        notifyExe = notifyExe + ' --urgency=critical Build Failed'
    else:
        notifyExe = notifyExe + ' --urgency=normal Build Succeed'

    os.system(notifyExe)

atexit.register(print_build_summary)

This only works in non-interactive mode. I'd like to be able to pop up something like this at the end of every build, specifically, when running multiple 'build' commands in an interactive scons session.

The only suggestions I've found, looking around, seem to be to use the dependency system or the AddPostAction call to glom this on. It doesn't seem quite right to me to do it that way, since it's not really a dependency (it's not even really a part of the build, strictly speaking) - it's just a static bit of code that needs to be run at the end of every build.

Thanks!


回答1:


I don't think there's anything wrong with using the dependency system to resolve this. This is how I normally do it:

def finish( target, source, env ):
    raise Exception( 'DO IT' )

finish_command = Command( 'finish', [], finish )
Depends( finish_command, DEFAULT_TARGETS )
Default( finish_command )

This creates a command that depends on the default targets for it's execution (so you know it'll always run last - see DEFAULT_TARGETS in scons manual). Hope this helps.




回答2:


Ive been looking into this and havent found that SCons offers anything that would help. This seems like quite a usefull feature, maybe the SCons developers are watching these threads and will take the suggestion...

I looked at the source code and figured out how to do it. I'll try to suggest this change to the SCons developers on scons.org.

If you're interested, the file is engine/SCons/Script/Main.py, and the function is _build_targets(). At the end of this funcion, you would simply need to add a call to a user supplied callback. Of course this solution would not be very useful if you build on several different machines in your network, since you would have to port the change everywhere its needed, but if you're only building on one machine, then maybe you could make the change until/if SCons officially provides a solution.

Let me know if you need help implementing the change, and I'll see what I can do.

Another option would be to wrap the call to SCons, and have the wrapper script perform the desired actions, but that wouldnt help in SCons interactive mode.

Hope this helps,

Brady

EDIT:

I create a feature request for this: http://scons.tigris.org/issues/show_bug.cgi?id=2834



来源:https://stackoverflow.com/questions/8901296/how-do-i-run-some-code-after-every-build-in-scons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!