Debug.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2011 Nick Mathewson, Michael Stone
  4. # Copyright 2013 The Tor Project
  5. #
  6. # You may do anything with this work that copyright law would normally
  7. # restrict, so long as you retain the above notice(s) and this license
  8. # in all redistributed copies and derived works. There is no warranty.
  9. # Future imports for Python 2.7, mandatory in 3.0
  10. from __future__ import division
  11. from __future__ import print_function
  12. from __future__ import unicode_literals
  13. import cgitb
  14. import os
  15. import sys
  16. # Get verbose tracebacks, so we can diagnose better.
  17. cgitb.enable(format="plain")
  18. # Set debug_flag=True in order to debug this program or to get hints
  19. # about what's going wrong in your system.
  20. debug_flag = os.environ.get("CHUTNEY_DEBUG", "") != ""
  21. def debug(s):
  22. "Print a debug message on stdout if debug_flag is True."
  23. if debug_flag:
  24. print("DEBUG: %s" % s)
  25. def main():
  26. global debug_flag
  27. debug("This message should appear if $CHUTNEY_DEBUG is true.")
  28. debug_flag = True
  29. debug("This message should always appear.")
  30. debug_flag = False
  31. debug("This message should never appear.")
  32. # We don't test tracebacks, because it's hard to know what to expect
  33. # (and they make python exit with a non-zero exit status)
  34. return 0
  35. if __name__ == '__main__':
  36. sys.exit(main())