| OLD | NEW |
| 1 #!/usr/bin/python2 | 1 #!/usr/bin/python2 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 ''' | 6 ''' |
| 7 Checks a policy_templates.json file for conformity to its syntax specification. | 7 Checks a policy_templates.json file for conformity to its syntax specification. |
| 8 ''' | 8 ''' |
| 9 | 9 |
| 10 import json | 10 import json |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 (container_name.title(), value_type.__name__, key), | 74 (container_name.title(), value_type.__name__, key), |
| 75 container_name, identifier, offending) | 75 container_name, identifier, offending) |
| 76 return None | 76 return None |
| 77 value = container[key] | 77 value = container[key] |
| 78 if not isinstance(value, value_type): | 78 if not isinstance(value, value_type): |
| 79 self._Error('Value of "%s" must be a %s.' % | 79 self._Error('Value of "%s" must be a %s.' % |
| 80 (key, value_type.__name__), | 80 (key, value_type.__name__), |
| 81 container_name, identifier, value) | 81 container_name, identifier, value) |
| 82 return value | 82 return value |
| 83 | 83 |
| 84 def _CheckPolicy(self, policy, may_contain_groups): | 84 def _AddPolicyID(self, id, policy_ids, policy): |
| 85 ''' |
| 86 Adds |id| to |policy_ids|. Generates an error message if the |
| 87 |id| exists already; |policy| is needed for this message. |
| 88 ''' |
| 89 if id in policy_ids: |
| 90 self._Error('Duplicate id', 'policy', policy.get('name'), |
| 91 id) |
| 92 else: |
| 93 policy_ids.add(id) |
| 94 |
| 95 def _CheckPolicyIDs(self, policy_ids): |
| 96 ''' |
| 97 Checks a set of policy_ids to make sure it contains a continuous range |
| 98 of entries (i.e. no holes). |
| 99 Holes would not be a technical problem, but we want to ensure that nobody |
| 100 accidentally omits IDs. |
| 101 ''' |
| 102 for i in range(len(policy_ids)): |
| 103 if (i + 1) not in policy_ids: |
| 104 self._Error('No policy with id: %s' % (i + 1)) |
| 105 |
| 106 def _CheckPolicy(self, policy, is_in_group, policy_ids): |
| 85 if not isinstance(policy, dict): | 107 if not isinstance(policy, dict): |
| 86 self._Error('Each policy must be a dictionary.', 'policy', None, policy) | 108 self._Error('Each policy must be a dictionary.', 'policy', None, policy) |
| 87 return | 109 return |
| 88 | 110 |
| 89 # There should not be any unknown keys in |policy|. | 111 # There should not be any unknown keys in |policy|. |
| 90 for key in policy: | 112 for key in policy: |
| 91 if key not in ('name', 'type', 'caption', 'desc', 'supported_on', | 113 if key not in ('name', 'type', 'caption', 'desc', 'supported_on', |
| 92 'label', 'policies', 'items', 'example_value', 'features', | 114 'label', 'policies', 'items', 'example_value', 'features', |
| 93 'deprecated', 'future'): | 115 'deprecated', 'future', 'id'): |
| 94 self.warning_count += 1 | 116 self.warning_count += 1 |
| 95 print ('In policy %s: Warning: Unknown key: %s' % | 117 print ('In policy %s: Warning: Unknown key: %s' % |
| 96 (policy.get('name'), key)) | 118 (policy.get('name'), key)) |
| 97 | 119 |
| 98 # Each policy must have a name. | 120 # Each policy must have a name. |
| 99 self._CheckContains(policy, 'name', str) | 121 self._CheckContains(policy, 'name', str) |
| 100 | 122 |
| 101 # Each policy must have a type. | 123 # Each policy must have a type. |
| 102 policy_type = self._CheckContains(policy, 'type', str) | 124 policy_type = self._CheckContains(policy, 'type', str) |
| 103 if policy_type not in ('group', 'main', 'string', 'int', 'list', 'int-enum', | 125 if policy_type not in ('group', 'main', 'string', 'int', 'list', 'int-enum', |
| (...skipping 13 matching lines...) Expand all Loading... |
| 117 | 139 |
| 118 # If 'deprecated' is present, it must be a bool. | 140 # If 'deprecated' is present, it must be a bool. |
| 119 self._CheckContains(policy, 'deprecated', bool, True) | 141 self._CheckContains(policy, 'deprecated', bool, True) |
| 120 | 142 |
| 121 # If 'future' is present, it must be a bool. | 143 # If 'future' is present, it must be a bool. |
| 122 self._CheckContains(policy, 'future', bool, True) | 144 self._CheckContains(policy, 'future', bool, True) |
| 123 | 145 |
| 124 if policy_type == 'group': | 146 if policy_type == 'group': |
| 125 | 147 |
| 126 # Groups must not be nested. | 148 # Groups must not be nested. |
| 127 if not may_contain_groups: | 149 if is_in_group: |
| 128 self._Error('Policy groups must not be nested.', 'policy', policy) | 150 self._Error('Policy groups must not be nested.', 'policy', policy) |
| 129 | 151 |
| 130 # Each policy group must have a list of policies. | 152 # Each policy group must have a list of policies. |
| 131 policies = self._CheckContains(policy, 'policies', list) | 153 policies = self._CheckContains(policy, 'policies', list) |
| 154 |
| 155 # Check sub-policies. |
| 132 if policies is not None: | 156 if policies is not None: |
| 133 for nested_policy in policies: | 157 for nested_policy in policies: |
| 134 self._CheckPolicy(nested_policy, False) | 158 self._CheckPolicy(nested_policy, True, policy_ids) |
| 159 |
| 160 # Groups must not have an |id|. |
| 161 if 'id' in policy: |
| 162 self._Error('Policies of type "group" must not have an "id" field.', |
| 163 'policy', policy) |
| 135 | 164 |
| 136 # Statistics. | 165 # Statistics. |
| 137 self.num_groups += 1 | 166 self.num_groups += 1 |
| 138 else: # policy_type != group | 167 else: # policy_type != group |
| 139 | 168 |
| 169 # Each policy must have a protobuf ID. |
| 170 id = self._CheckContains(policy, 'id', int) |
| 171 self._AddPolicyID(id, policy_ids, policy) |
| 172 |
| 140 # Each policy must have a supported_on list. | 173 # Each policy must have a supported_on list. |
| 141 supported_on = self._CheckContains(policy, 'supported_on', list) | 174 supported_on = self._CheckContains(policy, 'supported_on', list) |
| 142 if supported_on is not None: | 175 if supported_on is not None: |
| 143 for s in supported_on: | 176 for s in supported_on: |
| 144 if not isinstance(s, str): | 177 if not isinstance(s, str): |
| 145 self._Error('Entries in "supported_on" must be strings.', 'policy', | 178 self._Error('Entries in "supported_on" must be strings.', 'policy', |
| 146 policy, supported_on) | 179 policy, supported_on) |
| 147 | 180 |
| 148 # Each policy must have a 'features' dict. | 181 # Each policy must have a 'features' dict. |
| 149 self._CheckContains(policy, 'features', dict) | 182 self._CheckContains(policy, 'features', dict) |
| 150 | 183 |
| 151 # Each policy must have an 'example_value' of appropriate type. | 184 # Each policy must have an 'example_value' of appropriate type. |
| 152 if policy_type == 'main': | 185 if policy_type == 'main': |
| 153 value_type = bool | 186 value_type = bool |
| 154 elif policy_type in ('string', 'string-enum'): | 187 elif policy_type in ('string', 'string-enum'): |
| 155 value_type = str | 188 value_type = str |
| 156 elif policy_type in ('int', 'int-enum'): | 189 elif policy_type in ('int', 'int-enum'): |
| 157 value_type = int | 190 value_type = int |
| 158 elif policy_type == 'list': | 191 elif policy_type == 'list': |
| 159 value_type = list | 192 value_type = list |
| 160 else: | 193 else: |
| 161 raise NotImplementedError('Unimplemented policy type: %s' % policy_type) | 194 raise NotImplementedError('Unimplemented policy type: %s' % policy_type) |
| 162 self._CheckContains(policy, 'example_value', value_type) | 195 self._CheckContains(policy, 'example_value', value_type) |
| 163 | 196 |
| 164 # Statistics. | 197 # Statistics. |
| 165 self.num_policies += 1 | 198 self.num_policies += 1 |
| 166 if not may_contain_groups: | 199 if is_in_group: |
| 167 self.num_policies_in_groups += 1 | 200 self.num_policies_in_groups += 1 |
| 168 | 201 |
| 169 if policy_type in ('int-enum', 'string-enum'): | 202 if policy_type in ('int-enum', 'string-enum'): |
| 170 | 203 |
| 171 # Enums must contain a list of items. | 204 # Enums must contain a list of items. |
| 172 items = self._CheckContains(policy, 'items', list) | 205 items = self._CheckContains(policy, 'items', list) |
| 173 if items is not None: | 206 if items is not None: |
| 174 if len(items) < 1: | 207 if len(items) < 1: |
| 175 self._Error('"items" must not be empty.', 'policy', policy, items) | 208 self._Error('"items" must not be empty.', 'policy', policy, items) |
| 176 for item in items: | 209 for item in items: |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 self.options = options | 362 self.options = options |
| 330 | 363 |
| 331 # First part: check JSON structure. | 364 # First part: check JSON structure. |
| 332 | 365 |
| 333 # Check policy definitions. | 366 # Check policy definitions. |
| 334 policy_definitions = self._CheckContains(data, 'policy_definitions', list, | 367 policy_definitions = self._CheckContains(data, 'policy_definitions', list, |
| 335 parent_element=None, | 368 parent_element=None, |
| 336 container_name='The root element', | 369 container_name='The root element', |
| 337 offending=None) | 370 offending=None) |
| 338 if policy_definitions is not None: | 371 if policy_definitions is not None: |
| 372 policy_ids = set() |
| 339 for policy in policy_definitions: | 373 for policy in policy_definitions: |
| 340 self._CheckPolicy(policy, True) | 374 self._CheckPolicy(policy, False, policy_ids) |
| 375 self._CheckPolicyIDs(policy_ids) |
| 341 | 376 |
| 342 # Check (non-policy-specific) message definitions. | 377 # Check (non-policy-specific) message definitions. |
| 343 messages = self._CheckContains(data, 'messages', dict, | 378 messages = self._CheckContains(data, 'messages', dict, |
| 344 parent_element=None, | 379 parent_element=None, |
| 345 container_name='The root element', | 380 container_name='The root element', |
| 346 offending=None) | 381 offending=None) |
| 347 if messages is not None: | 382 if messages is not None: |
| 348 for message in messages: | 383 for message in messages: |
| 349 self._CheckMessage(message, messages[message]) | 384 self._CheckMessage(message, messages[message]) |
| 350 | 385 |
| 351 # Check placeholders. | 386 # Check placeholders. |
| 352 placeholders = self._CheckContains(data, 'placeholders', list, | 387 placeholders = self._CheckContains(data, 'placeholders', list, |
| 353 parent_element=None, | 388 parent_element=None, |
| 354 container_name='The root element', | 389 container_name='The root element', |
| 355 offending=None) | 390 offending=None) |
| 356 if placeholders is not None: | 391 if placeholders is not None: |
| 357 for placeholder in placeholders: | 392 for placeholder in placeholders: |
| 358 self._CheckPlaceholder(placeholder) | 393 self._CheckPlaceholder(placeholder) |
| 359 | 394 |
| 360 # Second part: check formatting. | 395 # Second part: check formatting. |
| 361 self._CheckFormat(filename) | 396 self._CheckFormat(filename) |
| 362 | 397 |
| 363 # Third part: summary and exit. | 398 # Third part: summary and exit. |
| 364 print ('Finished. %d errors, %d warnings.' % | 399 print ('Finished checking %s. %d errors, %d warnings.' % |
| 365 (self.error_count, self.warning_count)) | 400 (filename, self.error_count, self.warning_count)) |
| 366 if self.options.stats: | 401 if self.options.stats: |
| 367 if self.num_groups > 0: | 402 if self.num_groups > 0: |
| 368 print ('%d policies, %d of those in %d groups (containing on ' | 403 print ('%d policies, %d of those in %d groups (containing on ' |
| 369 'average %.1f policies).' % | 404 'average %.1f policies).' % |
| 370 (self.num_policies, self.num_policies_in_groups, self.num_groups, | 405 (self.num_policies, self.num_policies_in_groups, self.num_groups, |
| 371 (1.0 * self.num_policies_in_groups / self.num_groups))) | 406 (1.0 * self.num_policies_in_groups / self.num_groups))) |
| 372 else: | 407 else: |
| 373 print self.num_policies, 'policies, 0 policy groups.' | 408 print self.num_policies, 'policies, 0 policy groups.' |
| 374 if self.error_count > 0: | 409 if self.error_count > 0: |
| 375 return 1 | 410 return 1 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 390 if len(args) != 2: | 425 if len(args) != 2: |
| 391 parser.print_help() | 426 parser.print_help() |
| 392 sys.exit(1) | 427 sys.exit(1) |
| 393 filename = args[1] | 428 filename = args[1] |
| 394 return self.Main(filename, options) | 429 return self.Main(filename, options) |
| 395 | 430 |
| 396 | 431 |
| 397 if __name__ == '__main__': | 432 if __name__ == '__main__': |
| 398 checker = PolicyTemplateChecker() | 433 checker = PolicyTemplateChecker() |
| 399 sys.exit(checker.Run(sys.argv)) | 434 sys.exit(checker.Run(sys.argv)) |
| OLD | NEW |