trivial typo in man page
[scons.git] / test / option-j.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 """
26 This tests the -j command line option, and the num_jobs
27 SConscript settable option.
28 """
29
30 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
31
32 import os.path
33 import string
34
35 import TestSCons
36
37 _python_ = TestSCons._python_
38
39 try:
40     import threading
41 except ImportError:
42     # if threads are not supported, then
43     # there is nothing to test
44     TestCmd.no_result()
45     sys.exit()
46
47
48 test = TestSCons.TestSCons()
49
50 test.write('build.py', r"""
51 import time
52 import sys
53 file = open(sys.argv[1], 'wb')
54 file.write(str(time.time()) + '\n')
55 time.sleep(1)
56 file.write(str(time.time()))
57 file.close()
58 """)
59
60 test.subdir('foo')
61
62 test.write(['foo','foo.in'], r"""
63 foo you
64 """)
65
66 test.write('SConstruct', """
67 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
68 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
69 env.Tool('install')
70 env.MyBuild(target = 'f1', source = 'f1.in')
71 env.MyBuild(target = 'f2', source = 'f2.in')
72
73 def copyn(env, target, source):
74     import shutil
75     import time
76     time.sleep(1)
77     for t in target:
78         shutil.copy(str(source[0]), str(t))
79
80 t = env.Command(target=['foo/foo1.out', 'foo/foo2.out'],
81                 source='foo/foo.in',
82                 action=copyn)
83 env.Install('out', t)
84 """ % locals())
85
86 def RunTest(args, extra):
87     """extra is used to make scons rebuild the output file"""
88     test.write('f1.in', 'f1.in'+extra)
89     test.write('f2.in', 'f2.in'+extra)
90
91     test.run(arguments = args)
92
93     str = test.read("f1")
94     start1,finish1 = map(float, string.split(str, "\n"))
95
96     str = test.read("f2")
97     start2,finish2 = map(float, string.split(str, "\n"))
98
99     return start2, finish1
100
101 # Test 2 parallel jobs.
102 # fail if the second file was not started
103 # before the first one was finished.
104 start2, finish1 = RunTest('-j 2 f1 f2', "first")
105 test.fail_test(not (start2 < finish1))
106
107 # re-run the test with the same input, fail if we don't
108 # get back the same times, which would indicate that
109 # SCons rebuilt the files even though nothing changed
110 s2, f1 = RunTest('-j 2 f1 f2', "first")
111 test.fail_test(start2 != s2)
112 test.fail_test(finish1 != f1)
113
114 # Test a single serial job.
115 # fail if the second file was started
116 # before the first one was finished
117 start2, finish1 = RunTest('f1 f2', "second")
118 test.fail_test(start2 < finish1)
119
120 # Make sure that a parallel build using a list builder
121 # succeeds.
122 test.run(arguments='-j 2 out')
123
124
125 # Test that we fall back and warn properly if there's no threading.py
126 # module (simulated), which is the case if this version of Python wasn't
127 # built with threading support.
128
129 test.subdir('pythonlib')
130
131 test.write(['pythonlib', 'threading.py'], """\
132 raise ImportError
133 """)
134
135 save_pythonpath = os.environ.get('PYTHONPATH', '')
136 os.environ['PYTHONPATH'] = test.workpath('pythonlib')
137
138 #start2, finish1 = RunTest('-j 2 f1, f2', "fifth")
139
140 test.write('f1.in', 'f1.in pythonlib\n')
141 test.write('f2.in', 'f2.in pythonlib\n')
142
143 test.run(arguments = "-j 2 f1 f2", stderr=None)
144
145 warn = \
146 """scons: warning: parallel builds are unsupported by this version of Python;
147 \tignoring -j or num_jobs option.
148 """
149 test.must_contain_all_lines(test.stderr(), [warn])
150
151 str = test.read("f1")
152 start1,finish1 = map(float, string.split(str, "\n"))
153
154 str = test.read("f2")
155 start2,finish2 = map(float, string.split(str, "\n"))
156
157 test.fail_test(start2 < finish1)
158
159 os.environ['PYTHONPATH'] = save_pythonpath
160
161
162 # Test SetJobs() with no -j:
163 test.write('SConstruct', """
164 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
165 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
166 env.Tool('install')
167 env.MyBuild(target = 'f1', source = 'f1.in')
168 env.MyBuild(target = 'f2', source = 'f2.in')
169
170 def copyn(env, target, source):
171     import shutil
172     import time
173     time.sleep(1)
174     for t in target:
175         shutil.copy(str(source[0]), str(t))
176
177 t = env.Command(target=['foo/foo1.out', 'foo/foo2.out'], source='foo/foo.in', action=copyn)
178 env.Install('out', t)
179
180 assert GetOption('num_jobs') == 1
181 SetOption('num_jobs', 2)
182 assert GetOption('num_jobs') == 2
183 """ % locals())
184
185 # This should be a parallel build because the SConscript sets jobs to 2.
186 # fail if the second file was not started
187 # before the first one was finished
188 start2, finish1 = RunTest('f1 f2', "third")
189 test.fail_test(not (start2 < finish1))
190
191 # Test SetJobs() with -j:
192 test.write('SConstruct', """
193 MyBuild = Builder(action = r'%(_python_)s build.py $TARGETS')
194 env = Environment(BUILDERS = { 'MyBuild' : MyBuild })
195 env.Tool('install')
196 env.MyBuild(target = 'f1', source = 'f1.in')
197 env.MyBuild(target = 'f2', source = 'f2.in')
198
199 def copyn(env, target, source):
200     import shutil
201     import time
202     time.sleep(1)
203     for t in target:
204         shutil.copy(str(source[0]), str(t))
205
206 t = env.Command(target=['foo/foo1.out', 'foo/foo2.out'], source='foo/foo.in', action=copyn)
207 env.Install('out', t)
208
209 assert GetOption('num_jobs') == 1
210 SetOption('num_jobs', 2)
211 assert GetOption('num_jobs') == 1
212 """ % locals())
213
214 # This should be a serial build since -j 1 overrides the call to SetJobs().
215 # fail if the second file was started
216 # before the first one was finished
217 start2, finish1 = RunTest('-j 1 f1 f2', "fourth")
218 test.fail_test(start2 < finish1)
219
220
221
222 test.pass_test()
223
224 # Local Variables:
225 # tab-width:4
226 # indent-tabs-mode:nil
227 # End:
228 # vim: set expandtab tabstop=4 shiftwidth=4: