site_init.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import subprocess
  2. import sys
  3. import re
  4. from platforms.stm32.stm32 import set_stm32_platform
  5. from platforms.avr.avr import set_avr_platform
  6. from platforms.mips.mips import set_mips_platform
  7. from platforms.mipsel.mipsel import set_mipsel_platform
  8. from platforms.riscv64.riscv64 import set_riscv64_platform
  9. platforms = {
  10. 'STM32': set_stm32_platform,
  11. 'AVR': set_avr_platform,
  12. 'MIPS': set_mips_platform,
  13. 'MIPSEL': set_mipsel_platform,
  14. 'RISCV64': set_riscv64_platform,
  15. }
  16. try:
  17. # Make terminal colors work on windows
  18. import colorama
  19. colorama.init()
  20. except ImportError:
  21. pass
  22. def add_nanopb_builders(env):
  23. '''Add the necessary builder commands for nanopb tests.'''
  24. # Build command that runs a test program and saves the output
  25. def run_test(target, source, env):
  26. if len(source) > 1:
  27. infile = open(str(source[1]))
  28. else:
  29. infile = None
  30. if "COMMAND" in env:
  31. args = [env["COMMAND"]]
  32. else:
  33. args = [str(source[0])]
  34. if 'ARGS' in env:
  35. args.extend(env['ARGS'])
  36. if "TEST_RUNNER" in env:
  37. args = [env["TEST_RUNNER"]] + args
  38. print('Command line: ' + str(args))
  39. pipe = subprocess.Popen(args,
  40. stdin = infile,
  41. stdout = open(str(target[0]), 'w'),
  42. stderr = sys.stderr)
  43. result = pipe.wait()
  44. if result == 0:
  45. print('\033[32m[ OK ]\033[0m Ran ' + args[0])
  46. else:
  47. print('\033[31m[FAIL]\033[0m Program ' + args[0] + ' returned ' + str(result))
  48. return result
  49. run_test_builder = Builder(action = run_test,
  50. suffix = '.output')
  51. env.Append(BUILDERS = {'RunTest': run_test_builder})
  52. # Build command that decodes a message using protoc
  53. def decode_actions(source, target, env, for_signature):
  54. esc = env['ESCAPE']
  55. dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
  56. return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (
  57. dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
  58. decode_builder = Builder(generator = decode_actions,
  59. suffix = '.decoded')
  60. env.Append(BUILDERS = {'Decode': decode_builder})
  61. # Build command that encodes a message using protoc
  62. def encode_actions(source, target, env, for_signature):
  63. esc = env['ESCAPE']
  64. dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
  65. return '$PROTOC $PROTOCFLAGS %s --encode=%s %s <%s >%s' % (
  66. dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
  67. encode_builder = Builder(generator = encode_actions,
  68. suffix = '.encoded')
  69. env.Append(BUILDERS = {'Encode': encode_builder})
  70. # Build command that asserts that two files be equal
  71. def compare_files(target, source, env):
  72. data1 = open(str(source[0]), 'rb').read()
  73. data2 = open(str(source[1]), 'rb').read()
  74. if data1 == data2:
  75. print('\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1]))
  76. return 0
  77. else:
  78. print('\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1]))
  79. return 1
  80. compare_builder = Builder(action = compare_files,
  81. suffix = '.equal')
  82. env.Append(BUILDERS = {'Compare': compare_builder})
  83. # Build command that checks that each pattern in source2 is found in source1.
  84. def match_files(target, source, env):
  85. data = open(str(source[0]), 'rU').read()
  86. patterns = open(str(source[1]))
  87. for pattern in patterns:
  88. if pattern.strip():
  89. invert = False
  90. if pattern.startswith('! '):
  91. invert = True
  92. pattern = pattern[2:]
  93. status = re.search(pattern.strip(), data, re.MULTILINE)
  94. if not status and not invert:
  95. print('\033[31m[FAIL]\033[0m Pattern not found in ' + str(source[0]) + ': ' + pattern)
  96. return 1
  97. elif status and invert:
  98. print('\033[31m[FAIL]\033[0m Pattern should not exist, but does in ' + str(source[0]) + ': ' + pattern)
  99. return 1
  100. else:
  101. print('\033[32m[ OK ]\033[0m All patterns found in ' + str(source[0]))
  102. return 0
  103. match_builder = Builder(action = match_files, suffix = '.matched')
  104. env.Append(BUILDERS = {'Match': match_builder})