3

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.

Peter Hansen
  • 21,046
  • 5
  • 50
  • 72
sorin
  • 161,544
  • 178
  • 535
  • 806

1 Answers1

4

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".

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395