do_support.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import logging
  2. import logging.config
  3. import os
  4. import string
  5. import io
  6. LOG = logging.getLogger(__name__)
  7. BASH = '/bin/bash'
  8. # This is used by some programs in falcon_kit/mains.
  9. simple_logging_config = """
  10. [loggers]
  11. keys=root
  12. [handlers]
  13. keys=stream
  14. [formatters]
  15. keys=form01,form02
  16. [logger_root]
  17. level=NOTSET
  18. handlers=stream
  19. [handler_stream]
  20. class=StreamHandler
  21. level=${FALCON_LOG_LEVEL}
  22. formatter=form01
  23. args=(sys.stderr,)
  24. [formatter_form01]
  25. format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
  26. [formatter_form02]
  27. format=[%(levelname)s]%(message)s
  28. """
  29. def setup_simple_logging(FALCON_LOG_LEVEL='DEBUG', **ignored):
  30. cfg = string.Template(simple_logging_config).substitute(FALCON_LOG_LEVEL=FALCON_LOG_LEVEL)
  31. logger_fileobj = io.StringIO(cfg)
  32. defaults = {}
  33. logging.config.fileConfig(logger_fileobj, defaults=defaults, disable_existing_loggers=False)
  34. def run_bash(script_fn):
  35. # Assume script was written by this program, so we know it is
  36. # available in the filesystem.
  37. # However, we cannot be sure that the execute permission is set,
  38. # so run it as a script.
  39. cmd = '{} -vex {}'.format(BASH, script_fn)
  40. LOG.info('!{}'.format(cmd))
  41. rc = os.system(cmd)
  42. if rc:
  43. raise Exception('{} <- {!r}'.format(rc, cmd))