__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import json
  5. import logging
  6. import argparse
  7. from yatest_lib.ya import Ya
  8. import yatest.common as yc
  9. RECIPE_START_OPTION = "start"
  10. RECIPE_STOP_OPTION = "stop"
  11. ya = None
  12. collect_cores = None
  13. sanitizer_extra_checks = None
  14. class Config:
  15. def __init__(self):
  16. self.ya = ya
  17. self.collect_cores = collect_cores
  18. self.sanitizer_extra_checks = sanitizer_extra_checks
  19. def _setup_logging(level=logging.DEBUG):
  20. root_logger = logging.getLogger()
  21. root_logger.setLevel(level)
  22. log_format = '%(asctime)s - %(levelname)s - %(name)s - %(funcName)s: %(message)s'
  23. stdout_handler = logging.StreamHandler(sys.stdout)
  24. stdout_handler.setLevel(logging.DEBUG)
  25. formatter = logging.Formatter(log_format)
  26. stdout_handler.setFormatter(formatter)
  27. root_logger.addHandler(stdout_handler)
  28. def get_options():
  29. parser = argparse.ArgumentParser()
  30. parser.add_argument("--show-cwd", action="store_true", dest="show_cwd", default=False, help="show recipe cwd")
  31. parser.add_argument("--test-debug", action="store_true", dest="test_debug", default=False, help="test debug mode")
  32. parser.add_argument("--test-stderr", action="store_true", dest="test_stderr", default=False, help="test stderr")
  33. parser.add_argument("--pdb", action="store_true", dest="pdb", default=False, help="run pdb on error")
  34. parser.add_argument("--sanitizer-extra-checks", dest="sanitizer_extra_checks", action="store_true", default=False, help="enables extra checks for tests built with sanitizers")
  35. parser.add_argument("--collect-cores", dest="collect_cores", action="store_true", default=False, help="allows core dump file recovering during test")
  36. parser.add_argument("--build-root", type=str, dest="build_root", default=None, help="Build root directory")
  37. parser.add_argument("--source-root", type=str, dest="source_root", default=None, help="Source root directory")
  38. parser.add_argument("--output-dir", type=str, dest="output_dir", default=None, help="Output directory")
  39. parser.add_argument("--env-file", type=str, dest="env_file", default=None, help="File to read/write environment variables")
  40. args, opts = parser.parse_known_args()
  41. global ya, sanitizer_extra_checks, collect_cores
  42. _setup_logging()
  43. context = {
  44. "test_stderr": args.test_stderr,
  45. }
  46. ya = Ya(context=context, build_root=args.build_root, source_root=args.source_root, output_dir=args.output_dir, env_file=args.env_file)
  47. ya._data_root = "" # XXX remove
  48. sanitizer_extra_checks = args.sanitizer_extra_checks
  49. if sanitizer_extra_checks:
  50. for envvar in ['LSAN_OPTIONS', 'ASAN_OPTIONS']:
  51. if envvar in os.environ:
  52. os.environ.pop(envvar)
  53. if envvar + '_ORIGINAL' in os.environ:
  54. os.environ[envvar] = os.environ[envvar + '_ORIGINAL']
  55. collect_cores = args.collect_cores
  56. yc.runtime._set_ya_config(config=Config())
  57. for recipe_option in RECIPE_START_OPTION, RECIPE_STOP_OPTION:
  58. if recipe_option in opts:
  59. return args, opts[opts.index(recipe_option):]
  60. def set_env(key, value):
  61. with open(ya.env_file, "a") as f:
  62. json.dump({key: value}, f)
  63. f.write("\n")
  64. def tty():
  65. if os.isatty(1):
  66. return
  67. f = open('/dev/tty', 'w+')
  68. fd = f.fileno()
  69. os.dup2(fd, 0)
  70. os.dup2(fd, 1)
  71. os.dup2(fd, 2)
  72. def declare_recipe(start, stop):
  73. parsed_args, argv = get_options()
  74. if parsed_args.show_cwd:
  75. print("Recipe \"{} {}\" working dir is {}".format(sys.argv[0], " ".join(argv), os.getcwd()))
  76. try:
  77. if argv[0] == RECIPE_START_OPTION:
  78. start(argv[1:])
  79. elif argv[0] == RECIPE_STOP_OPTION:
  80. stop(argv[1:])
  81. except Exception:
  82. if parsed_args.pdb:
  83. tty()
  84. import ipdb
  85. ipdb.post_mortem()
  86. else:
  87. raise