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

Side by Side Diff: mojo/devtools/common/devtoolslib/shell_arguments.py

Issue 1419333005: Add `mojo_run --free-ports` to run servers on system-allocated ports. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 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
« no previous file with comments | « no previous file | mojo/devtools/common/devtoolslib/shell_config.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2015 The Chromium Authors. All rights reserved. 1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Produces configured shell abstractions. 5 """Produces configured shell abstractions.
6 6
7 This module knows how to produce a configured shell abstraction based on 7 This module knows how to produce a configured shell abstraction based on
8 shell_config.ShellConfig. 8 shell_config.ShellConfig.
9 """ 9 """
10 10
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 if _is_web_url(parts[1]): 60 if _is_web_url(parts[1]):
61 # The destination is a web url, do nothing. 61 # The destination is a web url, do nothing.
62 return mapping 62 return mapping
63 63
64 src = parts[0] 64 src = parts[0]
65 dest = host_destination_functon(shell, parts[1], port, free_host_port) 65 dest = host_destination_functon(shell, parts[1], port, free_host_port)
66 return src + '=' + dest 66 return src + '=' + dest
67 67
68 68
69 def _apply_mappings(shell, original_arguments, map_urls, map_origins, 69 def _apply_mappings(shell, original_arguments, map_urls, map_origins,
70 free_host_ports): 70 free_ports, free_host_ports):
71 """Applies mappings for specified urls and origins. For each local path 71 """Applies mappings for specified urls and origins. For each local path
72 specified as destination a local server will be spawned and the mapping will 72 specified as destination a local server will be spawned and the mapping will
73 be rewritten accordingly. 73 be rewritten accordingly.
74 74
75 Args: 75 Args:
76 shell: The shell that is being configured. 76 shell: The shell that is being configured.
77 original_arguments: Current list of shell arguments. 77 original_arguments: Current list of shell arguments.
78 map_urls: List of url mappings, each in the form of 78 map_urls: List of url mappings, each in the form of
79 <url>=<url-or-local-path>. 79 <url>=<url-or-local-path>.
80 map_origins: List of origin mappings, each in the form of 80 map_origins: List of origin mappings, each in the form of
81 <origin>=<url-or-local-path>. 81 <origin>=<url-or-local-path>.
qsr 2015/11/02 10:53:16 Can you comment on free_ports and free_hosts_ports
ppi 2015/11/02 13:04:17 Done.
82 82
83 Returns: 83 Returns:
84 The updated argument list. 84 The updated argument list.
85 """ 85 """
86 next_port = _MAPPINGS_BASE_PORT 86 next_port = 0 if free_ports else _MAPPINGS_BASE_PORT
87 args = original_arguments 87 args = original_arguments
88 if map_urls: 88 if map_urls:
89 # Sort the mappings to preserve caching regardless of argument order. 89 # Sort the mappings to preserve caching regardless of argument order.
90 for map_url in sorted(map_urls): 90 for map_url in sorted(map_urls):
91 mapping = _rewrite(map_url, _host_local_url_destination, shell, next_port, 91 mapping = _rewrite(map_url, _host_local_url_destination, shell, next_port,
92 free_host_ports) 92 free_host_ports)
93 next_port += 1 93 if not free_ports:
94 next_port += 1
94 # All url mappings need to be coalesced into one shell argument. 95 # All url mappings need to be coalesced into one shell argument.
95 args = append_to_argument(args, '--url-mappings=', mapping) 96 args = append_to_argument(args, '--url-mappings=', mapping)
96 97
97 if map_origins: 98 if map_origins:
98 for map_origin in sorted(map_origins): 99 for map_origin in sorted(map_origins):
99 mapping = _rewrite(map_origin, _host_local_origin_destination, shell, 100 mapping = _rewrite(map_origin, _host_local_origin_destination, shell,
100 next_port, free_host_ports) 101 next_port, free_host_ports)
101 next_port += 1 102 if not free_ports:
103 next_port += 1
102 # Origin mappings are specified as separate, repeated shell arguments. 104 # Origin mappings are specified as separate, repeated shell arguments.
103 args.append('--map-origin=' + mapping) 105 args.append('--map-origin=' + mapping)
104 return args 106 return args
105 107
106 108
107 def configure_local_origin(shell, local_dir, free_host_port): 109 def configure_local_origin(shell, local_dir, port, free_host_port):
108 """Sets up a local http server to serve files in |local_dir| along with 110 """Sets up a local http server to serve files in |local_dir| along with
109 device port forwarding if needed. 111 device port forwarding if needed.
110 112
111 Returns: 113 Returns:
112 The list of arguments to be appended to the shell argument list. 114 The list of arguments to be appended to the shell argument list.
113 """ 115 """
114 116
115 origin_url = shell.serve_local_directory( 117 origin_url = shell.serve_local_directory(local_dir, port, free_host_port)
116 local_dir, _LOCAL_ORIGIN_PORT, free_host_port)
117 return ["--origin=" + origin_url] 118 return ["--origin=" + origin_url]
118 119
119 120
120 def append_to_argument(arguments, key, value, delimiter=","): 121 def append_to_argument(arguments, key, value, delimiter=","):
121 """Looks for an argument of the form "key=val1,val2" within |arguments| and 122 """Looks for an argument of the form "key=val1,val2" within |arguments| and
122 appends |value| to it. 123 appends |value| to it.
123 124
124 If the argument is not present in |arguments| it is added. 125 If the argument is not present in |arguments| it is added.
125 126
126 Args: 127 Args:
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 else: 209 else:
209 if not shell_config.shell_path: 210 if not shell_config.shell_path:
210 raise ShellConfigurationException('Can not run without a shell binary. ' 211 raise ShellConfigurationException('Can not run without a shell binary. '
211 'Please pass --shell-path.') 212 'Please pass --shell-path.')
212 shell = LinuxShell(shell_config.shell_path) 213 shell = LinuxShell(shell_config.shell_path)
213 if shell_config.use_osmesa: 214 if shell_config.use_osmesa:
214 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa') 215 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa')
215 216
216 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list, 217 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list,
217 shell_config.map_origin_list, 218 shell_config.map_origin_list,
219 shell_config.free_ports,
218 shell_config.free_host_ports) 220 shell_config.free_host_ports)
219 221
220 if shell_config.origin: 222 if shell_config.origin:
221 if _is_web_url(shell_config.origin): 223 if _is_web_url(shell_config.origin):
222 shell_args.append('--origin=' + shell_config.origin) 224 shell_args.append('--origin=' + shell_config.origin)
223 else: 225 else:
226 local_origin_port = 0 if shell_config.free_ports else _LOCAL_ORIGIN_PORT
224 shell_args.extend(configure_local_origin(shell, shell_config.origin, 227 shell_args.extend(configure_local_origin(shell, shell_config.origin,
228 local_origin_port,
225 shell_config.free_host_ports)) 229 shell_config.free_host_ports))
226 230
227 if shell_config.content_handlers: 231 if shell_config.content_handlers:
228 for (mime_type, 232 for (mime_type,
229 content_handler_url) in shell_config.content_handlers.iteritems(): 233 content_handler_url) in shell_config.content_handlers.iteritems():
230 shell_args = append_to_argument(shell_args, '--content-handlers=', 234 shell_args = append_to_argument(shell_args, '--content-handlers=',
231 '%s,%s' % (mime_type, 235 '%s,%s' % (mime_type,
232 content_handler_url)) 236 content_handler_url))
233 237
234 for dev_server_config in shell_config.dev_servers: 238 for dev_server_config in shell_config.dev_servers:
235 shell_args = _configure_dev_server(shell, shell_args, dev_server_config, 239 shell_args = _configure_dev_server(shell, shell_args, dev_server_config,
236 shell_config.free_host_ports, 240 shell_config.free_host_ports,
237 shell_config.verbose) 241 shell_config.verbose)
238 242
239 return shell, shell_args 243 return shell, shell_args
OLDNEW
« no previous file with comments | « no previous file | mojo/devtools/common/devtoolslib/shell_config.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698