add a parameter to RebootNodeWithPCU to perform a 'testrun' without rebooting the...
[plcapi.git] / PLC / Debug.py
1 # $Id$
2 # $URL$
3 import time
4 import sys
5 import syslog
6
7 class unbuffered:
8     """
9     Write to /var/log/httpd/error_log. See
10
11     http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp
12     """
13
14     def write(self, data):
15         sys.stderr.write(data)
16         sys.stderr.flush()
17
18 log = unbuffered()
19
20 def profile(callable):
21     """
22     Prints the runtime of the specified callable. Use as a decorator, e.g.,
23
24         @profile
25         def foo(...):
26             ...
27
28     Or, equivalently,
29
30         def foo(...):
31             ...
32         foo = profile(foo)
33
34     Or inline:
35
36         result = profile(foo)(...)
37     """
38
39     def wrapper(*args, **kwds):
40         start = time.time()
41         result = callable(*args, **kwds)
42         end = time.time()
43         args = map(str, args)
44         args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()]
45         print >> log, "%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start)
46         return result
47
48     return wrapper
49
50 if __name__ == "__main__":
51     def sleep(seconds = 1):
52         time.sleep(seconds)
53
54     sleep = profile(sleep)
55
56     sleep(1)