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

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: 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
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 [ x for (x,_,_,_) in _GetPolicyList(template_file_contents) ]
Mattias Nissler (ping if slow) 2011/10/13 14:14:34 space after comma?
Joao da Silva 2011/10/13 14:40:43 Done.
104
105
106 def _GetChromePolicyList(template_file_contents):
107 return [ (x,y) for (x,y,z,w) in _GetPolicyList(template_file_contents)
Mattias Nissler (ping if slow) 2011/10/13 14:14:34 space after comma?
Joao da Silva 2011/10/13 14:40:43 Done.
108 if "chrome" in z and not w ]
109
110
111 def _GetChromeOSPolicyList(template_file_contents):
112 return [ (x,y) for (x,y,z,w) in _GetPolicyList(template_file_contents)
Mattias Nissler (ping if slow) 2011/10/13 14:14:34 space after comma?
Joao da Silva 2011/10/13 14:40:43 Done.
113 if "chrome_os" in z and not "chrome" in z and not w ]
77 114
78 115
79 def _LoadJSONFile(json_file): 116 def _LoadJSONFile(json_file):
80 with open(json_file, "r") as f: 117 with open(json_file, "r") as f:
81 text = f.read() 118 text = f.read()
82 return eval(text) 119 return eval(text)
83 120
84 121
85 #------------------ policy constants header ------------------------# 122 #------------------ policy constants header ------------------------#
86 def _WritePolicyConstantHeader(template_file_contents, args, opts): 123 def _WritePolicyConstantHeader(template_file_contents, args, opts):
87 platform = args[0]; 124 platform = args[0];
88 with open(opts.header_path, "w") as f: 125 with open(opts.header_path, "w") as f:
89 _OutputGeneratedWarningForC(f, args[1]) 126 _OutputGeneratedWarningForC(f, args[1])
127
90 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' 128 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n'
91 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' 129 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n'
92 '#pragma once\n' 130 '#pragma once\n'
93 '\n' 131 '\n'
132 '#include "base/values.h"\n'
133 '#include "policy/configuration_policy_type.h"\n'
134 '\n'
94 'namespace policy {\n\n') 135 'namespace policy {\n\n')
136
95 if platform == "win": 137 if platform == "win":
96 f.write('// The windows registry path where policy configuration ' 138 f.write('// The windows registry path where policy configuration '
97 'resides.\nextern const wchar_t kRegistrySubKey[];\n\n') 139 'resides.\n'
140 'extern const wchar_t kRegistrySubKey[];\n\n')
141
142 f.write('// Lists policy types mapped to their names and expected types.\n'
143 '// Used to initialize ConfigurationPolicyProviders.\n'
144 'struct PolicyDefinitionList {\n'
145 ' struct Entry {\n'
146 ' ConfigurationPolicyType policy_type;\n'
147 ' base::Value::Type value_type;\n'
148 ' const char* name;\n'
149 ' };\n'
150 '\n'
151 ' const Entry* begin;\n'
152 ' const Entry* end;\n'
153 '};\n'
154 '\n'
155 '// Gets the policy name indexed by the policy type.\n'
Mattias Nissler (ping if slow) 2011/10/13 14:14:34 s/indexed by the/for the given/g
Joao da Silva 2011/10/13 14:40:43 Done.
156 'const char* GetPolicyName(ConfigurationPolicyType type);\n'
157 '\n'
158 '// Returns the default policy definition list for Chrome.\n'
159 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n')
98 f.write('// Key names for the policy settings.\n' 160 f.write('// Key names for the policy settings.\n'
99 'namespace key {\n\n') 161 'namespace key {\n\n')
100 for policy_name in _GetPolicyNameList(template_file_contents): 162 for policy_name in _GetPolicyNameList(template_file_contents):
101 f.write('extern const char k' + policy_name + '[];\n') 163 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' 164 f.write('\n} // namespace key\n\n'
105 '} // namespace policy\n\n' 165 '} // namespace policy\n\n'
106 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') 166 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n')
107 167
108 168
109 #------------------ policy constants source ------------------------# 169 #------------------ policy constants source ------------------------#
110 def _WritePolicyConstantSource(template_file_contents, args, opts): 170 def _WritePolicyConstantSource(template_file_contents, args, opts):
111 platform = args[0]; 171 platform = args[0];
112 with open(opts.source_path, "w") as f: 172 with open(opts.source_path, "w") as f:
113 _OutputGeneratedWarningForC(f, args[1]) 173 _OutputGeneratedWarningForC(f, args[1])
114 f.write('#include "policy/policy_constants.h"\n' 174
175 f.write('#include "base/basictypes.h"\n'
176 '#include "base/logging.h"\n'
177 '#include "policy/policy_constants.h"\n'
115 '\n' 178 '\n'
116 'namespace policy {\n' 179 'namespace policy {\n\n')
117 '\n') 180
181 f.write('namespace {\n\n')
182
183 f.write('PolicyDefinitionList::Entry entries[] = {\n')
184 for (name, vtype) in _GetChromePolicyList(template_file_contents):
185 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name))
186 f.write('\n#if defined(OS_CHROMEOS)\n')
187 for (name, vtype) in _GetChromeOSPolicyList(template_file_contents):
188 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name))
189 f.write('#endif\n'
190 '};\n\n')
191
192 f.write('PolicyDefinitionList policy_list = {\n'
193 ' entries,\n'
194 ' entries + arraysize(entries),\n'
195 '};\n\n')
196
197 f.write('} // namespace\n\n')
198
118 if platform == "win": 199 if platform == "win":
119 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' 200 f.write('#if defined(GOOGLE_CHROME_BUILD)\n'
120 'const wchar_t kRegistrySubKey[] = ' 201 'const wchar_t kRegistrySubKey[] = '
121 'L"' + CHROME_SUBKEY + '";\n' 202 'L"' + CHROME_SUBKEY + '";\n'
122 '#else\n' 203 '#else\n'
123 'const wchar_t kRegistrySubKey[] = ' 204 'const wchar_t kRegistrySubKey[] = '
124 'L"' + CHROMIUM_SUBKEY + '";\n' 205 'L"' + CHROMIUM_SUBKEY + '";\n'
125 '#endif\n\n') 206 '#endif\n\n')
207
208 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n'
209 ' const PolicyDefinitionList::Entry* entry;\n'
210 ' for (entry = policy_list.begin; entry != policy_list.end;'
211 ' ++entry) {\n'
Mattias Nissler (ping if slow) 2011/10/13 14:14:34 Please indent this line by two spaces if python al
Joao da Silva 2011/10/13 14:40:43 Obsolete; this became an array lookup as discussed
212 ' if (entry->policy_type == type)\n'
213 ' return entry->name;\n'
214 ' }\n'
215 ' NOTREACHED();\n'
216 ' return NULL;\n'
217 '}\n'
218 '\n'
219 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n'
220 ' return &policy_list;\n'
221 '}\n\n')
222
126 f.write('namespace key {\n\n') 223 f.write('namespace key {\n\n')
127 for policy_name in _GetPolicyNameList(template_file_contents): 224 for policy_name in _GetPolicyNameList(template_file_contents):
128 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) 225 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' 226 f.write('\n} // namespace key\n\n'
135 '} // namespace policy\n') 227 '} // namespace policy\n')
136 228
137 229
138 #------------------ policy type enumeration header -----------------# 230 #------------------ policy type enumeration header -----------------#
139 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): 231 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts):
140 with open(opts.type_path, "w") as f: 232 with open(opts.type_path, "w") as f:
141 _OutputGeneratedWarningForC(f, args[1]) 233 _OutputGeneratedWarningForC(f, args[1])
142 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' 234 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n'
143 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' 235 '#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']: 429 for sub_policy in policy['policies']:
338 _WritePolicyCode(f, sub_policy) 430 _WritePolicyCode(f, sub_policy)
339 else: 431 else:
340 _WritePolicyCode(f, policy) 432 _WritePolicyCode(f, policy)
341 f.write(CPP_FOOT) 433 f.write(CPP_FOOT)
342 434
343 435
344 #------------------ main() -----------------------------------------# 436 #------------------ main() -----------------------------------------#
345 if __name__ == '__main__': 437 if __name__ == '__main__':
346 main(); 438 main();
OLDNEW
« chrome/browser/policy/policy_map.h ('K') | « 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