6 # Make terminal colors work on windows
12 def add_nanopb_builders(env):
13 '''Add the necessary builder commands for nanopb tests.'''
15 # Build command that runs a test program and saves the output
16 def run_test(target, source, env):
18 infile = open(str(source[1]))
22 args = [str(source[0])]
23 if env.has_key('ARGS'):
24 args.extend(env['ARGS'])
26 pipe = subprocess.Popen(args,
28 stdout = open(str(target[0]), 'w'),
32 print '\033[32m[ OK ]\033[0m Ran ' + str(source[0])
34 print '\033[31m[FAIL]\033[0m Program ' + str(source[0]) + ' returned ' + str(result)
37 run_test_builder = Builder(action = run_test,
39 env.Append(BUILDERS = {'RunTest': run_test_builder})
41 # Build command that decodes a message using protoc
42 def decode_actions(source, target, env, for_signature):
44 dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
45 return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (
46 dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
48 decode_builder = Builder(generator = decode_actions,
50 env.Append(BUILDERS = {'Decode': decode_builder})
52 # Build command that encodes a message using protoc
53 def encode_actions(source, target, env, for_signature):
55 dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
56 return '$PROTOC $PROTOCFLAGS %s --encode=%s %s <%s >%s' % (
57 dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
59 encode_builder = Builder(generator = encode_actions,
61 env.Append(BUILDERS = {'Encode': encode_builder})
63 # Build command that asserts that two files be equal
64 def compare_files(target, source, env):
65 data1 = open(str(source[0]), 'rb').read()
66 data2 = open(str(source[1]), 'rb').read()
68 print '\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1])
71 print '\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1])
74 compare_builder = Builder(action = compare_files,
76 env.Append(BUILDERS = {'Compare': compare_builder})
78 # Build command that checks that each pattern in source2 is found in source1.
79 def match_files(target, source, env):
80 data = open(str(source[0]), 'rU').read()
81 patterns = open(str(source[1]))
82 for pattern in patterns:
83 if pattern.strip() and not re.search(pattern.strip(), data, re.MULTILINE):
84 print '\033[31m[FAIL]\033[0m Pattern not found in ' + str(source[0]) + ': ' + pattern
87 print '\033[32m[ OK ]\033[0m All patterns found in ' + str(source[0])
90 match_builder = Builder(action = match_files, suffix = '.matched')
91 env.Append(BUILDERS = {'Match': match_builder})