exec_wrapper.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. # Import as few modules and define as few names as possible at global level before exec'ing the
  3. # file, as all names will be seen by the exec'ed code. To prevent name collisions, all names
  4. # defined here should begin with 'exec_wrapper_'
  5. import sys,os,time
  6. def exec_wrapper_get_colors():
  7. from collections import namedtuple
  8. return namedtuple('colors',['red','green','yellow','blue'])(*[
  9. (lambda s:s) if os.getenv('MMGEN_DISABLE_COLOR') else
  10. (lambda s,n=n:f'\033[{n};1m{s}\033[0m' )
  11. for n in (31,32,33,34) ])
  12. def exec_wrapper_init(): # don't change: name is used to test if script is running under exec_wrapper
  13. if os.path.dirname(sys.argv[1]) == 'test': # scripts in ./test do overlay setup themselves
  14. sys.path[0] = 'test'
  15. else:
  16. from test.overlay import overlay_setup
  17. sys.path[0] = overlay_setup(repo_root=os.getcwd()) # assume we're in the repo root
  18. os.environ['MMGEN_EXEC_WRAPPER'] = '1'
  19. os.environ['PYTHONPATH'] = '.'
  20. if 'TMUX' in os.environ:
  21. del os.environ['TMUX']
  22. if not os.getenv('EXEC_WRAPPER_NO_TRACEBACK'):
  23. try:
  24. os.unlink('test.py.err')
  25. except:
  26. pass
  27. def exec_wrapper_write_traceback(e):
  28. import traceback
  29. def gen_output():
  30. cwd = os.path.abspath('.')
  31. yield 'Traceback (most recent call last):'
  32. for e in traceback.extract_tb(sys.exc_info()[2]):
  33. yield ' File "{f}", line {l}, in {n}\n {L}'.format(
  34. f = (
  35. exec_wrapper_execed_file if e.filename == '<string>' else
  36. e.filename.removeprefix(cwd+'/').removeprefix('test/overlay/tree/').replace('_orig.py','.py')
  37. ),
  38. l = '(scrubbed)' if os.getenv('MMGEN_TEST_SUITE_DETERMINISTIC') else e.lineno,
  39. n = e.name,
  40. L = e.line or 'N/A' )
  41. tb_lines = list( gen_output() )
  42. exc_line = (
  43. repr(e) if type(e).__name__ in ('MMGenError','MMGenSystemExit') else
  44. '{}: {}'.format( type(e).__name__, e ))
  45. if 'SystemExit' in exc_line:
  46. tb_lines.pop()
  47. c = exec_wrapper_get_colors()
  48. sys.stdout.write('{}\n{}\n'.format( c.yellow( '\n'.join(tb_lines) ), c.red(exc_line) ))
  49. if not os.getenv('EXEC_WRAPPER_NO_TRACEBACK'):
  50. with open('test.py.err','w') as fp:
  51. fp.write('\n'.join(tb_lines + [exc_line]))
  52. def exec_wrapper_end_msg():
  53. if os.getenv('EXEC_WRAPPER_SPAWN') and not os.getenv('MMGEN_TEST_SUITE_DETERMINISTIC'):
  54. c = exec_wrapper_get_colors()
  55. # write to stdout to ensure script output gets to terminal first
  56. sys.stdout.write(c.blue('Runtime: {:0.5f} secs\n'.format(time.time() - exec_wrapper_tstart)))
  57. def exec_wrapper_tracemalloc_setup():
  58. if os.getenv('MMGEN_TRACEMALLOC'):
  59. os.environ['PYTHONTRACEMALLOC'] = '1'
  60. import tracemalloc
  61. tracemalloc.start()
  62. sys.stderr.write("INFO → Appending memory allocation stats to 'tracemalloc.log'\n")
  63. def exec_wrapper_tracemalloc_log():
  64. if os.getenv('MMGEN_TRACEMALLOC'):
  65. import tracemalloc,re
  66. snapshot = tracemalloc.take_snapshot()
  67. stats = snapshot.statistics('lineno')
  68. depth = 100
  69. col1w = 100
  70. with open('tracemalloc.log','a') as fp:
  71. fp.write('##### TOP {} {} #####\n'.format(depth,' '.join(sys.argv)))
  72. for stat in stats[:depth]:
  73. frame = stat.traceback[0]
  74. fn = re.sub(r'.*\/site-packages\/|.*\/mmgen\/test\/overlay\/tree\/','',frame.filename)
  75. fn = re.sub(r'.*\/mmgen\/test\/','test/',fn)
  76. fp.write('{f:{w}} {s:>8.2f} KiB\n'.format(
  77. f = f'{fn}:{frame.lineno}:',
  78. s = stat.size/1024,
  79. w = col1w ))
  80. fp.write('{f:{w}} {s:8.2f} KiB\n\n'.format(
  81. f = 'TOTAL {}:'.format(' '.join(sys.argv))[:col1w],
  82. s = sum(stat.size for stat in stats) / 1024,
  83. w = col1w ))
  84. exec_wrapper_init() # sets sys.path[0]
  85. exec_wrapper_tstart = time.time()
  86. exec_wrapper_tracemalloc_setup()
  87. try:
  88. sys.argv.pop(0)
  89. exec_wrapper_execed_file = sys.argv[0]
  90. with open(exec_wrapper_execed_file) as fp:
  91. exec(fp.read())
  92. except SystemExit as e:
  93. if e.code != 0 and not os.getenv('EXEC_WRAPPER_NO_TRACEBACK'):
  94. exec_wrapper_write_traceback(e)
  95. else:
  96. exec_wrapper_tracemalloc_log()
  97. exec_wrapper_end_msg()
  98. sys.exit(e.code)
  99. except Exception as e:
  100. if not os.getenv('EXEC_WRAPPER_NO_TRACEBACK'):
  101. exec_wrapper_write_traceback(e)
  102. retval = e.mmcode if hasattr(e,'mmcode') else e.code if hasattr(e,'code') else 1
  103. sys.exit(retval)
  104. exec_wrapper_tracemalloc_log()
  105. exec_wrapper_end_msg()