media/webrtc/trunk/tools/gyp/gyptest.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2
michael@0 3 # Copyright (c) 2012 Google Inc. All rights reserved.
michael@0 4 # Use of this source code is governed by a BSD-style license that can be
michael@0 5 # found in the LICENSE file.
michael@0 6
michael@0 7 __doc__ = """
michael@0 8 gyptest.py -- test runner for GYP tests.
michael@0 9 """
michael@0 10
michael@0 11 import os
michael@0 12 import optparse
michael@0 13 import subprocess
michael@0 14 import sys
michael@0 15
michael@0 16 class CommandRunner:
michael@0 17 """
michael@0 18 Executor class for commands, including "commands" implemented by
michael@0 19 Python functions.
michael@0 20 """
michael@0 21 verbose = True
michael@0 22 active = True
michael@0 23
michael@0 24 def __init__(self, dictionary={}):
michael@0 25 self.subst_dictionary(dictionary)
michael@0 26
michael@0 27 def subst_dictionary(self, dictionary):
michael@0 28 self._subst_dictionary = dictionary
michael@0 29
michael@0 30 def subst(self, string, dictionary=None):
michael@0 31 """
michael@0 32 Substitutes (via the format operator) the values in the specified
michael@0 33 dictionary into the specified command.
michael@0 34
michael@0 35 The command can be an (action, string) tuple. In all cases, we
michael@0 36 perform substitution on strings and don't worry if something isn't
michael@0 37 a string. (It's probably a Python function to be executed.)
michael@0 38 """
michael@0 39 if dictionary is None:
michael@0 40 dictionary = self._subst_dictionary
michael@0 41 if dictionary:
michael@0 42 try:
michael@0 43 string = string % dictionary
michael@0 44 except TypeError:
michael@0 45 pass
michael@0 46 return string
michael@0 47
michael@0 48 def display(self, command, stdout=None, stderr=None):
michael@0 49 if not self.verbose:
michael@0 50 return
michael@0 51 if type(command) == type(()):
michael@0 52 func = command[0]
michael@0 53 args = command[1:]
michael@0 54 s = '%s(%s)' % (func.__name__, ', '.join(map(repr, args)))
michael@0 55 if type(command) == type([]):
michael@0 56 # TODO: quote arguments containing spaces
michael@0 57 # TODO: handle meta characters?
michael@0 58 s = ' '.join(command)
michael@0 59 else:
michael@0 60 s = self.subst(command)
michael@0 61 if not s.endswith('\n'):
michael@0 62 s += '\n'
michael@0 63 sys.stdout.write(s)
michael@0 64 sys.stdout.flush()
michael@0 65
michael@0 66 def execute(self, command, stdout=None, stderr=None):
michael@0 67 """
michael@0 68 Executes a single command.
michael@0 69 """
michael@0 70 if not self.active:
michael@0 71 return 0
michael@0 72 if type(command) == type(''):
michael@0 73 command = self.subst(command)
michael@0 74 cmdargs = shlex.split(command)
michael@0 75 if cmdargs[0] == 'cd':
michael@0 76 command = (os.chdir,) + tuple(cmdargs[1:])
michael@0 77 if type(command) == type(()):
michael@0 78 func = command[0]
michael@0 79 args = command[1:]
michael@0 80 return func(*args)
michael@0 81 else:
michael@0 82 if stdout is sys.stdout:
michael@0 83 # Same as passing sys.stdout, except python2.4 doesn't fail on it.
michael@0 84 subout = None
michael@0 85 else:
michael@0 86 # Open pipe for anything else so Popen works on python2.4.
michael@0 87 subout = subprocess.PIPE
michael@0 88 if stderr is sys.stderr:
michael@0 89 # Same as passing sys.stderr, except python2.4 doesn't fail on it.
michael@0 90 suberr = None
michael@0 91 elif stderr is None:
michael@0 92 # Merge with stdout if stderr isn't specified.
michael@0 93 suberr = subprocess.STDOUT
michael@0 94 else:
michael@0 95 # Open pipe for anything else so Popen works on python2.4.
michael@0 96 suberr = subprocess.PIPE
michael@0 97 p = subprocess.Popen(command,
michael@0 98 shell=(sys.platform == 'win32'),
michael@0 99 stdout=subout,
michael@0 100 stderr=suberr)
michael@0 101 p.wait()
michael@0 102 if stdout is None:
michael@0 103 self.stdout = p.stdout.read()
michael@0 104 elif stdout is not sys.stdout:
michael@0 105 stdout.write(p.stdout.read())
michael@0 106 if stderr not in (None, sys.stderr):
michael@0 107 stderr.write(p.stderr.read())
michael@0 108 return p.returncode
michael@0 109
michael@0 110 def run(self, command, display=None, stdout=None, stderr=None):
michael@0 111 """
michael@0 112 Runs a single command, displaying it first.
michael@0 113 """
michael@0 114 if display is None:
michael@0 115 display = command
michael@0 116 self.display(display)
michael@0 117 return self.execute(command, stdout, stderr)
michael@0 118
michael@0 119
michael@0 120 class Unbuffered:
michael@0 121 def __init__(self, fp):
michael@0 122 self.fp = fp
michael@0 123 def write(self, arg):
michael@0 124 self.fp.write(arg)
michael@0 125 self.fp.flush()
michael@0 126 def __getattr__(self, attr):
michael@0 127 return getattr(self.fp, attr)
michael@0 128
michael@0 129 sys.stdout = Unbuffered(sys.stdout)
michael@0 130 sys.stderr = Unbuffered(sys.stderr)
michael@0 131
michael@0 132
michael@0 133 def find_all_gyptest_files(directory):
michael@0 134 result = []
michael@0 135 for root, dirs, files in os.walk(directory):
michael@0 136 if '.svn' in dirs:
michael@0 137 dirs.remove('.svn')
michael@0 138 result.extend([ os.path.join(root, f) for f in files
michael@0 139 if f.startswith('gyptest') and f.endswith('.py') ])
michael@0 140 result.sort()
michael@0 141 return result
michael@0 142
michael@0 143
michael@0 144 def main(argv=None):
michael@0 145 if argv is None:
michael@0 146 argv = sys.argv
michael@0 147
michael@0 148 usage = "gyptest.py [-ahlnq] [-f formats] [test ...]"
michael@0 149 parser = optparse.OptionParser(usage=usage)
michael@0 150 parser.add_option("-a", "--all", action="store_true",
michael@0 151 help="run all tests")
michael@0 152 parser.add_option("-C", "--chdir", action="store", default=None,
michael@0 153 help="chdir to the specified directory")
michael@0 154 parser.add_option("-f", "--format", action="store", default='',
michael@0 155 help="run tests with the specified formats")
michael@0 156 parser.add_option("-G", '--gyp_option', action="append", default=[],
michael@0 157 help="Add -G options to the gyp command line")
michael@0 158 parser.add_option("-l", "--list", action="store_true",
michael@0 159 help="list available tests and exit")
michael@0 160 parser.add_option("-n", "--no-exec", action="store_true",
michael@0 161 help="no execute, just print the command line")
michael@0 162 parser.add_option("--passed", action="store_true",
michael@0 163 help="report passed tests")
michael@0 164 parser.add_option("--path", action="append", default=[],
michael@0 165 help="additional $PATH directory")
michael@0 166 parser.add_option("-q", "--quiet", action="store_true",
michael@0 167 help="quiet, don't print test command lines")
michael@0 168 opts, args = parser.parse_args(argv[1:])
michael@0 169
michael@0 170 if opts.chdir:
michael@0 171 os.chdir(opts.chdir)
michael@0 172
michael@0 173 if opts.path:
michael@0 174 extra_path = [os.path.abspath(p) for p in opts.path]
michael@0 175 extra_path = os.pathsep.join(extra_path)
michael@0 176 os.environ['PATH'] += os.pathsep + extra_path
michael@0 177
michael@0 178 if not args:
michael@0 179 if not opts.all:
michael@0 180 sys.stderr.write('Specify -a to get all tests.\n')
michael@0 181 return 1
michael@0 182 args = ['test']
michael@0 183
michael@0 184 tests = []
michael@0 185 for arg in args:
michael@0 186 if os.path.isdir(arg):
michael@0 187 tests.extend(find_all_gyptest_files(os.path.normpath(arg)))
michael@0 188 else:
michael@0 189 tests.append(arg)
michael@0 190
michael@0 191 if opts.list:
michael@0 192 for test in tests:
michael@0 193 print test
michael@0 194 sys.exit(0)
michael@0 195
michael@0 196 CommandRunner.verbose = not opts.quiet
michael@0 197 CommandRunner.active = not opts.no_exec
michael@0 198 cr = CommandRunner()
michael@0 199
michael@0 200 os.environ['PYTHONPATH'] = os.path.abspath('test/lib')
michael@0 201 if not opts.quiet:
michael@0 202 sys.stdout.write('PYTHONPATH=%s\n' % os.environ['PYTHONPATH'])
michael@0 203
michael@0 204 passed = []
michael@0 205 failed = []
michael@0 206 no_result = []
michael@0 207
michael@0 208 if opts.format:
michael@0 209 format_list = opts.format.split(',')
michael@0 210 else:
michael@0 211 # TODO: not duplicate this mapping from pylib/gyp/__init__.py
michael@0 212 format_list = {
michael@0 213 'freebsd7': ['make'],
michael@0 214 'freebsd8': ['make'],
michael@0 215 'cygwin': ['msvs'],
michael@0 216 'win32': ['msvs', 'ninja'],
michael@0 217 'linux2': ['make', 'ninja'],
michael@0 218 'linux3': ['make', 'ninja'],
michael@0 219 'darwin': ['make', 'ninja', 'xcode'],
michael@0 220 }[sys.platform]
michael@0 221
michael@0 222 for format in format_list:
michael@0 223 os.environ['TESTGYP_FORMAT'] = format
michael@0 224 if not opts.quiet:
michael@0 225 sys.stdout.write('TESTGYP_FORMAT=%s\n' % format)
michael@0 226
michael@0 227 gyp_options = []
michael@0 228 for option in opts.gyp_option:
michael@0 229 gyp_options += ['-G', option]
michael@0 230 if gyp_options and not opts.quiet:
michael@0 231 sys.stdout.write('Extra Gyp options: %s\n' % gyp_options)
michael@0 232
michael@0 233 for test in tests:
michael@0 234 status = cr.run([sys.executable, test] + gyp_options,
michael@0 235 stdout=sys.stdout,
michael@0 236 stderr=sys.stderr)
michael@0 237 if status == 2:
michael@0 238 no_result.append(test)
michael@0 239 elif status:
michael@0 240 failed.append(test)
michael@0 241 else:
michael@0 242 passed.append(test)
michael@0 243
michael@0 244 if not opts.quiet:
michael@0 245 def report(description, tests):
michael@0 246 if tests:
michael@0 247 if len(tests) == 1:
michael@0 248 sys.stdout.write("\n%s the following test:\n" % description)
michael@0 249 else:
michael@0 250 fmt = "\n%s the following %d tests:\n"
michael@0 251 sys.stdout.write(fmt % (description, len(tests)))
michael@0 252 sys.stdout.write("\t" + "\n\t".join(tests) + "\n")
michael@0 253
michael@0 254 if opts.passed:
michael@0 255 report("Passed", passed)
michael@0 256 report("Failed", failed)
michael@0 257 report("No result from", no_result)
michael@0 258
michael@0 259 if failed:
michael@0 260 return 1
michael@0 261 else:
michael@0 262 return 0
michael@0 263
michael@0 264
michael@0 265 if __name__ == "__main__":
michael@0 266 sys.exit(main())

mercurial