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

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: Address Ben's comments. 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>.
82 free_ports: Iff True, run local development servers on system-allocated
83 ports. This defeats any performance benefits from caching.
84 free_host_ports: Only applicable on Android. Iff True, local development
85 servers are run on system-allocated ports, but are still forwarded from
86 fixed ports on the device.
82 87
83 Returns: 88 Returns:
84 The updated argument list. 89 The updated argument list.
85 """ 90 """
86 next_port = _MAPPINGS_BASE_PORT 91 next_port = 0 if free_ports else _MAPPINGS_BASE_PORT
87 args = original_arguments 92 args = original_arguments
88 if map_urls: 93 if map_urls:
89 # Sort the mappings to preserve caching regardless of argument order. 94 # Sort the mappings to preserve caching regardless of argument order.
90 for map_url in sorted(map_urls): 95 for map_url in sorted(map_urls):
91 mapping = _rewrite(map_url, _host_local_url_destination, shell, next_port, 96 mapping = _rewrite(map_url, _host_local_url_destination, shell, next_port,
92 free_host_ports) 97 free_host_ports)
93 next_port += 1 98 if not free_ports:
99 next_port += 1
94 # All url mappings need to be coalesced into one shell argument. 100 # All url mappings need to be coalesced into one shell argument.
95 args = append_to_argument(args, '--url-mappings=', mapping) 101 args = append_to_argument(args, '--url-mappings=', mapping)
96 102
97 if map_origins: 103 if map_origins:
98 for map_origin in sorted(map_origins): 104 for map_origin in sorted(map_origins):
99 mapping = _rewrite(map_origin, _host_local_origin_destination, shell, 105 mapping = _rewrite(map_origin, _host_local_origin_destination, shell,
100 next_port, free_host_ports) 106 next_port, free_host_ports)
101 next_port += 1 107 if not free_ports:
108 next_port += 1
102 # Origin mappings are specified as separate, repeated shell arguments. 109 # Origin mappings are specified as separate, repeated shell arguments.
103 args.append('--map-origin=' + mapping) 110 args.append('--map-origin=' + mapping)
104 return args 111 return args
105 112
106 113
107 def configure_local_origin(shell, local_dir, free_host_port): 114 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 115 """Sets up a local http server to serve files in |local_dir| along with
109 device port forwarding if needed. 116 device port forwarding if needed.
110 117
111 Returns: 118 Returns:
112 The list of arguments to be appended to the shell argument list. 119 The list of arguments to be appended to the shell argument list.
113 """ 120 """
114 121
115 origin_url = shell.serve_local_directory( 122 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] 123 return ["--origin=" + origin_url]
118 124
119 125
120 def append_to_argument(arguments, key, value, delimiter=","): 126 def append_to_argument(arguments, key, value, delimiter=","):
121 """Looks for an argument of the form "key=val1,val2" within |arguments| and 127 """Looks for an argument of the form "key=val1,val2" within |arguments| and
122 appends |value| to it. 128 appends |value| to it.
123 129
124 If the argument is not present in |arguments| it is added. 130 If the argument is not present in |arguments| it is added.
125 131
126 Args: 132 Args:
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 else: 214 else:
209 if not shell_config.shell_path: 215 if not shell_config.shell_path:
210 raise ShellConfigurationException('Can not run without a shell binary. ' 216 raise ShellConfigurationException('Can not run without a shell binary. '
211 'Please pass --shell-path.') 217 'Please pass --shell-path.')
212 shell = LinuxShell(shell_config.shell_path) 218 shell = LinuxShell(shell_config.shell_path)
213 if shell_config.use_osmesa: 219 if shell_config.use_osmesa:
214 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa') 220 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa')
215 221
216 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list, 222 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list,
217 shell_config.map_origin_list, 223 shell_config.map_origin_list,
224 shell_config.free_ports,
218 shell_config.free_host_ports) 225 shell_config.free_host_ports)
219 226
220 if shell_config.origin: 227 if shell_config.origin:
221 if _is_web_url(shell_config.origin): 228 if _is_web_url(shell_config.origin):
222 shell_args.append('--origin=' + shell_config.origin) 229 shell_args.append('--origin=' + shell_config.origin)
223 else: 230 else:
231 local_origin_port = 0 if shell_config.free_ports else _LOCAL_ORIGIN_PORT
224 shell_args.extend(configure_local_origin(shell, shell_config.origin, 232 shell_args.extend(configure_local_origin(shell, shell_config.origin,
233 local_origin_port,
225 shell_config.free_host_ports)) 234 shell_config.free_host_ports))
226 235
227 if shell_config.content_handlers: 236 if shell_config.content_handlers:
228 for (mime_type, 237 for (mime_type,
229 content_handler_url) in shell_config.content_handlers.iteritems(): 238 content_handler_url) in shell_config.content_handlers.iteritems():
230 shell_args = append_to_argument(shell_args, '--content-handlers=', 239 shell_args = append_to_argument(shell_args, '--content-handlers=',
231 '%s,%s' % (mime_type, 240 '%s,%s' % (mime_type,
232 content_handler_url)) 241 content_handler_url))
233 242
234 for dev_server_config in shell_config.dev_servers: 243 for dev_server_config in shell_config.dev_servers:
235 shell_args = _configure_dev_server(shell, shell_args, dev_server_config, 244 shell_args = _configure_dev_server(shell, shell_args, dev_server_config,
236 shell_config.free_host_ports, 245 shell_config.free_host_ports,
237 shell_config.verbose) 246 shell_config.verbose)
238 247
239 return shell, shell_args 248 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