OLD | NEW |
---|---|
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 |
11 import os.path | 11 import os.path |
12 import sys | 12 import sys |
13 import urlparse | 13 import urlparse |
14 | 14 |
15 from devtoolslib.android_shell import AndroidShell | 15 from devtoolslib.android_shell import AndroidShell |
16 from devtoolslib.linux_shell import LinuxShell | 16 from devtoolslib.linux_shell import LinuxShell |
17 from devtoolslib.shell_config import ShellConfigurationException | 17 from devtoolslib.shell_config import ShellConfigurationException |
18 | 18 |
19 # When spinning up servers for local origins, we want to use predictable ports | 19 # When spinning up servers for local origins, we want to use predictable ports |
20 # so that caching works between subsequent runs with the same command line. | 20 # so that caching works between subsequent runs with the same command line. |
21 _LOCAL_ORIGIN_PORT = 31840 | 21 _LOCAL_ORIGIN_PORT = 31840 |
22 _MAPPINGS_BASE_PORT = 31841 | 22 _MAPPINGS_BASE_PORT = 31841 |
23 | 23 |
24 | 24 |
25 def _is_web_url(dest): | 25 def _is_web_url(dest): |
26 return True if urlparse.urlparse(dest).scheme else False | 26 return True if urlparse.urlparse(dest).scheme else False |
27 | 27 |
28 | 28 |
29 def _host_local_url_destination(shell, dest_file, port): | 29 def _host_local_url_destination(shell, dest_file, port, free_host_port): |
30 """Starts a local server to host |dest_file|. | 30 """Starts a local server to host |dest_file|. |
31 | 31 |
32 Returns: | 32 Returns: |
33 Url of the hosted file. | 33 Url of the hosted file. |
34 """ | 34 """ |
35 directory = os.path.dirname(dest_file) | 35 directory = os.path.dirname(dest_file) |
36 if not os.path.exists(directory): | 36 if not os.path.exists(directory): |
37 raise ValueError('local path passed as --map-url destination ' | 37 raise ValueError('local path passed as --map-url destination ' |
38 'does not exist') | 38 'does not exist') |
39 server_url = shell.serve_local_directory(directory, port) | 39 server_url = shell.serve_local_directory(directory, port, free_host_port) |
40 return server_url + os.path.relpath(dest_file, directory) | 40 return server_url + os.path.relpath(dest_file, directory) |
41 | 41 |
42 | 42 |
43 def _host_local_origin_destination(shell, dest_dir, port): | 43 def _host_local_origin_destination(shell, dest_dir, port, free_host_port): |
44 """Starts a local server to host |dest_dir|. | 44 """Starts a local server to host |dest_dir|. |
45 | 45 |
46 Returns: | 46 Returns: |
47 Url of the hosted directory. | 47 Url of the hosted directory. |
48 """ | 48 """ |
49 return shell.serve_local_directory(dest_dir, port) | 49 return shell.serve_local_directory(dest_dir, port, free_host_port) |
50 | 50 |
51 | 51 |
52 def _rewrite(mapping, host_destination_functon, shell, port): | 52 def _rewrite(mapping, host_destination_functon, shell, port, free_host_port): |
53 """Takes a mapping given as <src>=<dest> and rewrites the <dest> part to be | 53 """Takes a mapping given as <src>=<dest> and rewrites the <dest> part to be |
54 hosted locally using the given function if <dest> is not a web url. | 54 hosted locally using the given function if <dest> is not a web url. |
55 """ | 55 """ |
56 parts = mapping.split('=') | 56 parts = mapping.split('=') |
57 if len(parts) != 2: | 57 if len(parts) != 2: |
58 raise ValueError('each mapping value should be in format ' | 58 raise ValueError('each mapping value should be in format ' |
59 '"<url>=<url-or-local-path>"') | 59 '"<url>=<url-or-local-path>"') |
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) | 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 """Applies mappings for specified urls and origins. For each local path | 71 """Applies mappings for specified urls and origins. For each local path |
71 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 |
72 be rewritten accordingly. | 73 be rewritten accordingly. |
73 | 74 |
74 Args: | 75 Args: |
75 shell: The shell that is being configured. | 76 shell: The shell that is being configured. |
76 original_arguments: Current list of shell arguments. | 77 original_arguments: Current list of shell arguments. |
77 map_urls: List of url mappings, each in the form of | 78 map_urls: List of url mappings, each in the form of |
78 <url>=<url-or-local-path>. | 79 <url>=<url-or-local-path>. |
79 map_origins: List of origin mappings, each in the form of | 80 map_origins: List of origin mappings, each in the form of |
80 <origin>=<url-or-local-path>. | 81 <origin>=<url-or-local-path>. |
81 | 82 |
82 Returns: | 83 Returns: |
83 The updated argument list. | 84 The updated argument list. |
84 """ | 85 """ |
85 next_port = _MAPPINGS_BASE_PORT | 86 next_port = _MAPPINGS_BASE_PORT |
86 args = original_arguments | 87 args = original_arguments |
87 if map_urls: | 88 if map_urls: |
88 # Sort the mappings to preserve caching regardless of argument order. | 89 # Sort the mappings to preserve caching regardless of argument order. |
89 for map_url in sorted(map_urls): | 90 for map_url in sorted(map_urls): |
90 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) | |
91 next_port += 1 | 93 next_port += 1 |
92 # All url mappings need to be coalesced into one shell argument. | 94 # All url mappings need to be coalesced into one shell argument. |
93 args = append_to_argument(args, '--url-mappings=', mapping) | 95 args = append_to_argument(args, '--url-mappings=', mapping) |
94 | 96 |
95 if map_origins: | 97 if map_origins: |
96 for map_origin in sorted(map_origins): | 98 for map_origin in sorted(map_origins): |
97 mapping = _rewrite(map_origin, _host_local_origin_destination, shell, | 99 mapping = _rewrite(map_origin, _host_local_origin_destination, shell, |
98 next_port) | 100 next_port, free_host_ports) |
99 next_port += 1 | 101 next_port += 1 |
100 # Origin mappings are specified as separate, repeated shell arguments. | 102 # Origin mappings are specified as separate, repeated shell arguments. |
101 args.append('--map-origin=' + mapping) | 103 args.append('--map-origin=' + mapping) |
102 return args | 104 return args |
103 | 105 |
104 | 106 |
105 def configure_local_origin(shell, local_dir, fixed_port=True): | 107 def configure_local_origin(shell, local_dir, free_host_port): |
qsr
2015/10/14 07:35:39
Was someone using this? If no, no problem, but oth
ppi
2015/10/14 16:53:19
According to git grep, no-one was using this.
| |
106 """Sets up a local http server to serve files in |local_dir| along with | 108 """Sets up a local http server to serve files in |local_dir| along with |
107 device port forwarding if needed. | 109 device port forwarding if needed. |
108 | 110 |
109 Returns: | 111 Returns: |
110 The list of arguments to be appended to the shell argument list. | 112 The list of arguments to be appended to the shell argument list. |
111 """ | 113 """ |
112 | 114 |
113 origin_url = shell.serve_local_directory( | 115 origin_url = shell.serve_local_directory( |
114 local_dir, _LOCAL_ORIGIN_PORT if fixed_port else 0) | 116 local_dir, _LOCAL_ORIGIN_PORT, free_host_port) |
115 return ["--origin=" + origin_url] | 117 return ["--origin=" + origin_url] |
116 | 118 |
117 | 119 |
118 def append_to_argument(arguments, key, value, delimiter=","): | 120 def append_to_argument(arguments, key, value, delimiter=","): |
119 """Looks for an argument of the form "key=val1,val2" within |arguments| and | 121 """Looks for an argument of the form "key=val1,val2" within |arguments| and |
120 appends |value| to it. | 122 appends |value| to it. |
121 | 123 |
122 If the argument is not present in |arguments| it is added. | 124 If the argument is not present in |arguments| it is added. |
123 | 125 |
124 Args: | 126 Args: |
(...skipping 13 matching lines...) Expand all Loading... | |
138 if not argument.startswith(key): | 140 if not argument.startswith(key): |
139 continue | 141 continue |
140 arguments[i] = argument + delimiter + value | 142 arguments[i] = argument + delimiter + value |
141 break | 143 break |
142 else: | 144 else: |
143 arguments.append(key + value) | 145 arguments.append(key + value) |
144 | 146 |
145 return arguments | 147 return arguments |
146 | 148 |
147 | 149 |
148 def _configure_dev_server(shell, shell_args, dev_server_config, verbose): | 150 def _configure_dev_server(shell, shell_args, dev_server_config, free_host_port, |
151 verbose): | |
149 """Sets up a dev server on the host according to |dev_server_config|. | 152 """Sets up a dev server on the host according to |dev_server_config|. |
150 | 153 |
151 Args: | 154 Args: |
152 shell: The shell that is being configured. | 155 shell: The shell that is being configured. |
153 shell_arguments: Current list of shell arguments. | 156 shell_arguments: Current list of shell arguments. |
154 dev_server_config: Instance of shell_config.DevServerConfig describing the | 157 dev_server_config: Instance of shell_config.DevServerConfig describing the |
155 dev server to be set up. | 158 dev server to be set up. |
156 | 159 |
157 Returns: | 160 Returns: |
158 The updated argument list. | 161 The updated argument list. |
159 """ | 162 """ |
160 port = dev_server_config.port if dev_server_config.port else 0 | 163 port = dev_server_config.port if dev_server_config.port else 0 |
161 server_url = shell.serve_local_directories(dev_server_config.mappings, | 164 server_url = shell.serve_local_directories(dev_server_config.mappings, |
162 port=port) | 165 port=port, |
166 free_host_port=free_host_port) | |
163 shell_args.append('--map-origin=%s=%s' % (dev_server_config.host, server_url)) | 167 shell_args.append('--map-origin=%s=%s' % (dev_server_config.host, server_url)) |
164 | 168 |
165 if verbose: | 169 if verbose: |
166 print "Configured %s locally at %s to serve:" % (dev_server_config.host, | 170 print "Configured %s locally at %s to serve:" % (dev_server_config.host, |
167 server_url) | 171 server_url) |
168 for mapping_prefix, mapping_path in dev_server_config.mappings: | 172 for mapping_prefix, mapping_path in dev_server_config.mappings: |
169 print " /%s -> %s" % (mapping_prefix, mapping_path) | 173 print " /%s -> %s" % (mapping_prefix, mapping_path) |
170 return shell_args | 174 return shell_args |
171 | 175 |
172 | 176 |
(...skipping 30 matching lines...) Expand all Loading... | |
203 shell.install_apk(shell_config.shell_path) | 207 shell.install_apk(shell_config.shell_path) |
204 else: | 208 else: |
205 if not shell_config.shell_path: | 209 if not shell_config.shell_path: |
206 raise ShellConfigurationException('Can not run without a shell binary. ' | 210 raise ShellConfigurationException('Can not run without a shell binary. ' |
207 'Please pass --shell-path.') | 211 'Please pass --shell-path.') |
208 shell = LinuxShell(shell_config.shell_path) | 212 shell = LinuxShell(shell_config.shell_path) |
209 if shell_config.use_osmesa: | 213 if shell_config.use_osmesa: |
210 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa') | 214 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa') |
211 | 215 |
212 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list, | 216 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list, |
213 shell_config.map_origin_list) | 217 shell_config.map_origin_list, |
218 shell_config.free_host_ports) | |
214 | 219 |
215 if shell_config.origin: | 220 if shell_config.origin: |
216 if _is_web_url(shell_config.origin): | 221 if _is_web_url(shell_config.origin): |
217 shell_args.append('--origin=' + shell_config.origin) | 222 shell_args.append('--origin=' + shell_config.origin) |
218 else: | 223 else: |
219 shell_args.extend(configure_local_origin(shell, shell_config.origin, | 224 shell_args.extend(configure_local_origin(shell, shell_config.origin, |
220 fixed_port=True)) | 225 shell_config.free_host_ports)) |
221 | 226 |
222 if shell_config.content_handlers: | 227 if shell_config.content_handlers: |
223 for (mime_type, | 228 for (mime_type, |
224 content_handler_url) in shell_config.content_handlers.iteritems(): | 229 content_handler_url) in shell_config.content_handlers.iteritems(): |
225 shell_args = append_to_argument(shell_args, '--content-handlers=', | 230 shell_args = append_to_argument(shell_args, '--content-handlers=', |
226 '%s,%s' % (mime_type, | 231 '%s,%s' % (mime_type, |
227 content_handler_url)) | 232 content_handler_url)) |
228 | 233 |
229 for dev_server_config in shell_config.dev_servers: | 234 for dev_server_config in shell_config.dev_servers: |
230 shell_args = _configure_dev_server(shell, shell_args, dev_server_config, | 235 shell_args = _configure_dev_server(shell, shell_args, dev_server_config, |
236 shell_config.free_host_ports, | |
231 shell_config.verbose) | 237 shell_config.verbose) |
232 | 238 |
233 return shell, shell_args | 239 return shell, shell_args |
OLD | NEW |