Fix handling spaces in directory name
[apps/agl-service-can-low-level.git] / tests / site_scons / site_init.py
1 import subprocess
2 import sys
3 import re
4
5 try:
6     # Make terminal colors work on windows
7     import colorama
8     colorama.init()
9 except ImportError:
10     pass
11
12 def add_nanopb_builders(env):
13     '''Add the necessary builder commands for nanopb tests.'''
14
15     # Build command that runs a test program and saves the output
16     def run_test(target, source, env):
17         if len(source) > 1:
18             infile = open(str(source[1]))
19         else:
20             infile = None
21         
22         args = [str(source[0])]
23         if env.has_key('ARGS'):
24             args.extend(env['ARGS'])
25         
26         pipe = subprocess.Popen(args,
27                                 stdin = infile,
28                                 stdout = open(str(target[0]), 'w'),
29                                 stderr = sys.stderr)
30         result = pipe.wait()
31         if result == 0:
32             print '\033[32m[ OK ]\033[0m   Ran ' + str(source[0])
33         else:
34             print '\033[31m[FAIL]\033[0m   Program ' + str(source[0]) + ' returned ' + str(result)
35         return result
36         
37     run_test_builder = Builder(action = run_test,
38                                suffix = '.output')
39     env.Append(BUILDERS = {'RunTest': run_test_builder})
40
41     # Build command that decodes a message using protoc
42     def decode_actions(source, target, env, for_signature):
43         esc = env['ESCAPE']
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])))
47
48     decode_builder = Builder(generator = decode_actions,
49                              suffix = '.decoded')
50     env.Append(BUILDERS = {'Decode': decode_builder})    
51
52     # Build command that encodes a message using protoc
53     def encode_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 --encode=%s %s <%s >%s' % (
57             dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
58
59     encode_builder = Builder(generator = encode_actions,
60                              suffix = '.encoded')
61     env.Append(BUILDERS = {'Encode': encode_builder})    
62
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()
67         if data1 == data2:
68             print '\033[32m[ OK ]\033[0m   Files equal: ' + str(source[0]) + ' and ' + str(source[1])
69             return 0
70         else:
71             print '\033[31m[FAIL]\033[0m   Files differ: ' + str(source[0]) + ' and ' + str(source[1])
72             return 1
73
74     compare_builder = Builder(action = compare_files,
75                               suffix = '.equal')
76     env.Append(BUILDERS = {'Compare': compare_builder})
77
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
85                 return 1
86         else:
87             print '\033[32m[ OK ]\033[0m   All patterns found in ' + str(source[0])
88             return 0
89
90     match_builder = Builder(action = match_files, suffix = '.matched')
91     env.Append(BUILDERS = {'Match': match_builder})
92     
93