Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1910)

Side by Side Diff: chrome/tools/build/generate_policy_source.py

Issue 8258018: Generate Chrome policy definition list from policy_templates.json. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix the unittest fix Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/policy/user_policy_cache.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
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 '''python %prog [options] platform template 6 '''python %prog [options] platform template
7 7
8 platform specifies which platform source is being generated for 8 platform specifies which platform source is being generated for
9 and can be one of (win, mac, linux). 9 and can be one of (win, mac, linux).
10 template is the path to a .json policy template file.''' 10 template is the path to a .json policy template file.'''
11 11
12 from __future__ import with_statement 12 from __future__ import with_statement
13 from optparse import OptionParser 13 from optparse import OptionParser
14 import sys; 14 import sys;
15 15
16 16
17 CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'; 17 CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome';
18 CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium'; 18 CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium';
19 19
20 TYPE_MAP = {
21 'int': 'TYPE_INTEGER',
22 'int-enum': 'TYPE_INTEGER',
23 'list': 'TYPE_LIST',
24 'main': 'TYPE_BOOLEAN',
25 'string': 'TYPE_STRING',
26 'string-enum': 'TYPE_STRING',
27 }
28
20 29
21 def main(): 30 def main():
22 parser = OptionParser(usage=__doc__); 31 parser = OptionParser(usage=__doc__);
23 parser.add_option("--pch", "--policy-constants-header", dest="header_path", 32 parser.add_option("--pch", "--policy-constants-header", dest="header_path",
24 help="generate header file of policy constants", 33 help="generate header file of policy constants",
25 metavar="FILE"); 34 metavar="FILE");
26 parser.add_option("--pcc", "--policy-constants-source", dest="source_path", 35 parser.add_option("--pcc", "--policy-constants-source", dest="source_path",
27 help="generate source file of policy constants", 36 help="generate source file of policy constants",
28 metavar="FILE"); 37 metavar="FILE");
29 parser.add_option("--pth", "--policy-type-header", dest="type_path", 38 parser.add_option("--pth", "--policy-type-header", dest="type_path",
(...skipping 27 matching lines...) Expand all
57 66
58 #------------------ shared helpers ---------------------------------# 67 #------------------ shared helpers ---------------------------------#
59 def _OutputGeneratedWarningForC(f, template_file_path): 68 def _OutputGeneratedWarningForC(f, template_file_path):
60 f.write('//\n' 69 f.write('//\n'
61 '// DO NOT MODIFY THIS FILE DIRECTLY!\n' 70 '// DO NOT MODIFY THIS FILE DIRECTLY!\n'
62 '// IT IS GENERATED BY generate_policy_source.py\n' 71 '// IT IS GENERATED BY generate_policy_source.py\n'
63 '// FROM ' + template_file_path + '\n' 72 '// FROM ' + template_file_path + '\n'
64 '//\n\n') 73 '//\n\n')
65 74
66 75
67 def _GetPolicyNameList(template_file_contents): 76 # Returns a tuple with details about the given policy:
68 policy_names = []; 77 # (name, type, list_of_platforms, is_device_policy)
78 def _GetPolicyDetails(policy):
79 if not TYPE_MAP.has_key(policy['type']):
80 print "Unknown policy type for %s: %s" % (policy['name'], policy['type'])
81 sys.exit(3)
82 # platforms is a list of "chrome", "chrome_os" and/or "chrome_frame".
83 platforms = [ x.split('.')[0].split(':')[0] for x in policy['supported_on'] ]
84 is_device_policy = policy.get('device_only', False)
85 return (policy['name'], TYPE_MAP[policy['type']], platforms, is_device_policy)
86
87
88 def _GetPolicyList(template_file_contents):
89 policies = [];
69 for policy in template_file_contents['policy_definitions']: 90 for policy in template_file_contents['policy_definitions']:
70 if policy['type'] == 'group': 91 if policy['type'] == 'group':
71 for sub_policy in policy['policies']: 92 for sub_policy in policy['policies']:
72 policy_names.append(sub_policy['name']) 93 policies.append(_GetPolicyDetails(sub_policy))
73 else: 94 else:
74 policy_names.append(policy['name']) 95 policies.append(_GetPolicyDetails(policy))
75 policy_names.sort() 96 # Tuples are sorted in lexicographical order, which will sort by policy name
76 return policy_names 97 # in this case.
98 policies.sort()
99 return policies
100
101
102 def _GetPolicyNameList(template_file_contents):
103 return [name for (name, _, _, _) in _GetPolicyList(template_file_contents)]
104
105
106 def _GetChromePolicyList(template_file_contents):
107 return [(name, vtype) for (name, vtype, platforms, is_device_only)
108 in _GetPolicyList(template_file_contents)
109 if 'chrome' in platforms and not is_device_only]
110
111
112 def _GetChromeOSPolicyList(template_file_contents):
113 return [(name, vtype) for (name, vtype, platforms, is_device_only)
114 in _GetPolicyList(template_file_contents)
115 if 'chrome_os' in platforms
116 and not 'chrome' in platforms
117 and not is_device_only]
77 118
78 119
79 def _LoadJSONFile(json_file): 120 def _LoadJSONFile(json_file):
80 with open(json_file, "r") as f: 121 with open(json_file, "r") as f:
81 text = f.read() 122 text = f.read()
82 return eval(text) 123 return eval(text)
83 124
84 125
85 #------------------ policy constants header ------------------------# 126 #------------------ policy constants header ------------------------#
86 def _WritePolicyConstantHeader(template_file_contents, args, opts): 127 def _WritePolicyConstantHeader(template_file_contents, args, opts):
87 platform = args[0]; 128 platform = args[0];
88 with open(opts.header_path, "w") as f: 129 with open(opts.header_path, "w") as f:
89 _OutputGeneratedWarningForC(f, args[1]) 130 _OutputGeneratedWarningForC(f, args[1])
131
90 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' 132 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n'
91 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' 133 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n'
92 '#pragma once\n' 134 '#pragma once\n'
93 '\n' 135 '\n'
136 '#include "base/values.h"\n'
137 '#include "policy/configuration_policy_type.h"\n'
138 '\n'
94 'namespace policy {\n\n') 139 'namespace policy {\n\n')
140
95 if platform == "win": 141 if platform == "win":
96 f.write('// The windows registry path where policy configuration ' 142 f.write('// The windows registry path where policy configuration '
97 'resides.\nextern const wchar_t kRegistrySubKey[];\n\n') 143 'resides.\n'
144 'extern const wchar_t kRegistrySubKey[];\n\n')
145
146 f.write('// Lists policy types mapped to their names and expected types.\n'
147 '// Used to initialize ConfigurationPolicyProviders.\n'
148 'struct PolicyDefinitionList {\n'
149 ' struct Entry {\n'
150 ' ConfigurationPolicyType policy_type;\n'
151 ' base::Value::Type value_type;\n'
152 ' const char* name;\n'
153 ' };\n'
154 '\n'
155 ' const Entry* begin;\n'
156 ' const Entry* end;\n'
157 '};\n'
158 '\n'
159 '// Gets the policy name for the given policy type.\n'
160 'const char* GetPolicyName(ConfigurationPolicyType type);\n'
161 '\n'
162 '// Returns the default policy definition list for Chrome.\n'
163 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n')
98 f.write('// Key names for the policy settings.\n' 164 f.write('// Key names for the policy settings.\n'
99 'namespace key {\n\n') 165 'namespace key {\n\n')
100 for policy_name in _GetPolicyNameList(template_file_contents): 166 for policy_name in _GetPolicyNameList(template_file_contents):
101 f.write('extern const char k' + policy_name + '[];\n') 167 f.write('extern const char k' + policy_name + '[];\n')
102 f.write('\n// Only used in testing.'
103 '\nextern const char* kMapPolicyString[];\n')
104 f.write('\n} // namespace key\n\n' 168 f.write('\n} // namespace key\n\n'
105 '} // namespace policy\n\n' 169 '} // namespace policy\n\n'
106 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') 170 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n')
107 171
108 172
109 #------------------ policy constants source ------------------------# 173 #------------------ policy constants source ------------------------#
110 def _WritePolicyConstantSource(template_file_contents, args, opts): 174 def _WritePolicyConstantSource(template_file_contents, args, opts):
111 platform = args[0]; 175 platform = args[0];
112 with open(opts.source_path, "w") as f: 176 with open(opts.source_path, "w") as f:
113 _OutputGeneratedWarningForC(f, args[1]) 177 _OutputGeneratedWarningForC(f, args[1])
114 f.write('#include "policy/policy_constants.h"\n' 178
179 f.write('#include "base/basictypes.h"\n'
180 '#include "base/logging.h"\n'
181 '#include "policy/policy_constants.h"\n'
115 '\n' 182 '\n'
116 'namespace policy {\n' 183 'namespace policy {\n\n')
117 '\n') 184
185 f.write('namespace {\n\n')
186
187 f.write('PolicyDefinitionList::Entry entries[] = {\n')
188 for (name, vtype) in _GetChromePolicyList(template_file_contents):
189 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name))
190 f.write('\n#if defined(OS_CHROMEOS)\n')
191 for (name, vtype) in _GetChromeOSPolicyList(template_file_contents):
192 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name))
193 f.write('#endif\n'
194 '};\n\n')
195
196 f.write('PolicyDefinitionList chrome_policy_list = {\n'
197 ' entries,\n'
198 ' entries + arraysize(entries),\n'
199 '};\n\n')
200
201 f.write('// Maps a policy-type enum value to the policy name.\n'
202 'const char* policy_name_map[] = {\n');
203 for name in _GetPolicyNameList(template_file_contents):
204 f.write(' key::k%s,\n' % name)
205 f.write('};\n\n')
206
207 f.write('} // namespace\n\n')
208
118 if platform == "win": 209 if platform == "win":
119 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' 210 f.write('#if defined(GOOGLE_CHROME_BUILD)\n'
120 'const wchar_t kRegistrySubKey[] = ' 211 'const wchar_t kRegistrySubKey[] = '
121 'L"' + CHROME_SUBKEY + '";\n' 212 'L"' + CHROME_SUBKEY + '";\n'
122 '#else\n' 213 '#else\n'
123 'const wchar_t kRegistrySubKey[] = ' 214 'const wchar_t kRegistrySubKey[] = '
124 'L"' + CHROMIUM_SUBKEY + '";\n' 215 'L"' + CHROMIUM_SUBKEY + '";\n'
125 '#endif\n\n') 216 '#endif\n\n')
217
218 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n'
219 ' CHECK(type >= 0 && '
220 'static_cast<size_t>(type) < arraysize(policy_name_map));\n'
221 ' return policy_name_map[type];\n'
222 '}\n'
223 '\n'
224 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n'
225 ' return &chrome_policy_list;\n'
226 '}\n\n')
227
126 f.write('namespace key {\n\n') 228 f.write('namespace key {\n\n')
127 for policy_name in _GetPolicyNameList(template_file_contents): 229 for policy_name in _GetPolicyNameList(template_file_contents):
128 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) 230 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name))
129 f.write('\n// Only used in testing.'
130 '\nconst char* kMapPolicyString[] = {\n ')
131 for policy_name in _GetPolicyNameList(template_file_contents):
132 f.write('\n "%s",' % policy_name)
133 f.write('\n};\n')
134 f.write('\n} // namespace key\n\n' 231 f.write('\n} // namespace key\n\n'
135 '} // namespace policy\n') 232 '} // namespace policy\n')
136 233
137 234
138 #------------------ policy type enumeration header -----------------# 235 #------------------ policy type enumeration header -----------------#
139 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): 236 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts):
140 with open(opts.type_path, "w") as f: 237 with open(opts.type_path, "w") as f:
141 _OutputGeneratedWarningForC(f, args[1]) 238 _OutputGeneratedWarningForC(f, args[1])
142 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' 239 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n'
143 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' 240 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n'
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 for sub_policy in policy['policies']: 434 for sub_policy in policy['policies']:
338 _WritePolicyCode(f, sub_policy) 435 _WritePolicyCode(f, sub_policy)
339 else: 436 else:
340 _WritePolicyCode(f, policy) 437 _WritePolicyCode(f, policy)
341 f.write(CPP_FOOT) 438 f.write(CPP_FOOT)
342 439
343 440
344 #------------------ main() -----------------------------------------# 441 #------------------ main() -----------------------------------------#
345 if __name__ == '__main__': 442 if __name__ == '__main__':
346 main(); 443 main();
OLDNEW
« no previous file with comments | « chrome/browser/policy/user_policy_cache.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698