OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 # If this presubmit check fails or misbehaves, please complain to | |
6 # mnissler@chromium.org, pastarmovj@chromium.org or joaodasilva@chromium.org. | |
7 | |
8 import itertools | |
9 import sys | |
10 import xml.dom.minidom | |
11 | |
12 def _GetPolicyTemplates(template_path): | |
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 | |
15 # python features such as #-comments and '''strings'''. policy_templates.json | |
16 # is actually maintained as a python dictionary. | |
17 with open(template_path) as f: | |
18 template_data = eval(f.read(), {}) | |
19 policies = ( policy | |
20 for policy in template_data['policy_definitions'] | |
21 if policy['type'] != 'group' ) | |
22 groups = ( policy['policies'] | |
23 for policy in template_data['policy_definitions'] | |
24 if policy['type'] == 'group' ) | |
25 subpolicies = ( policy for group in groups for policy in group ) | |
26 return list(itertools.chain(policies, subpolicies)) | |
27 | |
28 def _CheckPolicyTemplatesSyntax(input_api, output_api): | |
29 filepath = input_api.os_path.join(input_api.PresubmitLocalPath(), | |
30 'policy_templates.json') | |
31 if any(f.AbsoluteLocalPath() == filepath | |
32 for f in input_api.AffectedFiles()): | |
33 old_sys_path = sys.path | |
34 try: | |
35 sys.path = [input_api.PresubmitLocalPath()] + sys.path | |
36 # Optimization: only load this when it's needed. | |
37 import syntax_check_policy_template_json | |
38 checker = syntax_check_policy_template_json.PolicyTemplateChecker() | |
39 if checker.Run([], filepath) > 0: | |
40 return [output_api.PresubmitError('Syntax error(s) in file:', | |
41 [filepath])] | |
42 finally: | |
43 sys.path = old_sys_path | |
44 return [] | |
45 | |
46 | |
47 def _CheckPolicyTestCases(input_api, output_api, policies): | |
48 # Read list of policies in chrome/test/data/policy/policy_test_cases.json. | |
49 root = input_api.change.RepositoryRoot() | |
50 policy_test_cases_file = input_api.os_path.join( | |
51 root, 'chrome', 'test', 'data', 'policy', 'policy_test_cases.json') | |
52 test_names = input_api.json.load(open(policy_test_cases_file)).keys() | |
53 tested_policies = frozenset(name for name in test_names if name[:2] != '--') | |
54 policy_names = frozenset(policy['name'] for policy in policies) | |
55 | |
56 # Finally check if any policies are missing. | |
57 missing = policy_names - tested_policies | |
58 extra = tested_policies - policy_names | |
59 error_missing = ('Policy \'%s\' was added to policy_templates.json but not ' | |
60 'to src/chrome/test/data/policy/policy_test_cases.json. ' | |
61 'Please update both files.') | |
62 error_extra = ('Policy \'%s\' is tested by ' | |
63 'src/chrome/test/data/policy/policy_test_cases.json but is not' | |
64 ' defined in policy_templates.json. Please update both files.') | |
65 results = [] | |
66 for policy in missing: | |
67 results.append(output_api.PresubmitError(error_missing % policy)) | |
68 for policy in extra: | |
69 results.append(output_api.PresubmitError(error_extra % policy)) | |
70 return results | |
71 | |
72 | |
73 def _CheckPolicyHistograms(input_api, output_api, policies): | |
74 root = input_api.change.RepositoryRoot() | |
75 histograms = input_api.os_path.join( | |
76 root, 'tools', 'metrics', 'histograms', 'histograms.xml') | |
77 with open(histograms) as f: | |
78 tree = xml.dom.minidom.parseString(f.read()) | |
79 enums = (tree.getElementsByTagName('histogram-configuration')[0] | |
80 .getElementsByTagName('enums')[0] | |
81 .getElementsByTagName('enum')) | |
82 policy_enum = [e for e in enums | |
83 if e.getAttribute('name') == 'EnterprisePolicies'][0] | |
84 policy_ids = frozenset([int(e.getAttribute('value')) | |
85 for e in policy_enum.getElementsByTagName('int')]) | |
86 | |
87 error_missing = ('Policy \'%s\' was added to policy_templates.json but not ' | |
88 'to src/tools/metrics/histograms/histograms.xml. ' | |
89 'Please update both files.') | |
90 results = [] | |
91 for policy in policies: | |
92 if policy['id'] not in policy_ids: | |
93 results.append(output_api.PresubmitError(error_missing % policy['name'])) | |
94 return results | |
95 | |
96 | |
97 def _CommonChecks(input_api, output_api): | |
98 results = [] | |
99 results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api)) | |
100 | |
101 os_path = input_api.os_path | |
102 local_path = input_api.PresubmitLocalPath() | |
103 template_path = os_path.join(local_path, 'policy_templates.json') | |
104 affected_files = input_api.AffectedFiles() | |
105 if any(f.AbsoluteLocalPath() == template_path for f in affected_files): | |
106 policies = _GetPolicyTemplates(template_path) | |
107 results.extend(_CheckPolicyTestCases(input_api, output_api, policies)) | |
108 results.extend(_CheckPolicyHistograms(input_api, output_api, policies)) | |
109 | |
110 return results | |
111 | |
112 | |
113 def CheckChangeOnUpload(input_api, output_api): | |
114 return _CommonChecks(input_api, output_api) | |
115 | |
116 | |
117 def CheckChangeOnCommit(input_api, output_api): | |
118 return _CommonChecks(input_api, output_api) | |
OLD | NEW |