Add emacs and vim editing settings to the bottom of *.py files.
[scons.git] / test / Scanner / source_scanner-dict.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Verify that a source_scanner that uses a dictionary to select more
29 specific scanners for source file suffixes works correctly, even
30 when it's handed a file suffix that it doesn't know how to scan
31 (i.e., for which it doesn't have a specific scanner in its dictionary).
32 """
33
34 import TestSCons
35
36 _python_ = TestSCons._python_
37
38 test = TestSCons.TestSCons()
39
40 test.write('build.py', r"""
41 import sys
42 output = open(sys.argv[1], 'wb')
43 for infile in sys.argv[2:]:
44     input = open(infile, 'rb')
45
46     include_prefix = 'include%s ' % infile[-1]
47
48     def process(infp, outfp, include_prefix=include_prefix):
49         for line in infp.readlines():
50             if line[:len(include_prefix)] == include_prefix:
51                 file = line[len(include_prefix):-1]
52                 process(open(file, 'rb'), outfp)
53             else:
54                 outfp.write(line)
55
56     process(input, output)
57
58 sys.exit(0)
59 """)
60
61 # Execute a subsidiary SConscript just to make sure we can
62 # get at the Scanner keyword from there.
63
64 test.write('SConstruct', """
65 SConscript('SConscript')
66 """)
67
68 test.write('SConscript', """
69 import re
70
71 include1_re = re.compile(r'^include1\s+(\S+)$', re.M)
72 include2_re = re.compile(r'^include2\s+(\S+)$', re.M)
73 include3_re = re.compile(r'^include3\s+(\S+)$', re.M)
74
75 def k1_scan(node, env, scanpaths, arg=None):
76     contents = node.get_text_contents()
77     includes = include1_re.findall(contents)
78     return includes
79
80 def k2_scan(node, env, scanpaths, arg=None):
81     contents = node.get_text_contents()
82     includes = include2_re.findall(contents)
83     return includes
84
85 def k3_scan(node, env, scanpaths, arg=None):
86     contents = node.get_text_contents()
87     includes = include3_re.findall(contents)
88     return includes
89
90 kscanner = Scanner({'.k1' : Scanner(k1_scan), '.k2': Scanner(k2_scan)})
91
92 b = Builder(action=r'%(_python_)s build.py $TARGET $SOURCES',
93             source_scanner=kscanner)
94 env = Environment(BUILDERS={'Build':b})
95
96 kscanner.add_scanner('.k3', Scanner(k3_scan))
97
98 env.Build('aaa', 'aaa.k1')
99 env.Build('bbb', 'bbb.k2')
100 env.Build('ccc', 'ccc.k3')
101 env.Build('ddd', ['ddd.k4', 'aaa.k1', 'bbb.k2', 'ccc.k3'])
102 """ % locals())
103
104 test.write('aaa.k1', 
105 """aaa.k1 1
106 line 2
107 include1 xxx
108 include2 yyy
109 include3 zzz
110 line 6
111 """)
112
113 test.write('bbb.k2', 
114 """bbb.k2 1
115 line 2
116 include1 xxx
117 include2 yyy
118 include3 zzz
119 line 6
120 """)
121
122 test.write('ccc.k3', 
123 """ccc.k3 1
124 line 2
125 include1 xxx
126 include2 yyy
127 include3 zzz
128 line 6
129 """)
130
131 test.write('ddd.k4', 
132 """ddd.k4 1
133 line 2
134 line 3
135 """)
136
137 test.write('xxx', "xxx 1\n")
138 test.write('yyy', "yyy 1\n")
139 test.write('zzz', "zzz 1\n")
140
141
142
143
144 expect = test.wrap_stdout("""\
145 %(_python_)s build.py aaa aaa.k1
146 %(_python_)s build.py bbb bbb.k2
147 %(_python_)s build.py ccc ccc.k3
148 %(_python_)s build.py ddd ddd.k4 aaa.k1 bbb.k2 ccc.k3
149 """ % locals())
150
151 test.run(stdout=expect)
152
153 expect_aaa = 'aaa.k1 1\nline 2\nxxx 1\ninclude2 yyy\ninclude3 zzz\nline 6\n'
154 expect_bbb = 'bbb.k2 1\nline 2\ninclude1 xxx\nyyy 1\ninclude3 zzz\nline 6\n'
155 expect_ccc = 'ccc.k3 1\nline 2\ninclude1 xxx\ninclude2 yyy\nzzz 1\nline 6\n'
156 expect_ddd = 'ddd.k4 1\nline 2\nline 3\n' + expect_aaa + expect_bbb + expect_ccc
157
158 test.must_match('aaa', expect_aaa)
159 test.must_match('bbb', expect_bbb)
160 test.must_match('ccc', expect_ccc)
161 test.must_match('ddd', expect_ddd)
162
163 test.up_to_date(arguments = '.')
164
165
166
167 test.write('zzz', "zzz 2\n")
168
169 expect = test.wrap_stdout("""\
170 %(_python_)s build.py ccc ccc.k3
171 %(_python_)s build.py ddd ddd.k4 aaa.k1 bbb.k2 ccc.k3
172 """ % locals())
173
174 test.run(stdout=expect)
175
176 expect_ccc = 'ccc.k3 1\nline 2\ninclude1 xxx\ninclude2 yyy\nzzz 2\nline 6\n'
177 expect_ddd = 'ddd.k4 1\nline 2\nline 3\n' + expect_aaa + expect_bbb + expect_ccc
178
179 test.must_match('bbb', expect_bbb)
180 test.must_match('ddd', expect_ddd)
181
182
183
184 test.write('yyy', "yyy 2\n")
185
186 expect = test.wrap_stdout("""\
187 %(_python_)s build.py bbb bbb.k2
188 %(_python_)s build.py ddd ddd.k4 aaa.k1 bbb.k2 ccc.k3
189 """ % locals())
190
191 test.run(stdout=expect)
192
193 expect_bbb = 'bbb.k2 1\nline 2\ninclude1 xxx\nyyy 2\ninclude3 zzz\nline 6\n'
194 expect_ddd = 'ddd.k4 1\nline 2\nline 3\n' + expect_aaa + expect_bbb + expect_ccc
195
196 test.must_match('bbb', expect_bbb)
197 test.must_match('ddd', expect_ddd)
198
199
200
201 test.write('xxx', "xxx 2\n")
202
203 expect = test.wrap_stdout("""\
204 %(_python_)s build.py aaa aaa.k1
205 %(_python_)s build.py ddd ddd.k4 aaa.k1 bbb.k2 ccc.k3
206 """ % locals())
207
208 test.run(stdout=expect)
209
210 expect_aaa = 'aaa.k1 1\nline 2\nxxx 2\ninclude2 yyy\ninclude3 zzz\nline 6\n'
211 expect_ddd = 'ddd.k4 1\nline 2\nline 3\n' + expect_aaa + expect_bbb + expect_ccc
212
213 test.must_match('aaa', expect_aaa)
214 test.must_match('ddd', expect_ddd)
215
216
217
218 test.pass_test()
219
220 # Local Variables:
221 # tab-width:4
222 # indent-tabs-mode:nil
223 # End:
224 # vim: set expandtab tabstop=4 shiftwidth=4: