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 """Configuration for the shell abstraction. | 5 """Configuration for the shell abstraction. |
6 | 6 |
7 This module declares ShellConfig and knows how to compute it from command-line | 7 This module declares ShellConfig and knows how to compute it from command-line |
8 arguments, applying any default paths inferred from the checkout, configuration | 8 arguments, applying any default paths inferred from the checkout, configuration |
9 file, etc. | 9 file, etc. |
10 """ | 10 """ |
11 | 11 |
12 import ast | |
13 import collections | |
14 | |
12 from devtoolslib import paths | 15 from devtoolslib import paths |
13 | 16 |
14 | 17 |
18 class ShellConfigurationException(Exception): | |
19 """Represents an error preventing creating a functional shell abstraction.""" | |
20 pass | |
21 | |
22 | |
15 class ShellConfig(object): | 23 class ShellConfig(object): |
16 """Configuration for the shell abstraction.""" | 24 """Configuration for the shell abstraction.""" |
17 | 25 |
18 def __init__(self): | 26 def __init__(self): |
19 self.android = None | 27 self.android = None |
20 self.shell_path = None | 28 self.shell_path = None |
21 self.origin = None | 29 self.origin = None |
22 self.map_url_list = None | 30 self.map_url_list = [] |
23 self.map_origin_list = None | 31 self.map_origin_list = [] |
32 self.dev_servers = [] | |
24 self.sky = None | 33 self.sky = None |
25 self.verbose = None | 34 self.verbose = None |
26 | 35 |
27 # Android-only. | 36 # Android-only. |
28 self.adb_path = None | 37 self.adb_path = None |
29 self.target_device = None | 38 self.target_device = None |
30 self.logcat_tags = None | 39 self.logcat_tags = None |
31 | 40 |
32 # Desktop-only. | 41 # Desktop-only. |
33 self.use_osmesa = None | 42 self.use_osmesa = None |
34 | 43 |
35 | 44 |
45 class DevServerConfig(object): | |
46 """Configuration for a development server running on a host and available to | |
47 the shell. | |
48 """ | |
49 def __init__(self): | |
50 self.host = None | |
51 self.mappings = None | |
52 | |
53 | |
36 def add_shell_arguments(parser): | 54 def add_shell_arguments(parser): |
37 """Adds argparse arguments allowing to configure shell abstraction using | 55 """Adds argparse arguments allowing to configure shell abstraction using |
38 configure_shell() below. | 56 configure_shell() below. |
39 """ | 57 """ |
40 # Arguments configuring the shell run. | 58 # Arguments configuring the shell run. |
41 parser.add_argument('--android', help='Run on Android', | 59 parser.add_argument('--android', help='Run on Android', |
42 action='store_true') | 60 action='store_true') |
43 parser.add_argument('--shell-path', help='Path of the Mojo shell binary.') | 61 parser.add_argument('--shell-path', help='Path of the Mojo shell binary.') |
44 parser.add_argument('--origin', help='Origin for mojo: URLs. This can be a ' | 62 parser.add_argument('--origin', help='Origin for mojo: URLs. This can be a ' |
45 'web url or a local directory path.') | 63 'web url or a local directory path.') |
(...skipping 15 matching lines...) Expand all Loading... | |
61 android_group.add_argument('--target-device', help='Device to run on.') | 79 android_group.add_argument('--target-device', help='Device to run on.') |
62 android_group.add_argument('--logcat-tags', help='Comma-separated list of ' | 80 android_group.add_argument('--logcat-tags', help='Comma-separated list of ' |
63 'additional logcat tags to display.') | 81 'additional logcat tags to display.') |
64 | 82 |
65 desktop_group = parser.add_argument_group('Desktop-only', | 83 desktop_group = parser.add_argument_group('Desktop-only', |
66 'These arguments apply only when running on desktop.') | 84 'These arguments apply only when running on desktop.') |
67 desktop_group.add_argument('--use-osmesa', action='store_true', | 85 desktop_group.add_argument('--use-osmesa', action='store_true', |
68 help='Configure the native viewport service ' | 86 help='Configure the native viewport service ' |
69 'for off-screen rendering.') | 87 'for off-screen rendering.') |
70 | 88 |
71 # Arguments allowing to indicate the configuration we are targeting when | 89 config_file_group = parser.add_argument_group('Configuration file', |
72 # running within a Chromium-like checkout. These will go away once we have | 90 'These arguments allow to modify the behavior regarding the mojoconfig ' |
73 # devtools config files, see https://github.com/domokit/devtools/issues/28. | 91 'file.') |
74 chromium_config_group = parser.add_argument_group('Chromium configuration', | 92 config_file_group.add_argument('--config-file', type=file, |
93 help='Path of the configuration file to use.') | |
94 | |
95 # Arguments allowing to indicate the build directory we are targeting when | |
96 # running within a Chromium-like checkout (e.g. Mojo checkout). These will go | |
97 # away once we have devtools config files, see | |
98 # https://github.com/domokit/devtools/issues/28. | |
99 chromium_checkout_group = parser.add_argument_group( | |
100 'Chromium-like checkout configuration', | |
75 'These arguments allow to infer paths to tools and build results ' | 101 'These arguments allow to infer paths to tools and build results ' |
76 'when running withing a Chromium-like checkout') | 102 'when running within a Chromium-like checkout') |
77 debug_group = chromium_config_group.add_mutually_exclusive_group() | 103 debug_group = chromium_checkout_group.add_mutually_exclusive_group() |
78 debug_group.add_argument('--debug', help='Debug build (default)', | 104 debug_group.add_argument('--debug', help='Debug build (default)', |
79 default=True, action='store_true') | 105 default=True, action='store_true') |
80 debug_group.add_argument('--release', help='Release build', default=False, | 106 debug_group.add_argument('--release', help='Release build', default=False, |
81 dest='debug', action='store_false') | 107 dest='debug', action='store_false') |
82 chromium_config_group.add_argument('--target-cpu', | 108 chromium_checkout_group.add_argument('--target-cpu', |
83 help='CPU architecture to run for.', | 109 help='CPU architecture to run for.', |
84 choices=['x64', 'x86', 'arm']) | 110 choices=['x64', 'x86', 'arm']) |
85 | 111 |
86 | 112 |
113 def _read_config_file(config_file, aliases): | |
114 spec = config_file.read() | |
115 for alias_pattern, alias_value in aliases: | |
116 spec = spec.replace(alias_pattern, alias_value) | |
117 config_dict = ast.literal_eval(spec) | |
118 return collections.defaultdict(lambda: None, config_dict) | |
qsr
2015/08/04 14:06:45
I do not think you need a defaultdict here.
ppi
2015/08/04 16:04:51
Done.
| |
119 | |
120 | |
87 def get_shell_config(script_args): | 121 def get_shell_config(script_args): |
88 """Processes command-line options defined in add_shell_arguments(), applying | 122 """Processes command-line options defined in add_shell_arguments(), applying |
89 any inferred default paths and produces an instance of ShellConfig. | 123 any inferred default paths and produces an instance of ShellConfig. |
90 | 124 |
91 Returns: | 125 Returns: |
92 An instance of ShellConfig. | 126 An instance of ShellConfig. |
93 """ | 127 """ |
94 # Infer paths based on the Chromium configuration options | 128 # Infer paths based on the Chromium configuration options |
95 # (--debug/--release, etc.), if running within a Chromium-like checkout. | 129 # (--debug/--release, etc.), if running within a Chromium-like checkout. |
96 inferred_paths = paths.infer_paths(script_args.android, script_args.debug, | 130 inferred_paths = paths.infer_paths(script_args.android, script_args.debug, |
97 script_args.target_cpu) | 131 script_args.target_cpu) |
98 | |
99 shell_config = ShellConfig() | 132 shell_config = ShellConfig() |
100 | 133 |
101 shell_config.android = script_args.android | 134 shell_config.android = script_args.android |
102 shell_config.shell_path = (script_args.shell_path or | 135 shell_config.shell_path = (script_args.shell_path or |
103 inferred_paths['shell_path']) | 136 inferred_paths['shell_path']) |
104 shell_config.origin = script_args.origin | 137 shell_config.origin = script_args.origin |
105 shell_config.map_url_list = script_args.map_url | 138 shell_config.map_url_list = script_args.map_url |
106 shell_config.map_origin_list = script_args.map_origin | 139 shell_config.map_origin_list = script_args.map_origin |
107 shell_config.sky = script_args.sky | 140 shell_config.sky = script_args.sky |
108 shell_config.verbose = script_args.verbose | 141 shell_config.verbose = script_args.verbose |
109 | 142 |
110 # Android-only. | 143 # Android-only. |
111 shell_config.adb_path = (script_args.adb_path or inferred_paths['adb_path']) | 144 shell_config.adb_path = (script_args.adb_path or inferred_paths['adb_path']) |
112 shell_config.target_device = script_args.target_device | 145 shell_config.target_device = script_args.target_device |
113 shell_config.logcat_tags = script_args.logcat_tags | 146 shell_config.logcat_tags = script_args.logcat_tags |
114 | 147 |
115 # Desktop-only. | 148 # Desktop-only. |
116 shell_config.use_osmesa = script_args.use_osmesa | 149 shell_config.use_osmesa = script_args.use_osmesa |
117 | 150 |
118 if (shell_config.android and not shell_config.origin and | 151 if (shell_config.android and not shell_config.origin and |
119 inferred_paths['build_dir_path']): | 152 inferred_paths['build_dir_path']): |
120 shell_config.origin = inferred_paths['build_dir_path'] | 153 shell_config.origin = inferred_paths['build_dir_path'] |
154 | |
155 # Read the mojoconfig file. | |
156 mojoconfig = None | |
157 if script_args.config_file: | |
158 config_file_aliases = [] | |
159 if inferred_paths['build_dir_path']: | |
160 config_file_aliases.append(('@{BUILD_DIR}', | |
161 inferred_paths['build_dir_path'])) | |
162 | |
163 try: | |
164 mojoconfig = _read_config_file(script_args.config_file, | |
165 config_file_aliases) | |
166 except SyntaxError: | |
167 raise ShellConfigurationException('Failed to parse the mojoconfig file.') | |
168 | |
169 if mojoconfig['dev_servers']: | |
170 try: | |
171 for dev_server_spec in mojoconfig['dev_servers']: | |
172 dev_server_config = DevServerConfig() | |
173 dev_server_config.host = dev_server_spec['host'] | |
174 dev_server_config.mappings = [] | |
175 for prefix, path in dev_server_spec['mappings']: | |
176 dev_server_config.mappings.append((prefix, path)) | |
177 shell_config.dev_servers.append(dev_server_config) | |
178 except (ValueError, KeyError): | |
179 raise ShellConfigurationException('Failed to parse dev_servers in ' | |
180 'the mojoconfig file.') | |
121 return shell_config | 181 return shell_config |
OLD | NEW |