Add emacs and vim editing settings to the bottom of *.py files.
[scons.git] / test / Scanner / FindPathDirs.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 use of the FindPathDirs() function (actually a class)
29 can be used to specify a path_function of a scanner.
30 """
31
32 import TestSCons
33
34 _python_ = TestSCons._python_
35
36 test = TestSCons.TestSCons()
37
38 test.subdir('inc1', 'inc2')
39
40 test.write('build.py', r"""
41 import os.path
42 import string
43 import sys
44 path = string.split(sys.argv[1])
45 input = open(sys.argv[2], 'rb')
46 output = open(sys.argv[3], 'wb')
47
48 def find_file(f):
49     for dir in path:
50         p = dir + os.sep + f
51         if os.path.exists(p):
52             return open(p, 'rb')
53     return None
54
55 def process(infp, outfp):
56     for line in infp.readlines():
57         if line[:8] == 'include ':
58             file = line[8:-1]
59             process(find_file(file), outfp)
60         else:
61             outfp.write(line)
62
63 process(input, output)
64
65 sys.exit(0)
66 """)
67
68 # Execute a subsidiary SConscript just to make sure we can
69 # get at the Scanner keyword from there.
70
71 test.write('SConstruct', """\
72 SConscript('SConscript')
73 """)
74
75 test.write('SConscript', """\
76 import os.path
77 import re
78
79 include_re = re.compile(r'^include\s+(\S+)$', re.M)
80
81 def kfile_scan(node, env, path, arg):
82     contents = node.get_text_contents()
83     includes = include_re.findall(contents)
84     if includes == []:
85          return []
86     results = []
87     for inc in includes:
88         for dir in path:
89             file = str(dir) + os.sep + inc
90             if os.path.exists(file):  
91                 results.append(file)
92                 break
93     return results
94
95 kscan = Scanner(name = 'kfile',
96                 function = kfile_scan,
97                 argument = None,
98                 skeys = ['.k'],
99                 path_function = FindPathDirs('KPATH'))
100
101 ##########################################################
102 # Test scanner as found automatically from the environment
103 # (backup_source_scanner)
104
105 env = Environment(KPATH=['inc1', 'inc2'])
106 env.Append(SCANNERS = kscan)
107
108 env.Command('foo', 'foo.k', r'%(_python_)s build.py "$KPATH" $SOURCES $TARGET')
109 """ % locals())
110
111
112
113 test.write('foo.k', 
114 """foo.k 1 line 1
115 include xxx
116 include yyy
117 foo.k 1 line 4
118 """)
119
120 test.write(['inc1', 'xxx'], "inc1/xxx 1\n")
121 test.write(['inc2', 'yyy'], "inc2/yyy 1\n")
122
123
124
125
126 test.run()
127
128 test.must_match('foo', "foo.k 1 line 1\ninc1/xxx 1\ninc2/yyy 1\nfoo.k 1 line 4\n")
129
130 test.up_to_date(arguments = '.')
131
132
133
134 test.write(['inc1', 'xxx'], "inc1/xxx 2\n")
135
136 test.run()
137
138 test.must_match('foo', "foo.k 1 line 1\ninc1/xxx 2\ninc2/yyy 1\nfoo.k 1 line 4\n")
139
140
141
142 test.write(['inc1', 'yyy'], "inc1/yyy 2\n")
143
144 test.run()
145
146 test.must_match('foo', "foo.k 1 line 1\ninc1/xxx 2\ninc1/yyy 2\nfoo.k 1 line 4\n")
147
148
149
150 test.pass_test()
151
152 # Local Variables:
153 # tab-width:4
154 # indent-tabs-mode:nil
155 # End:
156 # vim: set expandtab tabstop=4 shiftwidth=4: