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 if options.apitrace_source:
111 return os.path.join(options.apitrace_source, 'scripts')
113 bin_path = get_bin_path()
118 '../lib/apitrace/scripts',
121 for try_path in try_paths:
122 path = os.path.join(bin_path, try_path)
123 if os.path.exists(path):
124 return os.path.abspath(path)
126 sys.stderr.write('error: could not find scripts directory\n')
135 def createOptParser(self):
136 default_apitrace = 'apitrace'
137 if platform.system() == 'Windows':
138 default_apitrace += '.exe'
140 # Parse command line options
141 optparser = optparse.OptionParser(
142 usage='\n\t%prog [OPTIONS] -- [ARGS] ...',
144 optparser.add_option(
147 dest="verbose", default=False,
148 help="verbose output")
149 optparser.add_option(
150 '--apitrace', metavar='PROGRAM',
151 type='string', dest='apitrace', default=default_apitrace,
152 help='path to apitrace executable')
153 optparser.add_option(
154 '--apitrace-source', metavar='PATH',
155 type='string', dest='apitrace_source',
156 help='path to apitrace source tree')
157 optparser.add_option(
158 '-C', '--directory', metavar='PATH',
159 type='string', dest='cwd', default=None,
160 help='change to directory')
164 def parseOptions(self):
167 optparser = self.createOptParser()
168 (options, args) = optparser.parse_args(sys.argv[1:])
170 optparser.error('an argument must be specified')
172 print get_scripts_path()
174 sys.path.insert(0, get_scripts_path())
176 self.options = options
182 raise NotImplementedError