OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Launch a local server on an ephemeral port, then launch a executable that | 6 """Launch a local server on an ephemeral port, then launch a executable that |
7 points to that server. | 7 points to that server. |
8 """ | 8 """ |
9 | 9 |
10 import copy | 10 import copy |
(...skipping 22 matching lines...) Expand all Loading... |
33 help='Serve files out of this directory.', | 33 help='Serve files out of this directory.', |
34 dest='serve_dir', default=os.path.abspath('.')) | 34 dest='serve_dir', default=os.path.abspath('.')) |
35 parser.add_option('-P', '--path', help='Path to load from local server.', | 35 parser.add_option('-P', '--path', help='Path to load from local server.', |
36 dest='path', default='index.html') | 36 dest='path', default='index.html') |
37 parser.add_option('-D', | 37 parser.add_option('-D', |
38 help='Add debug command-line when launching the chrome debug.', | 38 help='Add debug command-line when launching the chrome debug.', |
39 dest='debug', action='append', default=[]) | 39 dest='debug', action='append', default=[]) |
40 parser.add_option('-E', | 40 parser.add_option('-E', |
41 help='Add environment variables when launching the executable.', | 41 help='Add environment variables when launching the executable.', |
42 dest='environ', action='append', default=[]) | 42 dest='environ', action='append', default=[]) |
43 parser.add_option('--test-mode', | |
44 help='Listen for posts to /ok or /fail and shut down the server with ' | |
45 ' errorcodes 0 and 1 respectively.', | |
46 dest='test_mode', action='store_true') | |
47 parser.add_option('-p', '--port', | 43 parser.add_option('-p', '--port', |
48 help='Port to run server on. Default is 5103, ephemeral is 0.', | 44 help='Port to run server on. Default is 5103, ephemeral is 0.', |
49 type='int', default=5103) | 45 type='int', default=5103) |
50 options, args = parser.parse_args(args) | 46 options, args = parser.parse_args(args) |
51 if not args: | 47 if not args: |
52 parser.error('No executable given.') | 48 parser.error('No executable given.') |
53 | 49 |
54 # 0 means use an ephemeral port. | 50 # 0 means use an ephemeral port. |
55 server = httpd.LocalHTTPServer(options.serve_dir, options.port, | 51 server = httpd.LocalHTTPServer(options.serve_dir, options.port) |
56 options.test_mode) | |
57 print 'Serving %s on %s...' % (options.serve_dir, server.GetURL('')) | 52 print 'Serving %s on %s...' % (options.serve_dir, server.GetURL('')) |
58 | 53 |
59 env = copy.copy(os.environ) | 54 env = copy.copy(os.environ) |
60 for e in options.environ: | 55 for e in options.environ: |
61 key, value = map(str.strip, e.split('=')) | 56 key, value = map(str.strip, e.split('=')) |
62 env[key] = value | 57 env[key] = value |
63 | 58 |
64 cmd = args + [server.GetURL(options.path)] | 59 cmd = args + [server.GetURL(options.path)] |
65 print 'Running: %s...' % (' '.join(cmd),) | 60 print 'Running: %s...' % (' '.join(cmd),) |
66 process = subprocess.Popen(cmd, env=env) | 61 process = subprocess.Popen(cmd, env=env) |
(...skipping 18 matching lines...) Expand all Loading... |
85 try: | 80 try: |
86 return server.ServeUntilSubprocessDies(process) | 81 return server.ServeUntilSubprocessDies(process) |
87 finally: | 82 finally: |
88 if process.returncode is None: | 83 if process.returncode is None: |
89 process.kill() | 84 process.kill() |
90 if debug_process and debug_process.returncode is None: | 85 if debug_process and debug_process.returncode is None: |
91 debug_process.kill() | 86 debug_process.kill() |
92 | 87 |
93 if __name__ == '__main__': | 88 if __name__ == '__main__': |
94 sys.exit(main(sys.argv[1:])) | 89 sys.exit(main(sys.argv[1:])) |
OLD | NEW |