Search code examples
pythonwaf

How can I add a code that is always executed by Waf before exit?


I want to make Waf generate a beep when it finishes the execution of any command that took more than 10 seconds.

I don't know how do add this and assure that the code executes when Waf exits.

This should run for any Waf command not only build.

I checked the Waf book but I wasn't able to find any indication about how should I do this.


Solution

  • In your wscript module, you can use the Python standard library's atexit to register callables that you want to be called when the process exit. For example:

    import atexit
    import time
    
    class MayBeep(object):
      def __init__(self, deadline=10.0):
        self.deadline = time.time() + deadline
      def __call__(self):
        if time.time() > self.deadline():
          print '\7'
    
    atexit.register(MayBeep())
    
    ... rest of your wscript module ...
    

    Of course you may use something better than print '\7' for beeping purposes (all the way to full-fledged multimedia extravaganzas, depending on what other Python extensions you import and use), but this code answers the Q's title -- "add code that's always executed on exit".