| 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 """Functions that configure the shell before it is run manipulating its argument | 5 """Produces configured shell abstractions. |
| 6 list. | 6 |
| 7 This module knows how to produce a configured shell abstraction based on |
| 8 shell_config.ShellConfig. |
| 7 """ | 9 """ |
| 8 | 10 |
| 9 import os.path | 11 import os.path |
| 10 import sys | 12 import sys |
| 11 import urlparse | 13 import urlparse |
| 12 | 14 |
| 13 from devtoolslib.android_shell import AndroidShell | 15 from devtoolslib.android_shell import AndroidShell |
| 14 from devtoolslib.linux_shell import LinuxShell | 16 from devtoolslib.linux_shell import LinuxShell |
| 15 | 17 |
| 16 # When spinning up servers for local origins, we want to use predictable ports | 18 # When spinning up servers for local origins, we want to use predictable ports |
| 17 # so that caching works between subsequent runs with the same command line. | 19 # so that caching works between subsequent runs with the same command line. |
| 18 _LOCAL_ORIGIN_PORT = 31840 | 20 _LOCAL_ORIGIN_PORT = 31840 |
| 19 _MAPPINGS_BASE_PORT = 31841 | 21 _MAPPINGS_BASE_PORT = 31841 |
| 20 _SKY_SERVER_PORT = 9998 | 22 |
| 23 |
| 24 class ShellConfigurationException(Exception): |
| 25 """Represents an error preventing creating a functional shell abstraction.""" |
| 26 pass |
| 21 | 27 |
| 22 | 28 |
| 23 def _is_web_url(dest): | 29 def _is_web_url(dest): |
| 24 return True if urlparse.urlparse(dest).scheme else False | 30 return True if urlparse.urlparse(dest).scheme else False |
| 25 | 31 |
| 26 | 32 |
| 27 def _host_local_url_destination(shell, dest_file, port): | 33 def _host_local_url_destination(shell, dest_file, port): |
| 28 """Starts a local server to host |dest_file|. | 34 """Starts a local server to host |dest_file|. |
| 29 | 35 |
| 30 Returns: | 36 Returns: |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 if not argument.startswith(key): | 163 if not argument.startswith(key): |
| 158 continue | 164 continue |
| 159 arguments[i] = argument + delimiter + value | 165 arguments[i] = argument + delimiter + value |
| 160 break | 166 break |
| 161 else: | 167 else: |
| 162 arguments.append(key + value) | 168 arguments.append(key + value) |
| 163 | 169 |
| 164 return arguments | 170 return arguments |
| 165 | 171 |
| 166 | 172 |
| 167 def add_shell_arguments(parser): | 173 def get_shell(shell_config, shell_args): |
| 168 """Adds argparse arguments allowing to configure shell abstraction using | |
| 169 configure_shell() below. | |
| 170 """ | 174 """ |
| 171 # Arguments configuring the shell run. | 175 Produces a shell abstraction configured according to |shell_config|. |
| 172 parser.add_argument('--shell-path', help='Path of the Mojo shell binary.') | |
| 173 parser.add_argument('--android', help='Run on Android', | |
| 174 action='store_true') | |
| 175 parser.add_argument('--origin', help='Origin for mojo: URLs. This can be a ' | |
| 176 'web url or a local directory path.') | |
| 177 parser.add_argument('--map-url', action='append', | |
| 178 help='Define a mapping for a url in the format ' | |
| 179 '<url>=<url-or-local-file-path>') | |
| 180 parser.add_argument('--map-origin', action='append', | |
| 181 help='Define a mapping for a url origin in the format ' | |
| 182 '<origin>=<url-or-local-file-path>') | |
| 183 parser.add_argument('--sky', action='store_true', | |
| 184 help='Maps mojo:sky_viewer as the content handler for ' | |
| 185 'dart apps.') | |
| 186 parser.add_argument('-v', '--verbose', action="store_true", | |
| 187 help="Increase output verbosity") | |
| 188 | |
| 189 android_group = parser.add_argument_group('Android-only', | |
| 190 'These arguments apply only when --android is passed.') | |
| 191 android_group.add_argument('--adb-path', help='Path of the adb binary.') | |
| 192 android_group.add_argument('--target-device', help='Device to run on.') | |
| 193 android_group.add_argument('--logcat-tags', help='Comma-separated list of ' | |
| 194 'additional logcat tags to display.') | |
| 195 | |
| 196 desktop_group = parser.add_argument_group('Desktop-only', | |
| 197 'These arguments apply only when running on desktop.') | |
| 198 desktop_group.add_argument('--use-osmesa', action='store_true', | |
| 199 help='Configure the native viewport service ' | |
| 200 'for off-screen rendering.') | |
| 201 | |
| 202 | |
| 203 class ShellConfigurationException(Exception): | |
| 204 """Represents an error preventing creating a functional shell abstraction.""" | |
| 205 pass | |
| 206 | |
| 207 | |
| 208 def configure_shell(config_args, shell_args): | |
| 209 """ | |
| 210 Produces a shell abstraction configured using the parsed arguments defined in | |
| 211 add_shell_arguments(). | |
| 212 | 176 |
| 213 Args: | 177 Args: |
| 214 config_args: Parsed arguments added using add_shell_arguments(). | 178 shell_config: Instance of shell_config.ShellConfig. |
| 215 shell_args: Additional raw shell arguments to be passed to the shell. We | 179 shell_args: Additional raw shell arguments to be passed to the shell. We |
| 216 need to take these into account as some parameters need to appear only | 180 need to take these into account as some parameters need to appear only |
| 217 once on the argument list (e.g. url-mappings) so we need to coalesce any | 181 once on the argument list (e.g. url-mappings) so we need to coalesce any |
| 218 overrides and the existing value into just one argument. | 182 overrides and the existing value into just one argument. |
| 219 | 183 |
| 220 Returns: | 184 Returns: |
| 221 A tuple of (shell, shell_args). | 185 A tuple of (shell, shell_args). |shell| is the configured shell abstraction, |
| 186 |shell_args| is updated list of shell arguments. |
| 222 | 187 |
| 223 Throws: | 188 Throws: |
| 224 ShellConfigurationException if shell abstraction could not be configured. | 189 ShellConfigurationException if shell abstraction could not be configured. |
| 225 """ | 190 """ |
| 226 if config_args.android: | 191 if shell_config.android: |
| 227 verbose_pipe = sys.stdout if config_args.verbose else None | 192 verbose_pipe = sys.stdout if shell_config.verbose else None |
| 228 | 193 |
| 229 shell = AndroidShell(config_args.adb_path, config_args.target_device, | 194 shell = AndroidShell(shell_config.adb_path, shell_config.target_device, |
| 230 logcat_tags=config_args.logcat_tags, | 195 logcat_tags=shell_config.logcat_tags, |
| 231 verbose_pipe=verbose_pipe) | 196 verbose_pipe=verbose_pipe) |
| 197 |
| 232 device_status, error = shell.CheckDevice() | 198 device_status, error = shell.CheckDevice() |
| 233 if not device_status: | 199 if not device_status: |
| 234 raise ShellConfigurationException('Device check failed: ' + error) | 200 raise ShellConfigurationException('Device check failed: ' + error) |
| 235 if config_args.shell_path: | 201 if shell_config.shell_path: |
| 236 shell.InstallApk(config_args.shell_path) | 202 shell.InstallApk(shell_config.shell_path) |
| 237 else: | 203 else: |
| 238 if not config_args.shell_path: | 204 if not shell_config.shell_path: |
| 239 raise ShellConfigurationException('Can not run without a shell binary. ' | 205 raise ShellConfigurationException('Can not run without a shell binary. ' |
| 240 'Please pass --shell-path.') | 206 'Please pass --shell-path.') |
| 241 shell = LinuxShell(config_args.shell_path) | 207 shell = LinuxShell(shell_config.shell_path) |
| 242 if config_args.use_osmesa: | 208 if shell_config.use_osmesa: |
| 243 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa') | 209 shell_args.append('--args-for=mojo:native_viewport_service --use-osmesa') |
| 244 | 210 |
| 245 shell_args = _apply_mappings(shell, shell_args, config_args.map_url, | 211 shell_args = _apply_mappings(shell, shell_args, shell_config.map_url_list, |
| 246 config_args.map_origin) | 212 shell_config.map_origin_list) |
| 247 | 213 |
| 248 if config_args.origin: | 214 if shell_config.origin: |
| 249 if _is_web_url(config_args.origin): | 215 if _is_web_url(shell_config.origin): |
| 250 shell_args.append('--origin=' + config_args.origin) | 216 shell_args.append('--origin=' + shell_config.origin) |
| 251 else: | 217 else: |
| 252 shell_args.extend(configure_local_origin(shell, config_args.origin, | 218 shell_args.extend(configure_local_origin(shell, shell_config.origin, |
| 253 fixed_port=True)) | 219 fixed_port=True)) |
| 254 | 220 |
| 255 if config_args.sky: | 221 if shell_config.sky: |
| 256 shell_args = _configure_sky(shell_args) | 222 shell_args = _configure_sky(shell_args) |
| 257 | 223 |
| 258 return shell, shell_args | 224 return shell, shell_args |
| OLD | NEW |