Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / tools / run.py
blobb0131c5e428912556b60d9ed003ce979c41d313b
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Launch a local http server, then launch a executable directed at the server.
8 This command creates a local server (on port 5103 by default) then runs:
9 <executable> <args..> http://localhost:<port>/<page>.
11 Where <page> can be set by -P, or uses index.html by default.
12 """
14 import argparse
15 import copy
16 import getos
17 import os
18 import subprocess
19 import sys
20 import httpd
23 if sys.version_info < (2, 7, 0):
24 sys.stderr.write("python 2.7 or later is required run this script\n")
25 sys.exit(1)
28 def main(args):
29 parser = argparse.ArgumentParser(description=__doc__)
30 parser.add_argument('-C', '--serve-dir',
31 help='Serve files out of this directory.',
32 dest='serve_dir', default=os.path.abspath('.'))
33 parser.add_argument('-P', '--path', help='Path to load from local server.',
34 dest='path', default='index.html')
35 parser.add_argument('-D',
36 help='Add debug command-line when launching the chrome debug.',
37 dest='debug', action='append', default=[])
38 parser.add_argument('-E',
39 help='Add environment variables when launching the executable.',
40 dest='environ', action='append', default=[])
41 parser.add_argument('-p', '--port',
42 help='Port to run server on. Default is 5103, ephemeral is 0.',
43 type=int, default=5103)
44 parser.add_argument('executable', help='command to run')
45 parser.add_argument('args', nargs='*', help='arguments for executable')
46 options = parser.parse_args(args)
48 # 0 means use an ephemeral port.
49 server = httpd.LocalHTTPServer(options.serve_dir, options.port)
50 print 'Serving %s on %s...' % (options.serve_dir, server.GetURL(''))
52 env = copy.copy(os.environ)
53 for e in options.environ:
54 key, value = map(str.strip, e.split('='))
55 env[key] = value
57 cmd = [options.executable] + options.args + [server.GetURL(options.path)]
58 print 'Running: %s...' % (' '.join(cmd),)
59 process = subprocess.Popen(cmd, env=env)
61 # If any debug args are passed in, assume we want to debug
62 if options.debug:
63 if getos.GetPlatform() == 'linux':
64 cmd = ['xterm', '-title', 'NaCl Debugger', '-e']
65 cmd += options.debug
66 elif getos.GetPlatform() == 'mac':
67 cmd = ['osascript', '-e',
68 'tell application "Terminal" to do script "%s"' %
69 ' '.join(r'\"%s\"' % x for x in options.debug)]
70 elif getos.GetPlatform() == 'win':
71 cmd = ['cmd.exe', '/c', 'start', 'cmd.exe', '/c']
72 cmd += options.debug
73 print 'Starting debugger: ' + ' '.join(cmd)
74 debug_process = subprocess.Popen(cmd, env=env)
75 else:
76 debug_process = False
78 try:
79 return server.ServeUntilSubprocessDies(process)
80 finally:
81 if process.returncode is None:
82 process.kill()
83 if debug_process and debug_process.returncode is None:
84 debug_process.kill()
86 if __name__ == '__main__':
87 sys.exit(main(sys.argv[1:]))