Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: native_client_sdk/src/tools/run.py

Issue 720233003: [NaCl SDK] Convert python scripts from optparse to argparse. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
9 This command creates a local server on an ephemeral port, then runs:
binji 2014/11/13 23:57:05 not true anymore. 5103 is the default
Sam Clegg 2014/11/30 17:55:14 Done.
10 <executable> <args..> http://localhost:<port>/<page>.
11
12 Where <page> can be set by -P, or uses index.html by default.
8 """ 13 """
9 14
10 import copy 15 import copy
11 import getos 16 import getos
12 import optparse 17 import argparse
binji 2014/11/13 23:57:04 sort
Sam Clegg 2014/11/30 17:55:14 Done.
13 import os 18 import os
14 import subprocess 19 import subprocess
15 import sys 20 import sys
16 import httpd 21 import httpd
17 22
18 23
19 if sys.version_info < (2, 6, 0): 24 if sys.version_info < (2, 6, 0):
binji 2014/11/13 23:57:04 2.7
Sam Clegg 2014/11/30 17:55:14 Done.
20 sys.stderr.write("python 2.6 or later is required run this script\n") 25 sys.stderr.write("python 2.6 or later is required run this script\n")
21 sys.exit(1) 26 sys.exit(1)
22 27
23 28
24 def main(args): 29 def main(args):
25 usage = """usage: %prog [options] -- executable args... 30 parser = argparse.ArgumentParser(description=__doc__)
26 31 parser.add_argument('-C', '--serve-dir',
27 This command creates a local server on an ephemeral port, then runs:
28 <executable> <args..> http://localhost:<port>/<page>.
29
30 Where <page> can be set by -P, or uses index.html by default."""
31 parser = optparse.OptionParser(usage)
32 parser.add_option('-C', '--serve-dir',
33 help='Serve files out of this directory.', 32 help='Serve files out of this directory.',
34 dest='serve_dir', default=os.path.abspath('.')) 33 dest='serve_dir', default=os.path.abspath('.'))
35 parser.add_option('-P', '--path', help='Path to load from local server.', 34 parser.add_argument('-P', '--path', help='Path to load from local server.',
36 dest='path', default='index.html') 35 dest='path', default='index.html')
37 parser.add_option('-D', 36 parser.add_argument('-D',
38 help='Add debug command-line when launching the chrome debug.', 37 help='Add debug command-line when launching the chrome debug.',
39 dest='debug', action='append', default=[]) 38 dest='debug', action='append', default=[])
40 parser.add_option('-E', 39 parser.add_argument('-E',
41 help='Add environment variables when launching the executable.', 40 help='Add environment variables when launching the executable.',
42 dest='environ', action='append', default=[]) 41 dest='environ', action='append', default=[])
43 parser.add_option('-p', '--port', 42 parser.add_argument('-p', '--port',
44 help='Port to run server on. Default is 5103, ephemeral is 0.', 43 help='Port to run server on. Default is 5103, ephemeral is 0.',
45 type='int', default=5103) 44 type=int, default=5103)
46 options, args = parser.parse_args(args) 45 parser.add_argument('args', nargs='+', help='arguments for executable')
binji 2014/11/13 23:57:05 this should probably be separated into two args: e
Sam Clegg 2014/11/30 17:55:14 Done.
47 if not args: 46 options = parser.parse_args(args)
48 parser.error('No executable given.')
49 47
50 # 0 means use an ephemeral port. 48 # 0 means use an ephemeral port.
51 server = httpd.LocalHTTPServer(options.serve_dir, options.port) 49 server = httpd.LocalHTTPServer(options.serve_dir, options.port)
52 print 'Serving %s on %s...' % (options.serve_dir, server.GetURL('')) 50 print 'Serving %s on %s...' % (options.serve_dir, server.GetURL(''))
53 51
54 env = copy.copy(os.environ) 52 env = copy.copy(os.environ)
55 for e in options.environ: 53 for e in options.environ:
56 key, value = map(str.strip, e.split('=')) 54 key, value = map(str.strip, e.split('='))
57 env[key] = value 55 env[key] = value
58 56
59 cmd = args + [server.GetURL(options.path)] 57 cmd = options.args + [server.GetURL(options.path)]
60 print 'Running: %s...' % (' '.join(cmd),) 58 print 'Running: %s...' % (' '.join(cmd),)
61 process = subprocess.Popen(cmd, env=env) 59 process = subprocess.Popen(cmd, env=env)
62 60
63 # If any debug args are passed in, assume we want to debug 61 # If any debug args are passed in, assume we want to debug
64 if options.debug: 62 if options.debug:
65 if getos.GetPlatform() == 'linux': 63 if getos.GetPlatform() == 'linux':
66 cmd = ['xterm', '-title', 'NaCl Debugger', '-e'] 64 cmd = ['xterm', '-title', 'NaCl Debugger', '-e']
67 cmd += options.debug 65 cmd += options.debug
68 elif getos.GetPlatform() == 'mac': 66 elif getos.GetPlatform() == 'mac':
69 cmd = ['osascript', '-e', 67 cmd = ['osascript', '-e',
(...skipping 10 matching lines...) Expand all
80 try: 78 try:
81 return server.ServeUntilSubprocessDies(process) 79 return server.ServeUntilSubprocessDies(process)
82 finally: 80 finally:
83 if process.returncode is None: 81 if process.returncode is None:
84 process.kill() 82 process.kill()
85 if debug_process and debug_process.returncode is None: 83 if debug_process and debug_process.returncode is None:
86 debug_process.kill() 84 debug_process.kill()
87 85
88 if __name__ == '__main__': 86 if __name__ == '__main__':
89 sys.exit(main(sys.argv[1:])) 87 sys.exit(main(sys.argv[1:]))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698