Added fix for TeX includes with same name as subdirs.
[scons.git] / bin / calibrate.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2009 The SCons Foundation
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 import optparse
25 import os
26 import re
27 import subprocess
28 import sys
29
30 variable_re = re.compile('^VARIABLE: (.*)$', re.M)
31 elapsed_re = re.compile('^ELAPSED: (.*)$', re.M)
32
33 def main(argv=None):
34     if argv is None:
35         argv = sys.argv
36
37     parser = optparse.OptionParser(usage="calibrate.py [-h] [-p PACKAGE], [--min time] [--max time] timings/*/*-run.py")
38     parser.add_option('--min', type='float', default=9.5,
39                       help="minimum acceptable execution time (default 9.5)")
40     parser.add_option('--max', type='float', default=10.00,
41                       help="maximum acceptable execution time (default 10.00)")
42     parser.add_option('-p', '--package', type="string",
43                       help="package type")
44     opts, args = parser.parse_args(argv[1:])
45
46     os.environ['TIMESCONS_CALIBRATE'] = '1'
47
48     for arg in args:
49         if len(args) > 1:
50             print arg + ':'
51
52         command = [sys.executable, 'runtest.py', '--noqmtest']
53         if opts.package:
54             command.extend(['-p', opts.package])
55         command.append(arg)
56
57         run = 1
58         good = 0
59         while good < 3:
60             p = subprocess.Popen(command,
61                                  stdout=subprocess.PIPE,
62                                  stderr=subprocess.STDOUT)
63             output = p.communicate()[0]
64             vm = variable_re.search(output)
65             em = elapsed_re.search(output)
66             elapsed = float(em.group(1))
67             print "run %3d: %7.3f:  %s" % (run, elapsed, ' '.join(vm.groups()))
68             if opts.min < elapsed and elapsed < opts.max:
69                 good += 1
70             else:
71                 good = 0
72                 for v in vm.groups():
73                     var, value = v.split('=', 1)
74                     value = int((int(value) * opts.max) / elapsed)
75                     os.environ[var] = str(value)
76             run += 1
77
78     return 0
79
80 if __name__ == "__main__":
81     sys.exit(main())