Run setup.py on the unpacked .tar.gz for testing.
[scons.git] / runtest.py
1 #!/usr/bin/env python
2 #
3 # runtests.py - wrapper script for running SCons tests
4 #
5 # This script mainly exists to set PYTHONPATH to the right list of
6 # directories to test the SCons modules.
7 #
8 # By default, it directly uses the modules in the local tree:
9 # ./src/ (source files we ship) and ./etc/ (other modules we don't)
10 #
11 # When "-b aegis" is specified, it assumes it's in a directory
12 # in which an Aegis build (aeb) has been performed, and sets
13 # PYTHONPATH so that it *only* references the modules that have
14 # unpacked from the built packages, to test whether the packages
15 # are good.
16 #
17 # Options:
18 #
19 #       -a              Run all tests; does a virtual 'find' for
20 #                       all SCons tests under the current directory.
21 #
22 #       -b system       Assume you're in the specified built system.
23 #                       'aegis' is the only one currently defined.
24 #
25 #       -d              Debug.  Runs the script under the Python
26 #                       debugger (pdb.py) so you don't have to
27 #                       muck with PYTHONPATH yourself.
28 #
29 #       -q              Quiet.  By default, runtest.py prints the
30 #                       command line it will execute before
31 #                       executing it.  This suppresses that print.
32 #
33 #       -v              Version.  Specifies the version number to
34 #                       be used for Aegis interaction.
35 #
36
37 import getopt
38 import os
39 import os.path
40 import re
41 import string
42 import sys
43
44 all = 0
45 build = None
46 debug = ''
47 tests = []
48 printcmd = 1
49 version = None
50
51 opts, tests = getopt.getopt(sys.argv[1:], "ab:dqv:",
52                             ['all','build=','debug','quiet','version='])
53
54 for o, a in opts:
55     if o == '-a' or o == '--all': all = 1
56     elif o == '-b' or o == '--build': build = a
57     elif o == '-d' or o == '--debug': debug = os.path.join(
58                                         sys.exec_prefix,
59                                         "lib",
60                                         "python" + sys.version[0:3],
61                                         "pdb.py")
62     elif o == '-q' or o == '--quiet': printcmd = 0
63     elif o == '-v' or o == '--version': version = a
64
65 cwd = os.getcwd()
66
67 if tests:
68     map(os.path.abspath, tests)
69 elif all:
70     def find_Test_py(arg, dirname, names):
71         global tests
72         n = filter(lambda n: n[-8:] == "Tests.py", names)
73         n = map(lambda x,d=dirname: os.path.join(d, x), n)
74         tests = tests + n
75     os.path.walk('src', find_Test_py, 0)
76
77     def find_py(arg, dirname, names):
78         global tests
79         n = filter(lambda n: n[-3:] == ".py", names)
80         n = map(lambda x,d=dirname: os.path.join(d, x), n)
81         tests = tests + n
82     os.path.walk('test', find_py, 0)
83
84 if build == 'aegis':
85     if not version:
86         version = os.popen("aesub '$version'").read()[:-1]
87
88     match = re.compile(r'^[CD]0*')
89
90     def aegis_to_version(aever):
91         arr = string.split(aever, '.')
92         end = max(len(arr) - 1, 2)
93         arr = map(lambda e: match.sub('', e), arr[:end])
94         def rep(e):
95             if len(e) == 1:
96                 e = '0' + e
97             return e
98         arr[1:] = map(rep, arr[1:])
99         return string.join(arr, '.')
100
101     version = aegis_to_version(version)
102
103     scons_dir = os.path.join(cwd, 'build', 'test', 'bin')
104
105     os.environ['PYTHONPATH'] = os.path.join(cwd,
106                                             'build',
107                                             'test',
108                                             'lib',
109                                             'python' + sys.version[0:3],
110                                             'site-packages')
111
112 else:
113
114     scons_dir = os.path.join(cwd, 'src', 'script')
115
116     os.environ['PYTHONPATH'] = string.join([os.path.join(cwd, 'src', 'engine'),
117                                             os.path.join(cwd, 'etc')],
118                                            os.pathsep)
119
120 os.chdir(scons_dir)
121
122 fail = []
123
124 for path in tests:
125     if os.path.isabs(path):
126         abs = path
127     else:
128         abs = os.path.join(cwd, path)
129     cmd = string.join(["python", debug, abs], " ")
130     if printcmd:
131         print cmd
132     if os.system(cmd):
133         fail.append(path)
134
135 sys.exit(len(fail))