Aviv Keshet | 6f45526 | 2013-03-01 16:02:29 -0800 | [diff] [blame] | 1 | # pylint: disable-msg=C0111 |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 2 | # Copyright 2008 Google Inc. Released under the GPL v2 |
| 3 | |
J. Richard Barnette | 89d5d21 | 2013-05-21 13:14:48 -0700 | [diff] [blame] | 4 | import warnings |
| 5 | with warnings.catch_warnings(): |
| 6 | # The 'compiler' module is gone in Python 3.0. Let's not say |
| 7 | # so in every log file. |
| 8 | warnings.simplefilter("ignore", DeprecationWarning) |
| 9 | import compiler |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 10 | import logging |
| 11 | import textwrap |
| 12 | import re |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 13 | |
Dan Shi | ef5b53f | 2013-01-22 10:22:01 -0800 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import enum |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import global_config |
Keith Haddow | 782e2a8 | 2017-09-26 15:44:51 -0700 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import priorities |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 17 | |
Scott Zawalski | 8ad52b6 | 2012-10-29 09:59:33 -0400 | [diff] [blame] | 18 | REQUIRED_VARS = set(['author', 'doc', 'name', 'time', 'test_type']) |
J. Richard Barnette | 4238478 | 2014-05-09 10:47:24 -0700 | [diff] [blame] | 19 | OBSOLETE_VARS = set(['experimental']) |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 20 | |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 21 | CONTROL_TYPE = enum.Enum('Server', 'Client', start_value=1) |
| 22 | CONTROL_TYPE_NAMES = enum.Enum(*CONTROL_TYPE.names, string_values=True) |
| 23 | |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 24 | _SUITE_ATTRIBUTE_PREFIX = 'suite:' |
| 25 | |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 26 | CONFIG = global_config.global_config |
| 27 | |
| 28 | # Default maximum test result size in kB. |
| 29 | DEFAULT_MAX_RESULT_SIZE_KB = CONFIG.get_config_value( |
Xixuan Wu | bfcd65b | 2017-11-13 21:16:27 -0800 | [diff] [blame] | 30 | 'AUTOSERV', 'default_max_result_size_KB', type=int, default=20000) |
| 31 | |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 32 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 33 | class ControlVariableException(Exception): |
| 34 | pass |
| 35 | |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 36 | def _validate_control_file_fields(control_file_path, control_file_vars, |
| 37 | raise_warnings): |
| 38 | """Validate the given set of variables from a control file. |
| 39 | |
| 40 | @param control_file_path: string path of the control file these were |
| 41 | loaded from. |
| 42 | @param control_file_vars: dict of variables set in a control file. |
| 43 | @param raise_warnings: True iff we should raise on invalid variables. |
| 44 | |
| 45 | """ |
| 46 | diff = REQUIRED_VARS - set(control_file_vars) |
| 47 | if diff: |
| 48 | warning = ('WARNING: Not all required control ' |
| 49 | 'variables were specified in %s. Please define ' |
| 50 | '%s.') % (control_file_path, ', '.join(diff)) |
| 51 | if raise_warnings: |
| 52 | raise ControlVariableException(warning) |
| 53 | print textwrap.wrap(warning, 80) |
| 54 | |
| 55 | obsolete = OBSOLETE_VARS & set(control_file_vars) |
| 56 | if obsolete: |
| 57 | warning = ('WARNING: Obsolete variables were ' |
| 58 | 'specified in %s. Please remove ' |
| 59 | '%s.') % (control_file_path, ', '.join(obsolete)) |
| 60 | if raise_warnings: |
| 61 | raise ControlVariableException(warning) |
| 62 | print textwrap.wrap(warning, 80) |
| 63 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 64 | |
| 65 | class ControlData(object): |
Dan Shi | ef5b53f | 2013-01-22 10:22:01 -0800 | [diff] [blame] | 66 | # Available TIME settings in control file, the list must be in lower case |
| 67 | # and in ascending order, test running faster comes first. |
| 68 | TEST_TIME_LIST = ['fast', 'short', 'medium', 'long', 'lengthy'] |
| 69 | TEST_TIME = enum.Enum(*TEST_TIME_LIST, string_values=False) |
| 70 | |
Dan Shi | ef5b53f | 2013-01-22 10:22:01 -0800 | [diff] [blame] | 71 | @staticmethod |
| 72 | def get_test_time_index(time): |
| 73 | """ |
| 74 | Get the order of estimated test time, based on the TIME setting in |
| 75 | Control file. Faster test gets a lower index number. |
| 76 | """ |
| 77 | try: |
| 78 | return ControlData.TEST_TIME.get_value(time.lower()) |
| 79 | except AttributeError: |
| 80 | # Raise exception if time value is not a valid TIME setting. |
| 81 | error_msg = '%s is not a valid TIME.' % time |
| 82 | logging.error(error_msg) |
| 83 | raise ControlVariableException(error_msg) |
| 84 | |
| 85 | |
mbligh | d7cd983 | 2008-10-02 16:20:37 +0000 | [diff] [blame] | 86 | def __init__(self, vars, path, raise_warnings=False): |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 87 | # Defaults |
mbligh | d7cd983 | 2008-10-02 16:20:37 +0000 | [diff] [blame] | 88 | self.path = path |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 89 | self.dependencies = set() |
J. Richard Barnette | 4238478 | 2014-05-09 10:47:24 -0700 | [diff] [blame] | 90 | # TODO(jrbarnette): This should be removed once outside |
| 91 | # code that uses can be changed. |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 92 | self.experimental = False |
| 93 | self.run_verify = True |
| 94 | self.sync_count = 1 |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 95 | self.test_parameters = set() |
Scott Zawalski | 8ad52b6 | 2012-10-29 09:59:33 -0400 | [diff] [blame] | 96 | self.test_category = '' |
| 97 | self.test_class = '' |
Alex Zamorzaev | ab788a4 | 2019-05-21 21:49:13 +0000 | [diff] [blame] | 98 | self.job_retries = 0 |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 99 | # Default to require server-side package. Unless require_ssp is |
| 100 | # explicitly set to False, server-side package will be used for the |
| 101 | # job. This can be overridden by global config |
| 102 | # AUTOSERV/enable_ssp_container |
| 103 | self.require_ssp = None |
Shuqian Zhao | 13248c5 | 2015-03-27 13:32:17 -0700 | [diff] [blame] | 104 | self.attributes = set() |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 105 | self.max_result_size_KB = DEFAULT_MAX_RESULT_SIZE_KB |
Keith Haddow | 782e2a8 | 2017-09-26 15:44:51 -0700 | [diff] [blame] | 106 | self.priority = priorities.Priority.DEFAULT |
Xixuan Wu | cd36ae0 | 2017-11-10 13:51:00 -0800 | [diff] [blame] | 107 | self.fast = False |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 108 | |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 109 | _validate_control_file_fields(self.path, vars, raise_warnings) |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 110 | |
| 111 | for key, val in vars.iteritems(): |
| 112 | try: |
| 113 | self.set_attr(key, val, raise_warnings) |
| 114 | except Exception, e: |
| 115 | if raise_warnings: |
| 116 | raise |
J. Richard Barnette | 4238478 | 2014-05-09 10:47:24 -0700 | [diff] [blame] | 117 | print 'WARNING: %s; skipping' % e |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 118 | |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 119 | self._patch_up_suites_from_attributes() |
| 120 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 121 | |
Allen Li | f20e17d | 2017-01-03 18:24:19 -0800 | [diff] [blame] | 122 | @property |
| 123 | def suite_tag_parts(self): |
| 124 | """Return the part strings of the test's suite tag.""" |
| 125 | if hasattr(self, 'suite'): |
| 126 | return [part.strip() for part in self.suite.split(',')] |
| 127 | else: |
| 128 | return [] |
| 129 | |
| 130 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 131 | def set_attr(self, attr, val, raise_warnings=False): |
| 132 | attr = attr.lower() |
| 133 | try: |
| 134 | set_fn = getattr(self, 'set_%s' % attr) |
| 135 | set_fn(val) |
| 136 | except AttributeError: |
| 137 | # This must not be a variable we care about |
| 138 | pass |
| 139 | |
| 140 | |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 141 | def _patch_up_suites_from_attributes(self): |
| 142 | """Patch up the set of suites this test is part of. |
| 143 | |
Christopher Wiley | bfec592 | 2016-03-07 09:11:32 -0800 | [diff] [blame] | 144 | Legacy builds will not have an appropriate ATTRIBUTES field set. |
| 145 | Take the union of suites specified via ATTRIBUTES and suites specified |
| 146 | via SUITE. |
| 147 | |
| 148 | SUITE used to be its own variable, but now suites are taken only from |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 149 | the attributes. |
| 150 | |
| 151 | """ |
Christopher Wiley | bfec592 | 2016-03-07 09:11:32 -0800 | [diff] [blame] | 152 | |
| 153 | suite_names = set() |
| 154 | # Extract any suites we know ourselves to be in based on the SUITE |
| 155 | # line. This line is deprecated, but control files in old builds will |
| 156 | # still have it. |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 157 | if hasattr(self, 'suite'): |
Christopher Wiley | bfec592 | 2016-03-07 09:11:32 -0800 | [diff] [blame] | 158 | existing_suites = self.suite.split(',') |
| 159 | existing_suites = [name.strip() for name in existing_suites] |
| 160 | existing_suites = [name for name in existing_suites if name] |
| 161 | suite_names.update(existing_suites) |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 162 | |
| 163 | # Figure out if our attributes mention any suites. |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 164 | for attribute in self.attributes: |
| 165 | if not attribute.startswith(_SUITE_ATTRIBUTE_PREFIX): |
| 166 | continue |
| 167 | suite_name = attribute[len(_SUITE_ATTRIBUTE_PREFIX):] |
Christopher Wiley | bfec592 | 2016-03-07 09:11:32 -0800 | [diff] [blame] | 168 | suite_names.add(suite_name) |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 169 | |
| 170 | # Rebuild the suite field if necessary. |
| 171 | if suite_names: |
Christopher Wiley | 4a14056 | 2016-03-08 09:36:24 -0800 | [diff] [blame] | 172 | self.set_suite(','.join(sorted(list(suite_names)))) |
Christopher Wiley | 6a8ac2d | 2016-03-01 15:41:36 -0800 | [diff] [blame] | 173 | |
| 174 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 175 | def _set_string(self, attr, val): |
| 176 | val = str(val) |
| 177 | setattr(self, attr, val) |
| 178 | |
| 179 | |
| 180 | def _set_option(self, attr, val, options): |
| 181 | val = str(val) |
| 182 | if val.lower() not in [x.lower() for x in options]: |
| 183 | raise ValueError("%s must be one of the following " |
| 184 | "options: %s" % (attr, |
| 185 | ', '.join(options))) |
| 186 | setattr(self, attr, val) |
| 187 | |
| 188 | |
| 189 | def _set_bool(self, attr, val): |
| 190 | val = str(val).lower() |
| 191 | if val == "false": |
| 192 | val = False |
| 193 | elif val == "true": |
| 194 | val = True |
| 195 | else: |
| 196 | msg = "%s must be either true or false" % attr |
| 197 | raise ValueError(msg) |
| 198 | setattr(self, attr, val) |
| 199 | |
| 200 | |
| 201 | def _set_int(self, attr, val, min=None, max=None): |
| 202 | val = int(val) |
| 203 | if min is not None and min > val: |
| 204 | raise ValueError("%s is %d, which is below the " |
| 205 | "minimum of %d" % (attr, val, min)) |
| 206 | if max is not None and max < val: |
| 207 | raise ValueError("%s is %d, which is above the " |
| 208 | "maximum of %d" % (attr, val, max)) |
| 209 | setattr(self, attr, val) |
| 210 | |
| 211 | |
| 212 | def _set_set(self, attr, val): |
| 213 | val = str(val) |
Dan Shi | cddad39 | 2016-03-23 15:15:07 -0700 | [diff] [blame] | 214 | items = [x.strip() for x in val.split(',') if x.strip()] |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 215 | setattr(self, attr, set(items)) |
| 216 | |
| 217 | |
| 218 | def set_author(self, val): |
| 219 | self._set_string('author', val) |
| 220 | |
| 221 | |
| 222 | def set_dependencies(self, val): |
| 223 | self._set_set('dependencies', val) |
| 224 | |
| 225 | |
| 226 | def set_doc(self, val): |
| 227 | self._set_string('doc', val) |
| 228 | |
| 229 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 230 | def set_name(self, val): |
| 231 | self._set_string('name', val) |
| 232 | |
| 233 | |
| 234 | def set_run_verify(self, val): |
| 235 | self._set_bool('run_verify', val) |
| 236 | |
| 237 | |
| 238 | def set_sync_count(self, val): |
| 239 | self._set_int('sync_count', val, min=1) |
| 240 | |
| 241 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 242 | def set_suite(self, val): |
| 243 | self._set_string('suite', val) |
| 244 | |
| 245 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 246 | def set_time(self, val): |
Dan Shi | ef5b53f | 2013-01-22 10:22:01 -0800 | [diff] [blame] | 247 | self._set_option('time', val, ControlData.TEST_TIME_LIST) |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | def set_test_class(self, val): |
| 251 | self._set_string('test_class', val.lower()) |
| 252 | |
| 253 | |
| 254 | def set_test_category(self, val): |
| 255 | self._set_string('test_category', val.lower()) |
| 256 | |
| 257 | |
| 258 | def set_test_type(self, val): |
Aviv Keshet | 3dd8beb | 2013-05-13 17:36:04 -0700 | [diff] [blame] | 259 | self._set_option('test_type', val, list(CONTROL_TYPE.names)) |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 260 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 261 | |
Eric Li | d3e8a3b | 2010-12-23 10:02:07 -0800 | [diff] [blame] | 262 | def set_test_parameters(self, val): |
| 263 | self._set_set('test_parameters', val) |
| 264 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 265 | |
Fang Deng | e3bc24b | 2014-03-17 15:19:46 -0700 | [diff] [blame] | 266 | def set_job_retries(self, val): |
| 267 | self._set_int('job_retries', val) |
| 268 | |
Aviv Keshet | 6f45526 | 2013-03-01 16:02:29 -0800 | [diff] [blame] | 269 | |
Prashanth B | ee70731 | 2014-03-31 13:00:36 -0700 | [diff] [blame] | 270 | def set_bug_template(self, val): |
| 271 | if type(val) == dict: |
| 272 | setattr(self, 'bug_template', val) |
| 273 | |
| 274 | |
Dan Shi | ec1d47d | 2015-02-13 11:38:13 -0800 | [diff] [blame] | 275 | def set_require_ssp(self, val): |
| 276 | self._set_bool('require_ssp', val) |
| 277 | |
| 278 | |
Michael Tang | 6dc174e | 2016-05-31 23:13:42 -0700 | [diff] [blame] | 279 | def set_build(self, val): |
| 280 | self._set_string('build', val) |
| 281 | |
| 282 | |
| 283 | def set_builds(self, val): |
| 284 | if type(val) == dict: |
| 285 | setattr(self, 'builds', val) |
| 286 | |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 287 | def set_max_result_size_kb(self, val): |
| 288 | self._set_int('max_result_size_KB', val) |
| 289 | |
Keith Haddow | 782e2a8 | 2017-09-26 15:44:51 -0700 | [diff] [blame] | 290 | def set_priority(self, val): |
| 291 | self._set_int('priority', val) |
| 292 | |
Xixuan Wu | cd36ae0 | 2017-11-10 13:51:00 -0800 | [diff] [blame] | 293 | def set_fast(self, val): |
| 294 | self._set_bool('fast', val) |
| 295 | |
Xixuan Wu | 0722448 | 2019-04-11 18:08:19 -0700 | [diff] [blame] | 296 | def set_update_type(self, val): |
| 297 | self._set_string('update_type', val) |
| 298 | |
| 299 | def set_source_release(self, val): |
| 300 | self._set_string('source_release', val) |
| 301 | |
| 302 | def set_target_release(self, val): |
| 303 | self._set_string('target_release', val) |
| 304 | |
| 305 | def set_target_payload_uri(self, val): |
| 306 | self._set_string('target_payload_uri', val) |
| 307 | |
| 308 | def set_source_payload_uri(self, val): |
| 309 | self._set_string('source_payload_uri', val) |
| 310 | |
| 311 | def set_source_archive_uri(self, val): |
| 312 | self._set_string('source_archive_uri', val) |
| 313 | |
Shuqian Zhao | 13248c5 | 2015-03-27 13:32:17 -0700 | [diff] [blame] | 314 | def set_attributes(self, val): |
Shuqian Zhao | 9e68121 | 2015-09-18 16:11:07 -0700 | [diff] [blame] | 315 | # Add subsystem:default if subsystem is not specified. |
Shuqian Zhao | 13248c5 | 2015-03-27 13:32:17 -0700 | [diff] [blame] | 316 | self._set_set('attributes', val) |
Shuqian Zhao | 9e68121 | 2015-09-18 16:11:07 -0700 | [diff] [blame] | 317 | if not any(a.startswith('subsystem') for a in self.attributes): |
| 318 | self.attributes.add('subsystem:default') |
Shuqian Zhao | 13248c5 | 2015-03-27 13:32:17 -0700 | [diff] [blame] | 319 | |
| 320 | |
Prashanth B | ee70731 | 2014-03-31 13:00:36 -0700 | [diff] [blame] | 321 | def _extract_const(expr): |
| 322 | assert(expr.__class__ == compiler.ast.Const) |
| 323 | assert(expr.value.__class__ in (str, int, float, unicode)) |
| 324 | return str(expr.value).strip() |
| 325 | |
| 326 | |
| 327 | def _extract_dict(expr): |
| 328 | assert(expr.__class__ == compiler.ast.Dict) |
| 329 | assert(expr.items.__class__ == list) |
| 330 | cf_dict = {} |
| 331 | for key, value in expr.items: |
| 332 | try: |
| 333 | key = _extract_const(key) |
| 334 | val = _extract_expression(value) |
| 335 | except (AssertionError, ValueError): |
| 336 | pass |
| 337 | else: |
| 338 | cf_dict[key] = val |
| 339 | return cf_dict |
| 340 | |
| 341 | |
| 342 | def _extract_list(expr): |
| 343 | assert(expr.__class__ == compiler.ast.List) |
| 344 | list_values = [] |
| 345 | for value in expr.nodes: |
| 346 | try: |
| 347 | list_values.append(_extract_expression(value)) |
| 348 | except (AssertionError, ValueError): |
| 349 | pass |
| 350 | return list_values |
| 351 | |
| 352 | |
| 353 | def _extract_name(expr): |
| 354 | assert(expr.__class__ == compiler.ast.Name) |
| 355 | assert(expr.name in ('False', 'True', 'None')) |
| 356 | return str(expr.name) |
| 357 | |
| 358 | |
| 359 | def _extract_expression(expr): |
| 360 | if expr.__class__ == compiler.ast.Const: |
| 361 | return _extract_const(expr) |
| 362 | if expr.__class__ == compiler.ast.Name: |
| 363 | return _extract_name(expr) |
| 364 | if expr.__class__ == compiler.ast.Dict: |
| 365 | return _extract_dict(expr) |
| 366 | if expr.__class__ == compiler.ast.List: |
| 367 | return _extract_list(expr) |
| 368 | raise ValueError('Unknown rval %s' % expr) |
| 369 | |
| 370 | |
| 371 | def _extract_assignment(n): |
mbligh | 93f4209 | 2008-07-18 01:01:58 +0000 | [diff] [blame] | 372 | assert(n.__class__ == compiler.ast.Assign) |
mbligh | 93f4209 | 2008-07-18 01:01:58 +0000 | [diff] [blame] | 373 | assert(n.nodes.__class__ == list) |
| 374 | assert(len(n.nodes) == 1) |
| 375 | assert(n.nodes[0].__class__ == compiler.ast.AssName) |
| 376 | assert(n.nodes[0].flags.__class__ == str) |
| 377 | assert(n.nodes[0].name.__class__ == str) |
| 378 | |
Prashanth B | ee70731 | 2014-03-31 13:00:36 -0700 | [diff] [blame] | 379 | val = _extract_expression(n.expr) |
mbligh | 93f4209 | 2008-07-18 01:01:58 +0000 | [diff] [blame] | 380 | key = n.nodes[0].name.lower() |
mbligh | 93f4209 | 2008-07-18 01:01:58 +0000 | [diff] [blame] | 381 | |
| 382 | return (key, val) |
| 383 | |
| 384 | |
Christopher Wiley | 10439d8 | 2016-03-07 12:45:26 -0800 | [diff] [blame] | 385 | def parse_control_string(control, raise_warnings=False, path=''): |
| 386 | """Parse a control file from a string. |
| 387 | |
| 388 | @param control: string containing the text of a control file. |
| 389 | @param raise_warnings: True iff ControlData should raise an error on |
| 390 | warnings about control file contents. |
| 391 | @param path: string path to the control file. |
| 392 | |
| 393 | """ |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 394 | try: |
| 395 | mod = compiler.parse(control) |
Prathmesh Prabhu | 8587d1f | 2018-01-08 17:31:21 -0800 | [diff] [blame] | 396 | except SyntaxError as e: |
| 397 | logging.error('Syntax error (%s) while parsing control string:', e) |
| 398 | lines = control.split('\n') |
| 399 | for n, l in enumerate(lines): |
| 400 | logging.error('Line %d: %s', n + 1, l) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 401 | raise ControlVariableException("Error parsing data because %s" % e) |
Christopher Wiley | 10439d8 | 2016-03-07 12:45:26 -0800 | [diff] [blame] | 402 | return finish_parse(mod, path, raise_warnings) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 403 | |
| 404 | |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 405 | def parse_control(path, raise_warnings=False): |
mbligh | d7cd983 | 2008-10-02 16:20:37 +0000 | [diff] [blame] | 406 | try: |
| 407 | mod = compiler.parseFile(path) |
| 408 | except SyntaxError, e: |
mbligh | 4b5c31e | 2009-07-11 00:55:34 +0000 | [diff] [blame] | 409 | raise ControlVariableException("Error parsing %s because %s" % |
| 410 | (path, e)) |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 411 | return finish_parse(mod, path, raise_warnings) |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 412 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 413 | |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 414 | def _try_extract_assignment(node, variables): |
| 415 | """Try to extract assignment from the given node. |
| 416 | |
| 417 | @param node: An Assign object. |
| 418 | @param variables: Dictionary to store the parsed assignments. |
| 419 | """ |
| 420 | try: |
| 421 | key, val = _extract_assignment(node) |
| 422 | variables[key] = val |
| 423 | except (AssertionError, ValueError): |
| 424 | pass |
| 425 | |
| 426 | |
Chris Masone | 6fed646 | 2011-10-20 16:36:43 -0700 | [diff] [blame] | 427 | def finish_parse(mod, path, raise_warnings): |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 428 | assert(mod.__class__ == compiler.ast.Module) |
| 429 | assert(mod.node.__class__ == compiler.ast.Stmt) |
| 430 | assert(mod.node.nodes.__class__ == list) |
| 431 | |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 432 | variables = {} |
Xixuan Wu | cd36ae0 | 2017-11-10 13:51:00 -0800 | [diff] [blame] | 433 | injection_variables = {} |
mbligh | 99c2c6f | 2008-07-11 18:15:46 +0000 | [diff] [blame] | 434 | for n in mod.node.nodes: |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 435 | if (n.__class__ == compiler.ast.Function and |
| 436 | re.match('step\d+', n.name)): |
| 437 | vars_in_step = {} |
| 438 | for sub_node in n.code.nodes: |
| 439 | _try_extract_assignment(sub_node, vars_in_step) |
| 440 | if vars_in_step: |
| 441 | # Empty the vars collection so assignments from multiple steps |
| 442 | # won't be mixed. |
| 443 | variables.clear() |
| 444 | variables.update(vars_in_step) |
| 445 | else: |
Xixuan Wu | cd36ae0 | 2017-11-10 13:51:00 -0800 | [diff] [blame] | 446 | _try_extract_assignment(n, injection_variables) |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 447 | |
Xixuan Wu | cd36ae0 | 2017-11-10 13:51:00 -0800 | [diff] [blame] | 448 | variables.update(injection_variables) |
Dan Shi | f53d126 | 2017-06-19 11:25:25 -0700 | [diff] [blame] | 449 | return ControlData(variables, path, raise_warnings) |