Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 # If this presubmit check fails or misbehaves, please complain to | 5 # If this presubmit check fails or misbehaves, please complain to |
| 6 # mnissler@chromium.org, pastarmovj@chromium.org or joaodasilva@chromium.org. | 6 # mnissler@chromium.org, pastarmovj@chromium.org or joaodasilva@chromium.org. |
| 7 | 7 |
| 8 import itertools | 8 import itertools |
| 9 import sys | 9 import sys |
| 10 import xml.dom.minidom | 10 import xml.dom.minidom |
| 11 | 11 |
| 12 def _GetPolicyTemplates(template_path): | 12 def _GetPolicyTemplates(template_path): |
| 13 # Read list of policies in the template. eval() is used instead of a JSON | 13 # Read list of policies in the template. eval() is used instead of a JSON |
| 14 # parser because policy_templates.json is not quite JSON, and uses some | 14 # parser because policy_templates.json is not quite JSON, and uses some |
| 15 # python features such as #-comments and '''strings'''. policy_templates.json | 15 # python features such as #-comments and '''strings'''. policy_templates.json |
| 16 # is actually maintained as a python dictionary. | 16 # is actually maintained as a python dictionary. |
| 17 with open(template_path) as f: | 17 with open(template_path) as f: |
| 18 template_data = eval(f.read(), {}) | 18 template_data = eval(f.read(), {}) |
| 19 policies = ( policy | 19 try: |
| 20 policies = ( policy | |
| 21 for policy in template_data['policy_definitions'] | |
| 22 if policy['type'] != 'group' ) | |
| 23 groups = ( policy['policies'] | |
| 20 for policy in template_data['policy_definitions'] | 24 for policy in template_data['policy_definitions'] |
| 21 if policy['type'] != 'group' ) | 25 if policy['type'] == 'group' ) |
| 22 groups = ( policy['policies'] | 26 subpolicies = ( policy for group in groups for policy in group ) |
| 23 for policy in template_data['policy_definitions'] | 27 return list(itertools.chain(policies, subpolicies)) |
| 24 if policy['type'] == 'group' ) | 28 except: |
| 25 subpolicies = ( policy for group in groups for policy in group ) | 29 return list() |
| 26 return list(itertools.chain(policies, subpolicies)) | |
| 27 | 30 |
| 28 def _CheckPolicyTemplatesSyntax(input_api, output_api): | 31 def _CheckPolicyTemplatesSyntax(input_api, output_api): |
| 29 local_path = input_api.PresubmitLocalPath() | 32 local_path = input_api.PresubmitLocalPath() |
| 30 filepath = input_api.os_path.join(local_path, 'policy_templates.json') | 33 filepath = input_api.os_path.join(local_path, 'policy_templates.json') |
| 31 if any(f.AbsoluteLocalPath() == filepath | 34 if any(f.AbsoluteLocalPath() == filepath |
| 32 for f in input_api.AffectedFiles()): | 35 for f in input_api.AffectedFiles()): |
| 33 old_sys_path = sys.path | 36 old_sys_path = sys.path |
| 34 try: | 37 try: |
| 35 tools_path = input_api.os_path.normpath( | 38 tools_path = input_api.os_path.normpath( |
| 36 input_api.os_path.join(local_path, input_api.os_path.pardir, 'tools')) | 39 input_api.os_path.join(local_path, input_api.os_path.pardir, 'tools')) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 | 101 |
| 99 def _CommonChecks(input_api, output_api): | 102 def _CommonChecks(input_api, output_api): |
| 100 results = [] | 103 results = [] |
| 101 results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api)) | 104 results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api)) |
| 102 | 105 |
| 103 os_path = input_api.os_path | 106 os_path = input_api.os_path |
| 104 local_path = input_api.PresubmitLocalPath() | 107 local_path = input_api.PresubmitLocalPath() |
| 105 template_path = os_path.join(local_path, 'policy_templates.json') | 108 template_path = os_path.join(local_path, 'policy_templates.json') |
| 106 affected_files = input_api.AffectedFiles() | 109 affected_files = input_api.AffectedFiles() |
| 107 if any(f.AbsoluteLocalPath() == template_path for f in affected_files): | 110 if any(f.AbsoluteLocalPath() == template_path for f in affected_files): |
| 108 policies = _GetPolicyTemplates(template_path) | 111 policies = _GetPolicyTemplates(template_path) |
|
Joao da Silva
2014/04/24 14:34:49
I suggest you do the try/catch here instead. If _G
Thiemo Nagel
2014/04/24 17:16:24
Done.
| |
| 109 results.extend(_CheckPolicyTestCases(input_api, output_api, policies)) | 112 results.extend(_CheckPolicyTestCases(input_api, output_api, policies)) |
| 110 results.extend(_CheckPolicyHistograms(input_api, output_api, policies)) | 113 results.extend(_CheckPolicyHistograms(input_api, output_api, policies)) |
| 111 | 114 |
| 112 return results | 115 return results |
| 113 | 116 |
| 114 | 117 |
| 115 def CheckChangeOnUpload(input_api, output_api): | 118 def CheckChangeOnUpload(input_api, output_api): |
| 116 return _CommonChecks(input_api, output_api) | 119 return _CommonChecks(input_api, output_api) |
| 117 | 120 |
| 118 | 121 |
| 119 def CheckChangeOnCommit(input_api, output_api): | 122 def CheckChangeOnCommit(input_api, output_api): |
| 120 return _CommonChecks(input_api, output_api) | 123 return _CommonChecks(input_api, output_api) |
| OLD | NEW |