1 | # Copyright (c) MetaCommunications, Inc. 2003-2005 |
---|
2 | # |
---|
3 | # Distributed under the Boost Software License, Version 1.0. |
---|
4 | # (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | # http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | import difflib |
---|
8 | import os |
---|
9 | import re |
---|
10 | import shutil |
---|
11 | import string |
---|
12 | import sys |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | def scan_for_test_cases(): |
---|
17 | return [ os.path.join( "test-cases", x ) for x in os.listdir( "test-cases" ) if x != "CVS" ] |
---|
18 | |
---|
19 | def clean_dir( dir ): |
---|
20 | if os.path.exists( dir ): |
---|
21 | shutil.rmtree( dir ) |
---|
22 | os.makedirs( dir ) |
---|
23 | |
---|
24 | def system( commands ): |
---|
25 | if sys.platform == 'win32': |
---|
26 | f = open( 'tmp.cmd', 'w' ) |
---|
27 | f.write( string.join( commands, '\n' ) ) |
---|
28 | f.close() |
---|
29 | rc = os.system( 'tmp.cmd' ) |
---|
30 | os.unlink( 'tmp.cmd' ) |
---|
31 | return rc |
---|
32 | else: |
---|
33 | rc = os.system( '&&'.join( commands ) ) |
---|
34 | return rc |
---|
35 | |
---|
36 | def checked_system( commands, valid_return_codes = [ 0 ] ): |
---|
37 | rc = system( commands ) |
---|
38 | if rc not in [ 0 ] + valid_return_codes: |
---|
39 | raise Exception( 'Command sequence "%s" failed with return code %d' % ( commands, rc ) ) |
---|
40 | return rc |
---|
41 | |
---|
42 | def list_recursively( dir ): |
---|
43 | r = [] |
---|
44 | for root, dirs, files in os.walk( dir, topdown=False ): |
---|
45 | root = root[ len( dir ) + 1 : ] |
---|
46 | r.extend( [ os.path.join( root, x ) for x in dirs ] ) |
---|
47 | r.extend( [ os.path.join( root, x ) for x in files ] ) |
---|
48 | |
---|
49 | return r |
---|
50 | |
---|
51 | def find_process_jam_log(): |
---|
52 | root = "../../../" |
---|
53 | |
---|
54 | for root, dirs, files in os.walk( os.path.join( root, "bin.v2" ), topdown=False ): |
---|
55 | if "process_jam_log.exe" in files: |
---|
56 | return os.path.abspath( os.path.normpath( os.path.join( root, "process_jam_log.exe" ) ) ) |
---|
57 | if "process_jam_log" in files: |
---|
58 | return os.path.abspath( os.path.normpath( os.path.join( root, "process_jam_log" ) ) ) |
---|
59 | return None |
---|
60 | |
---|
61 | def process_jam_log( executable, file, locate_root, results_dir ): |
---|
62 | args = [] |
---|
63 | args.append( executable ) |
---|
64 | # args.append( '--echo' ) |
---|
65 | args.append( '--create-directories' ) |
---|
66 | args.append( '--v2' ) |
---|
67 | args.append( locate_root ) |
---|
68 | args.append( '<' ) |
---|
69 | args.append( file ) |
---|
70 | |
---|
71 | cmd = " ".join( args ) |
---|
72 | print "Running process_jam_log (%s)" % cmd |
---|
73 | checked_system( [ cmd ] ) |
---|
74 | |
---|
75 | |
---|
76 | def read_file( file_path ): |
---|
77 | f = open( file_path ) |
---|
78 | try: |
---|
79 | return f.read() |
---|
80 | finally: |
---|
81 | f.close() |
---|
82 | |
---|
83 | def remove_timestamps( log_lines ): |
---|
84 | return [ re.sub( "timestamp=\"[^\"]+\"", "timestamp=\"\"", x ) for x in log_lines ] |
---|
85 | |
---|
86 | def determine_locate_root( bjam_log ): |
---|
87 | locate_root = None |
---|
88 | f = open( 'bjam.log' ) |
---|
89 | try: |
---|
90 | locate_root_re = re.compile( r'locate-root\s+"(.*)"' ) |
---|
91 | for l in f.readlines(): |
---|
92 | m = locate_root_re.match( l ) |
---|
93 | if m: |
---|
94 | locate_root = m.group(1) |
---|
95 | break |
---|
96 | finally: |
---|
97 | f.close() |
---|
98 | return locate_root |
---|
99 | |
---|
100 | def read_file( path ): |
---|
101 | f = open( path ) |
---|
102 | try: |
---|
103 | return f.read() |
---|
104 | finally: |
---|
105 | f.close() |
---|
106 | |
---|
107 | def read_file_lines( path ): |
---|
108 | f = open( path ) |
---|
109 | try: |
---|
110 | return f.readlines() |
---|
111 | finally: |
---|
112 | f.close() |
---|
113 | |
---|
114 | def write_file( path, content ): |
---|
115 | f = open( path, 'w' ) |
---|
116 | try: |
---|
117 | return f.write( content ) |
---|
118 | finally: |
---|
119 | f.close() |
---|
120 | |
---|
121 | def write_file_lines( path, content ): |
---|
122 | f = open( path, 'w' ) |
---|
123 | try: |
---|
124 | return f.writelines( content ) |
---|
125 | finally: |
---|
126 | f.close() |
---|
127 | |
---|
128 | |
---|
129 | def run_test_cases( test_cases ): |
---|
130 | process_jam_log_executable = find_process_jam_log() |
---|
131 | print 'Found process_jam_log: %s' % process_jam_log_executable |
---|
132 | initial_dir = os.getcwd() |
---|
133 | for test_case in test_cases: |
---|
134 | os.chdir( initial_dir ) |
---|
135 | print 'Running test case "%s"' % test_case |
---|
136 | os.chdir( test_case ) |
---|
137 | if os.path.exists( "expected" ): |
---|
138 | locate_root = determine_locate_root( 'bjam.log' ) |
---|
139 | print 'locate_root: %s' % locate_root |
---|
140 | |
---|
141 | actual_results_dir = os.path.join( test_case, "actual" ) |
---|
142 | clean_dir( "actual" ) |
---|
143 | os.chdir( "actual" ) |
---|
144 | root = os.getcwd() |
---|
145 | i = 0 |
---|
146 | while 1: |
---|
147 | if i == 0: |
---|
148 | bjam_log_file = 'bjam.log' |
---|
149 | else: |
---|
150 | bjam_log_file = 'bjam.log.%0d' % i |
---|
151 | i += 1 |
---|
152 | print 'Looking for %s' % bjam_log_file |
---|
153 | if not os.path.exists( os.path.join( '..', bjam_log_file ) ): |
---|
154 | print ' does not exists' |
---|
155 | break |
---|
156 | print ' found' |
---|
157 | write_file_lines(bjam_log_file.replace( 'bjam', 'bjam_' ), |
---|
158 | [ x.replace( locate_root, root ) for x in read_file_lines( os.path.join( '..', bjam_log_file ) ) ] ) |
---|
159 | |
---|
160 | process_jam_log( executable = process_jam_log_executable |
---|
161 | , results_dir = "." |
---|
162 | , locate_root = root |
---|
163 | , file=bjam_log_file.replace( 'bjam', 'bjam_' ) ) |
---|
164 | |
---|
165 | actual_content = list_recursively( "." ) |
---|
166 | actual_content.sort() |
---|
167 | result_xml = [] |
---|
168 | for test_log in [ x for x in actual_content if os.path.splitext( x )[1] == '.xml' ]: |
---|
169 | print 'reading %s' % test_log |
---|
170 | result = [ re.sub( r'timestamp="(.*)"', 'timestamp="xxx"', x ) for x in read_file_lines( test_log ) ] |
---|
171 | result_xml.extend( result ) |
---|
172 | |
---|
173 | write_file_lines( 'results.xml', result_xml ) |
---|
174 | os.chdir( '..' ) |
---|
175 | assert read_file( 'expected/results.xml' ) == read_file( 'actual/results.xml' ) |
---|
176 | os.chdir( '..' ) |
---|
177 | else: |
---|
178 | raise ' Test case "%s" doesn\'t contain the expected results directory ("expected" )' % ( test_case ) |
---|
179 | |
---|
180 | run_test_cases( scan_for_test_cases() ) |
---|
181 | # print find_process_jam_log() |
---|