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

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

Issue 10916235: Record policy usage statistics every 24 hours. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed comments, moved PolicyStatisticsCollector to BrowserPolicyConnector, fixed policy sorting Created 8 years, 3 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
« chrome/chrome_browser.gypi ('K') | « chrome/common/pref_names.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/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 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 chromium_os_flag template 6 '''python %prog [options] platform chromium_os_flag 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 chromium_os_flag should be 1 if this is a Chromium OS build 10 chromium_os_flag should be 1 if this is a Chromium OS build
11 template is the path to a .json policy template file.''' 11 template is the path to a .json policy template file.'''
12 12
13 from __future__ import with_statement 13 from __future__ import with_statement
14 from collections import namedtuple
14 from optparse import OptionParser 15 from optparse import OptionParser
15 import re 16 import re
16 import sys 17 import sys
17 import textwrap 18 import textwrap
18 19
19 20
20 CHROME_MANDATORY_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome' 21 CHROME_MANDATORY_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'
21 CHROME_RECOMMENDED_SUBKEY = CHROME_MANDATORY_SUBKEY + '\\\\Recommended' 22 CHROME_RECOMMENDED_SUBKEY = CHROME_MANDATORY_SUBKEY + '\\\\Recommended'
22 CHROMIUM_MANDATORY_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium' 23 CHROMIUM_MANDATORY_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium'
23 CHROMIUM_RECOMMENDED_SUBKEY = CHROMIUM_MANDATORY_SUBKEY + '\\\\Recommended' 24 CHROMIUM_RECOMMENDED_SUBKEY = CHROMIUM_MANDATORY_SUBKEY + '\\\\Recommended'
24 25
25 TYPE_MAP = { 26 TYPE_MAP = {
26 'dict': 'TYPE_DICTIONARY', 27 'dict': 'TYPE_DICTIONARY',
27 'int': 'TYPE_INTEGER', 28 'int': 'TYPE_INTEGER',
28 'int-enum': 'TYPE_INTEGER', 29 'int-enum': 'TYPE_INTEGER',
29 'list': 'TYPE_LIST', 30 'list': 'TYPE_LIST',
30 'main': 'TYPE_BOOLEAN', 31 'main': 'TYPE_BOOLEAN',
31 'string': 'TYPE_STRING', 32 'string': 'TYPE_STRING',
32 'string-enum': 'TYPE_STRING', 33 'string-enum': 'TYPE_STRING',
33 } 34 }
34 35
36 PolicyDetails = namedtuple(
37 'PolicyDetails',
38 'id name vtype platforms is_deprecated is_device_policy')
39
35 40
36 def main(): 41 def main():
37 parser = OptionParser(usage=__doc__) 42 parser = OptionParser(usage=__doc__)
38 parser.add_option('--pch', '--policy-constants-header', dest='header_path', 43 parser.add_option('--pch', '--policy-constants-header', dest='header_path',
39 help='generate header file of policy constants', 44 help='generate header file of policy constants',
40 metavar='FILE') 45 metavar='FILE')
41 parser.add_option('--pcc', '--policy-constants-source', dest='source_path', 46 parser.add_option('--pcc', '--policy-constants-source', dest='source_path',
42 help='generate source file of policy constants', 47 help='generate source file of policy constants',
43 metavar='FILE') 48 metavar='FILE')
44 parser.add_option('--ppb', '--policy-protobuf', dest='proto_path', 49 parser.add_option('--ppb', '--policy-protobuf', dest='proto_path',
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 result = '' 105 result = ''
101 pos = 0 106 pos = 0
102 for m in PH_PATTERN.finditer(text): 107 for m in PH_PATTERN.finditer(text):
103 result += text[pos:m.start(0)] 108 result += text[pos:m.start(0)]
104 result += m.group(2) or m.group(1) 109 result += m.group(2) or m.group(1)
105 pos = m.end(0) 110 pos = m.end(0)
106 result += text[pos:] 111 result += text[pos:]
107 return result; 112 return result;
108 113
109 114
110 # Returns a tuple with details about the given policy: 115 # Returns a PolicyDetails named tuple with details about the given policy.
111 # (name, type, list_of_platforms, is_deprecated, is_device_policy)
112 def _GetPolicyDetails(policy): 116 def _GetPolicyDetails(policy):
113 name = policy['name'] 117 name = policy['name']
114 if not TYPE_MAP.has_key(policy['type']): 118 if not TYPE_MAP.has_key(policy['type']):
115 print 'Unknown policy type for %s: %s' % (name, policy['type']) 119 print 'Unknown policy type for %s: %s' % (name, policy['type'])
116 sys.exit(3) 120 sys.exit(3)
117 vtype = TYPE_MAP[policy['type']] 121 vtype = TYPE_MAP[policy['type']]
118 # platforms is a list of 'chrome', 'chrome_os' and/or 'chrome_frame'. 122 # platforms is a list of 'chrome', 'chrome_os' and/or 'chrome_frame'.
119 platforms = [ x.split(':')[0] for x in policy['supported_on'] ] 123 platforms = [ x.split(':')[0] for x in policy['supported_on'] ]
120 is_deprecated = policy.get('deprecated', False) 124 is_deprecated = policy.get('deprecated', False)
121 is_device_policy = policy.get('device_only', False) 125 is_device_policy = policy.get('device_only', False)
122 return (name, vtype, platforms, is_deprecated, is_device_policy) 126 return PolicyDetails(name=name, vtype=vtype, platforms=platforms,
127 is_deprecated=is_deprecated, id=policy['id'],
128 is_device_policy=is_device_policy)
Joao da Silva 2012/09/18 11:49:34 Very cool
129
123 130
124 def _GetPolicyList(template_file_contents): 131 def _GetPolicyList(template_file_contents):
125 policies = [] 132 policies = []
126 for policy in template_file_contents['policy_definitions']: 133 for policy in template_file_contents['policy_definitions']:
127 if policy['type'] == 'group': 134 if policy['type'] == 'group':
128 for sub_policy in policy['policies']: 135 for sub_policy in policy['policies']:
129 policies.append(_GetPolicyDetails(sub_policy)) 136 policies.append(_GetPolicyDetails(sub_policy))
130 else: 137 else:
131 policies.append(_GetPolicyDetails(policy)) 138 policies.append(_GetPolicyDetails(policy))
132 # Tuples are sorted in lexicographical order, which will sort by policy name 139 policies.sort(key=lambda policy: policy.name)
qfel 2012/09/18 11:30:56 Could make policy namedtuple have name first, but
133 # in this case.
134 policies.sort()
135 return policies 140 return policies
136 141
137 142
138 def _GetPolicyNameList(template_file_contents):
139 return [name for (name, _, _, _, _) in _GetPolicyList(template_file_contents)]
140
141 def _GetChromePolicyList(template_file_contents):
142 return [(name, platforms, vtype, is_device_policy)
143 for (name, vtype, platforms, _, is_device_policy)
144 in _GetPolicyList(template_file_contents)]
145
146
147 def _GetDeprecatedPolicyList(template_file_contents):
148 return [name for (name, _, _, is_deprecated, _)
149 in _GetPolicyList(template_file_contents)
150 if is_deprecated]
151
152
153 def _LoadJSONFile(json_file): 143 def _LoadJSONFile(json_file):
154 with open(json_file, 'r') as f: 144 with open(json_file, 'r') as f:
155 text = f.read() 145 text = f.read()
156 return eval(text) 146 return eval(text)
157 147
158 148
159 #------------------ policy constants header ------------------------# 149 #------------------ policy constants header ------------------------#
160 def _WritePolicyConstantHeader(template_file_contents, args, opts): 150 def _WritePolicyConstantHeader(template_file_contents, args, opts):
161 os = args[0] 151 os = args[0]
162 with open(opts.header_path, 'w') as f: 152 with open(opts.header_path, 'w') as f:
(...skipping 16 matching lines...) Expand all
179 'configuration resides.\n' 169 'configuration resides.\n'
180 'extern const wchar_t kRegistryRecommendedSubKey[];\n\n') 170 'extern const wchar_t kRegistryRecommendedSubKey[];\n\n')
181 171
182 f.write('// Lists policy types mapped to their names and expected types.\n' 172 f.write('// Lists policy types mapped to their names and expected types.\n'
183 '// Used to initialize ConfigurationPolicyProviders.\n' 173 '// Used to initialize ConfigurationPolicyProviders.\n'
184 'struct PolicyDefinitionList {\n' 174 'struct PolicyDefinitionList {\n'
185 ' struct Entry {\n' 175 ' struct Entry {\n'
186 ' const char* name;\n' 176 ' const char* name;\n'
187 ' base::Value::Type value_type;\n' 177 ' base::Value::Type value_type;\n'
188 ' bool device_policy;\n' 178 ' bool device_policy;\n'
179 ' int id;\n'
189 ' };\n' 180 ' };\n'
190 '\n' 181 '\n'
191 ' const Entry* begin;\n' 182 ' const Entry* begin;\n'
192 ' const Entry* end;\n' 183 ' const Entry* end;\n'
193 '};\n' 184 '};\n'
194 '\n' 185 '\n'
195 '// Returns true if the given policy is deprecated.\n' 186 '// Returns true if the given policy is deprecated.\n'
196 'bool IsDeprecatedPolicy(const std::string& policy);\n' 187 'bool IsDeprecatedPolicy(const std::string& policy);\n'
197 '\n' 188 '\n'
198 '// Returns the default policy definition list for Chrome.\n' 189 '// Returns the default policy definition list for Chrome.\n'
199 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') 190 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n')
200 f.write('// Key names for the policy settings.\n' 191 f.write('// Key names for the policy settings.\n'
201 'namespace key {\n\n') 192 'namespace key {\n\n')
202 for policy_name in _GetPolicyNameList(template_file_contents): 193 for policy in _GetPolicyList(template_file_contents):
203 f.write('extern const char k' + policy_name + '[];\n') 194 f.write('extern const char k' + policy.name + '[];\n')
204 f.write('\n} // namespace key\n\n' 195 f.write('\n} // namespace key\n\n'
205 '} // namespace policy\n\n' 196 '} // namespace policy\n\n'
206 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') 197 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n')
207 198
208 199
209 #------------------ policy constants source ------------------------# 200 #------------------ policy constants source ------------------------#
210 def _WritePolicyConstantSource(template_file_contents, args, opts): 201 def _WritePolicyConstantSource(template_file_contents, args, opts):
211 os = args[0] 202 os = args[0]
212 is_chromium_os = args[1] == '1' 203 is_chromium_os = args[1] == '1'
213 platform = None 204 platform = None
214 platform_wildcard = None 205 platform_wildcard = None
215 if is_chromium_os: 206 if is_chromium_os:
216 platform = 'chrome_os' 207 platform = 'chrome_os'
217 else: 208 else:
218 platform = 'chrome.' + os.lower() 209 platform = 'chrome.' + os.lower()
219 platform_wildcard = 'chrome.*' 210 platform_wildcard = 'chrome.*'
220 with open(opts.source_path, 'w') as f: 211 with open(opts.source_path, 'w') as f:
221 _OutputGeneratedWarningForC(f, args[2]) 212 _OutputGeneratedWarningForC(f, args[2])
222 213
223 f.write('#include "base/basictypes.h"\n' 214 f.write('#include "base/basictypes.h"\n'
224 '#include "base/logging.h"\n' 215 '#include "base/logging.h"\n'
225 '#include "policy/policy_constants.h"\n' 216 '#include "policy/policy_constants.h"\n'
226 '\n' 217 '\n'
227 'namespace policy {\n\n') 218 'namespace policy {\n\n')
228 219
229 f.write('namespace {\n\n') 220 f.write('namespace {\n\n')
230 221
231 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') 222 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n')
232 policy_list = _GetChromePolicyList(template_file_contents) 223 for policy in _GetPolicyList(template_file_contents):
233 for (name, platforms, vtype, device_policy) in policy_list: 224 if (platform in policy.platforms) or \
234 if (platform in platforms) or (platform_wildcard in platforms): 225 (platform_wildcard in policy.platforms):
235 f.write(' { key::k%s, Value::%s, %s },\n' % 226 f.write(' { key::k%s, Value::%s, %s, %s },\n' %
236 (name, vtype, 'true' if device_policy else 'false')) 227 (policy.name, policy.vtype,
228 'true' if policy.is_device_policy else 'false', policy.id))
237 f.write('};\n\n') 229 f.write('};\n\n')
238 230
239 f.write('const PolicyDefinitionList kChromePolicyList = {\n' 231 f.write('const PolicyDefinitionList kChromePolicyList = {\n'
240 ' kEntries,\n' 232 ' kEntries,\n'
241 ' kEntries + arraysize(kEntries),\n' 233 ' kEntries + arraysize(kEntries),\n'
242 '};\n\n') 234 '};\n\n')
243 235
244 f.write('// List of deprecated policies.\n' 236 f.write('// List of deprecated policies.\n'
245 'const char* kDeprecatedPolicyList[] = {\n') 237 'const char* kDeprecatedPolicyList[] = {\n')
246 for name in _GetDeprecatedPolicyList(template_file_contents): 238 for policy in _GetPolicyList(template_file_contents):
247 f.write(' key::k%s,\n' % name) 239 if policy.is_deprecated:
240 f.write(' key::k%s,\n' % policy.name)
248 f.write('};\n\n') 241 f.write('};\n\n')
249 242
250 f.write('} // namespace\n\n') 243 f.write('} // namespace\n\n')
251 244
252 if os == 'win': 245 if os == 'win':
253 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' 246 f.write('#if defined(GOOGLE_CHROME_BUILD)\n'
254 'const wchar_t kRegistryMandatorySubKey[] = ' 247 'const wchar_t kRegistryMandatorySubKey[] = '
255 'L"' + CHROME_MANDATORY_SUBKEY + '";\n' 248 'L"' + CHROME_MANDATORY_SUBKEY + '";\n'
256 'const wchar_t kRegistryRecommendedSubKey[] = ' 249 'const wchar_t kRegistryRecommendedSubKey[] = '
257 'L"' + CHROME_RECOMMENDED_SUBKEY + '";\n' 250 'L"' + CHROME_RECOMMENDED_SUBKEY + '";\n'
(...skipping 11 matching lines...) Expand all
269 ' return true;\n' 262 ' return true;\n'
270 ' }\n' 263 ' }\n'
271 ' return false;\n' 264 ' return false;\n'
272 '}\n' 265 '}\n'
273 '\n' 266 '\n'
274 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' 267 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n'
275 ' return &kChromePolicyList;\n' 268 ' return &kChromePolicyList;\n'
276 '}\n\n') 269 '}\n\n')
277 270
278 f.write('namespace key {\n\n') 271 f.write('namespace key {\n\n')
279 for policy_name in _GetPolicyNameList(template_file_contents): 272 for policy in _GetPolicyList(template_file_contents):
280 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) 273 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name))
281 f.write('\n} // namespace key\n\n' 274 f.write('\n} // namespace key\n\n'
282 '} // namespace policy\n') 275 '} // namespace policy\n')
283 276
284 277
285 #------------------ policy protobuf --------------------------------# 278 #------------------ policy protobuf --------------------------------#
286 PROTO_HEAD = ''' 279 PROTO_HEAD = '''
287 syntax = "proto2"; 280 syntax = "proto2";
288 281
289 option optimize_for = LITE_RUNTIME; 282 option optimize_for = LITE_RUNTIME;
290 283
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 if policy['type'] == 'group': 466 if policy['type'] == 'group':
474 for sub_policy in policy['policies']: 467 for sub_policy in policy['policies']:
475 _WritePolicyCode(f, sub_policy) 468 _WritePolicyCode(f, sub_policy)
476 else: 469 else:
477 _WritePolicyCode(f, policy) 470 _WritePolicyCode(f, policy)
478 f.write(CPP_FOOT) 471 f.write(CPP_FOOT)
479 472
480 473
481 if __name__ == '__main__': 474 if __name__ == '__main__':
482 sys.exit(main()) 475 sys.exit(main())
OLDNEW
« chrome/chrome_browser.gypi ('K') | « chrome/common/pref_names.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698