2 ##########################################################################
4 # Copyright 2011-2012 Jose Fonseca
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 ##########################################################################/
27 '''Common test driver code.'''
37 def _exit(status, code, reason=None):
41 reason = ' (%s)' % reason
42 sys.stdout.write('%s%s\n' % (status, reason))
45 def fail(reason=None):
46 _exit('FAIL', 1, reason)
48 def skip(reason=None):
49 _exit('SKIP', 0, reason)
51 def pass_(reason=None):
52 _exit('PASS', 0, reason)
55 def popen(command, *args, **kwargs):
56 if kwargs.get('cwd', None) is not None:
57 sys.stdout.write('cd %s && ' % kwargs['cwd'])
60 env = kwargs.pop('env')
68 if value != os.environ.get(name, None):
69 sys.stdout.write('%s=%s ' % (name, value))
70 env[name] = str(value)
72 sys.stdout.write(' '.join(command) + '\n')
75 return subprocess.Popen(command, *args, env=env, **kwargs)
78 def which(executable):
79 dirs = os.environ['PATH'].split(os.path.pathsep)
81 path = os.path.join(dir, executable)
82 if os.path.exists(path):
88 if os.path.exists(options.apitrace):
89 apitrace_abspath = os.path.abspath(options.apitrace)
91 apitrace_abspath = which(options.apitrace)
92 if apitrace_abspath is None:
93 sys.stderr.write('error: could not determine the absolute path of\n' % options.apitrace)
95 return os.path.dirname(apitrace_abspath)
98 def get_build_program(program):
99 bin_path = get_bin_path()
100 if platform.system() == 'Windows':
102 path = os.path.join(bin_path, program)
103 if not os.path.exists(path):
104 sys.stderr.write('error: %s does not exist\n' % path)
109 def get_scripts_path():
110 bin_path = get_bin_path()
115 '../lib/apitrace/scripts',
118 for try_path in try_paths:
119 path = os.path.join(bin_path, try_path)
120 if os.path.exists(path):
121 return os.path.abspath(path)
123 sys.stderr.write('error: could not find scripts directory\n')
132 def createOptParser(self):
133 default_apitrace = 'apitrace'
134 if platform.system() == 'Windows':
135 default_apitrace += '.exe'
137 # Parse command line options
138 optparser = optparse.OptionParser(
139 usage='\n\t%prog [OPTIONS] -- [ARGS] ...',
141 optparser.add_option(
144 dest="verbose", default=False,
145 help="verbose output")
146 optparser.add_option(
147 '--apitrace', metavar='PROGRAM',
148 type='string', dest='apitrace', default=default_apitrace,
149 help='path to apitrace executable')
150 optparser.add_option(
151 '-C', '--directory', metavar='PATH',
152 type='string', dest='cwd', default=None,
153 help='change to directory')
157 def parseOptions(self):
160 optparser = self.createOptParser()
161 (options, args) = optparser.parse_args(sys.argv[1:])
163 optparser.error('an argument must be specified')
165 print get_scripts_path()
167 sys.path.insert(0, get_scripts_path())
169 self.options = options
175 raise NotImplementedError