Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import re | |
| 6 | |
| 7 _BISECT_CONFIG_SCHEMA = { | |
| 8 'command': {'type': 'string', 'required': True}, | |
| 9 'good_revision': {'type': 'revision', 'required': True}, | |
| 10 'bad_revision': {'type': 'revision', 'required': True}, | |
| 11 'bisect_bot': {'type': 'string'}, | |
| 12 'metric': {'type': 'string'}, | |
| 13 'bug_id': {'type': 'integer'}, | |
| 14 'repeat_count': {'type': 'integer'}, | |
| 15 'max_time_minutes': {'type': 'integer'}, | |
| 16 'truncate_percent': {'type': 'integer'}, | |
| 17 'bisect_mode': {'type': 'string', | |
| 18 'choices': ['mean', 'return_code', 'std_dev']}, | |
| 19 'gs_bucket': {'type': 'string'}, | |
| 20 'builder_host': {'type': 'string'}, | |
| 21 'builder_port': {'type': 'integer'}, | |
| 22 'test_type': {'type': 'string'}, | |
| 23 'improvement_direction': {'type': 'integer'}, | |
| 24 'recipe_tester_name': {'type': 'string'}, | |
| 25 'try_job_id': {'type': 'integer'}, | |
|
qyearsley
2016/03/11 01:41:34
Anything else that should be added? Or anything th
RobertoCN
2016/03/14 01:26:23
Metric could be required if the bisect_mode is mea
qyearsley
2016/03/18 22:50:19
Check for metric added; truncate_percent removed f
| |
| 26 } | |
| 27 | |
| 28 | |
| 29 class ValidationFail(Exception): | |
| 30 """An exception class that represents a failure to validate.""" | |
| 31 | |
| 32 | |
| 33 def validate_config(config, schema=None): | |
| 34 """Checks the correctness of the given bisect job config.""" | |
| 35 schema = _BISECT_CONFIG_SCHEMA if schema is None else schema | |
| 36 for key in set(schema): | |
| 37 validate_key(config, schema, key) | |
|
qyearsley
2016/03/11 01:41:34
Potentially to be added: check revision order and
RobertoCN
2016/03/14 01:26:23
could also check for well-formedness of gs locatio
qyearsley
2016/03/18 22:50:19
In most bisect configs, there aren't any gs URLs,
| |
| 38 | |
| 39 | |
| 40 def validate_key(config, schema, key): | |
| 41 """Checks the correctness of the given field in a config.""" | |
| 42 if schema[key].get('required') and key not in config: | |
| 43 raise ValidationFail('Required key "%s" missing.' % key) | |
| 44 if key not in config: | |
| 45 return # Optional field. | |
| 46 value = config[key] | |
| 47 field_type = schema[key].get('type') | |
| 48 if field_type == 'string': | |
| 49 _validate_string(value, key) | |
| 50 elif field_type == 'integer': | |
| 51 _validate_integer(value, key) | |
| 52 elif field_type == 'revision': | |
| 53 _validate_revision(value, key) | |
| 54 elif field_type == 'boolean': | |
| 55 _validate_boolean(value, key) | |
| 56 if 'choices' in schema[key] and value not in schema[key]['choices']: | |
| 57 _fail(value, key) | |
| 58 | |
| 59 | |
| 60 def _fail(value, key): | |
| 61 raise ValidationFail('Invalid value %r for "%s".' % (value, key)) | |
| 62 | |
| 63 | |
| 64 def _validate_string(value, key): | |
| 65 if not isinstance(value, basestring): | |
| 66 _fail(value, key) | |
| 67 | |
| 68 | |
| 69 def _validate_revision(value, key): | |
| 70 s = str(value) | |
| 71 if not (s.isdigit() or re.match('^[0-9A-Fa-f]{40}$', s)): | |
| 72 _fail(value, key) | |
| 73 | |
| 74 | |
| 75 def _validate_integer(value, key): | |
| 76 try: | |
| 77 int(value) | |
| 78 except ValueError: | |
| 79 _fail(value, key) | |
| 80 | |
| 81 | |
| 82 def _validate_boolean(value, key): | |
| 83 if value not in (True, False): | |
| 84 _fail(value, key) | |
| OLD | NEW |