OLD | NEW |
---|---|
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 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 template is the path to a .json policy template file.''' | 11 template is the path to a .json policy template file.''' |
11 | 12 |
12 from __future__ import with_statement | 13 from __future__ import with_statement |
13 from optparse import OptionParser | 14 from optparse import OptionParser |
14 import sys; | 15 import sys; |
15 | 16 |
16 | 17 |
17 CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'; | 18 CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'; |
18 CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium'; | 19 CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium'; |
19 | 20 |
(...skipping 20 matching lines...) Expand all Loading... | |
40 metavar="FILE"); | 41 metavar="FILE"); |
41 parser.add_option("--ppb", "--policy-protobuf", dest="proto_path", | 42 parser.add_option("--ppb", "--policy-protobuf", dest="proto_path", |
42 help="generate cloud policy protobuf file", | 43 help="generate cloud policy protobuf file", |
43 metavar="FILE"); | 44 metavar="FILE"); |
44 parser.add_option("--ppd", "--protobuf-decoder", dest="decoder_path", | 45 parser.add_option("--ppd", "--protobuf-decoder", dest="decoder_path", |
45 help="generate C++ code decoding the policy protobuf", | 46 help="generate C++ code decoding the policy protobuf", |
46 metavar="FILE"); | 47 metavar="FILE"); |
47 | 48 |
48 (opts, args) = parser.parse_args(); | 49 (opts, args) = parser.parse_args(); |
49 | 50 |
50 if len(args) < 2 or len(args) > 2: | 51 if len(args) != 3: |
51 print "exactly one platform and input file must be specified." | 52 print "exactly platform, chromium_os flag and input file must be specified." |
52 parser.print_help() | 53 parser.print_help() |
53 sys.exit(2) | 54 sys.exit(2) |
54 template_file_contents = _LoadJSONFile(args[1]); | 55 template_file_contents = _LoadJSONFile(args[2]); |
55 if opts.header_path is not None: | 56 if opts.header_path is not None: |
56 _WritePolicyConstantHeader(template_file_contents, args, opts); | 57 _WritePolicyConstantHeader(template_file_contents, args, opts); |
57 if opts.source_path is not None: | 58 if opts.source_path is not None: |
58 _WritePolicyConstantSource(template_file_contents, args, opts); | 59 _WritePolicyConstantSource(template_file_contents, args, opts); |
59 if opts.type_path is not None: | 60 if opts.type_path is not None: |
60 _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts); | 61 _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts); |
61 if opts.proto_path is not None: | 62 if opts.proto_path is not None: |
62 _WriteProtobuf(template_file_contents, args, opts.proto_path) | 63 _WriteProtobuf(template_file_contents, args, opts.proto_path) |
63 if opts.decoder_path is not None: | 64 if opts.decoder_path is not None: |
64 _WriteProtobufParser(template_file_contents, args, opts.decoder_path) | 65 _WriteProtobufParser(template_file_contents, args, opts.decoder_path) |
65 | 66 |
66 | 67 |
67 #------------------ shared helpers ---------------------------------# | 68 #------------------ shared helpers ---------------------------------# |
68 def _OutputGeneratedWarningForC(f, template_file_path): | 69 def _OutputGeneratedWarningForC(f, template_file_path): |
69 f.write('//\n' | 70 f.write('//\n' |
70 '// DO NOT MODIFY THIS FILE DIRECTLY!\n' | 71 '// DO NOT MODIFY THIS FILE DIRECTLY!\n' |
71 '// IT IS GENERATED BY generate_policy_source.py\n' | 72 '// IT IS GENERATED BY generate_policy_source.py\n' |
72 '// FROM ' + template_file_path + '\n' | 73 '// FROM ' + template_file_path + '\n' |
73 '//\n\n') | 74 '//\n\n') |
74 | 75 |
75 | 76 |
76 # Returns a tuple with details about the given policy: | 77 # Returns a tuple with details about the given policy: |
77 # (name, type, list_of_platforms, is_device_policy) | 78 # (name, type, list_of_platforms, is_device_policy) |
78 def _GetPolicyDetails(policy): | 79 def _GetPolicyDetails(policy): |
79 if not TYPE_MAP.has_key(policy['type']): | 80 if not TYPE_MAP.has_key(policy['type']): |
80 print "Unknown policy type for %s: %s" % (policy['name'], policy['type']) | 81 print "Unknown policy type for %s: %s" % (policy['name'], policy['type']) |
81 sys.exit(3) | 82 sys.exit(3) |
82 # platforms is a list of "chrome", "chrome_os" and/or "chrome_frame". | 83 # 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 platforms = [ x.split(':')[0] for x in policy['supported_on'] ] |
84 is_device_policy = policy.get('device_only', False) | 85 is_device_policy = policy.get('device_only', False) |
85 return (policy['name'], TYPE_MAP[policy['type']], platforms, is_device_policy) | 86 return (policy['name'], TYPE_MAP[policy['type']], platforms, is_device_policy) |
86 | 87 |
87 | 88 |
88 def _GetPolicyList(template_file_contents): | 89 def _GetPolicyList(template_file_contents): |
89 policies = []; | 90 policies = []; |
90 for policy in template_file_contents['policy_definitions']: | 91 for policy in template_file_contents['policy_definitions']: |
91 if policy['type'] == 'group': | 92 if policy['type'] == 'group': |
92 for sub_policy in policy['policies']: | 93 for sub_policy in policy['policies']: |
93 policies.append(_GetPolicyDetails(sub_policy)) | 94 policies.append(_GetPolicyDetails(sub_policy)) |
94 else: | 95 else: |
95 policies.append(_GetPolicyDetails(policy)) | 96 policies.append(_GetPolicyDetails(policy)) |
96 # Tuples are sorted in lexicographical order, which will sort by policy name | 97 # Tuples are sorted in lexicographical order, which will sort by policy name |
97 # in this case. | 98 # in this case. |
98 policies.sort() | 99 policies.sort() |
99 return policies | 100 return policies |
100 | 101 |
101 | 102 |
102 def _GetPolicyNameList(template_file_contents): | 103 def _GetPolicyNameList(template_file_contents): |
103 return [name for (name, _, _, _) in _GetPolicyList(template_file_contents)] | 104 return [name for (name, _, _, _) in _GetPolicyList(template_file_contents)] |
104 | 105 |
105 | 106 |
106 def _GetChromePolicyList(template_file_contents): | 107 def _GetChromePolicyList(template_file_contents): |
107 return [(name, vtype) for (name, vtype, platforms, is_device_only) | 108 return [(name, platforms, vtype) for (name, vtype, platforms, is_device_only) |
Joao da Silva
2011/11/04 17:44:24
Since |is_device_only| is never used, it could be
Mattias Nissler (ping if slow)
2011/11/07 10:48:45
Done.
| |
108 in _GetPolicyList(template_file_contents) | 109 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] | |
118 | 110 |
119 | 111 |
120 def _LoadJSONFile(json_file): | 112 def _LoadJSONFile(json_file): |
121 with open(json_file, "r") as f: | 113 with open(json_file, "r") as f: |
122 text = f.read() | 114 text = f.read() |
123 return eval(text) | 115 return eval(text) |
124 | 116 |
125 | 117 |
126 #------------------ policy constants header ------------------------# | 118 #------------------ policy constants header ------------------------# |
127 def _WritePolicyConstantHeader(template_file_contents, args, opts): | 119 def _WritePolicyConstantHeader(template_file_contents, args, opts): |
128 platform = args[0]; | 120 os = args[0]; |
129 with open(opts.header_path, "w") as f: | 121 with open(opts.header_path, "w") as f: |
130 _OutputGeneratedWarningForC(f, args[1]) | 122 _OutputGeneratedWarningForC(f, args[2]) |
131 | 123 |
132 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 124 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
133 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 125 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
134 '#pragma once\n' | 126 '#pragma once\n' |
135 '\n' | 127 '\n' |
136 '#include "base/values.h"\n' | 128 '#include "base/values.h"\n' |
137 '#include "policy/configuration_policy_type.h"\n' | 129 '#include "policy/configuration_policy_type.h"\n' |
138 '\n' | 130 '\n' |
139 'namespace policy {\n\n') | 131 'namespace policy {\n\n') |
140 | 132 |
141 if platform == "win": | 133 if os == "win": |
142 f.write('// The windows registry path where policy configuration ' | 134 f.write('// The windows registry path where policy configuration ' |
143 'resides.\n' | 135 'resides.\n' |
144 'extern const wchar_t kRegistrySubKey[];\n\n') | 136 'extern const wchar_t kRegistrySubKey[];\n\n') |
145 | 137 |
146 f.write('// Lists policy types mapped to their names and expected types.\n' | 138 f.write('// Lists policy types mapped to their names and expected types.\n' |
147 '// Used to initialize ConfigurationPolicyProviders.\n' | 139 '// Used to initialize ConfigurationPolicyProviders.\n' |
148 'struct PolicyDefinitionList {\n' | 140 'struct PolicyDefinitionList {\n' |
149 ' struct Entry {\n' | 141 ' struct Entry {\n' |
150 ' ConfigurationPolicyType policy_type;\n' | 142 ' ConfigurationPolicyType policy_type;\n' |
151 ' base::Value::Type value_type;\n' | 143 ' base::Value::Type value_type;\n' |
(...skipping 13 matching lines...) Expand all Loading... | |
165 'namespace key {\n\n') | 157 'namespace key {\n\n') |
166 for policy_name in _GetPolicyNameList(template_file_contents): | 158 for policy_name in _GetPolicyNameList(template_file_contents): |
167 f.write('extern const char k' + policy_name + '[];\n') | 159 f.write('extern const char k' + policy_name + '[];\n') |
168 f.write('\n} // namespace key\n\n' | 160 f.write('\n} // namespace key\n\n' |
169 '} // namespace policy\n\n' | 161 '} // namespace policy\n\n' |
170 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 162 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
171 | 163 |
172 | 164 |
173 #------------------ policy constants source ------------------------# | 165 #------------------ policy constants source ------------------------# |
174 def _WritePolicyConstantSource(template_file_contents, args, opts): | 166 def _WritePolicyConstantSource(template_file_contents, args, opts): |
175 platform = args[0]; | 167 os = args[0]; |
168 is_chromium_os = args[1] == "1" | |
169 platform = None | |
170 platform_wildcard = None | |
171 if is_chromium_os: | |
172 platform = 'chrome_os' | |
173 else: | |
174 platform = 'chrome.' + os.lower() | |
175 platform_wildcard = 'chrome.*' | |
176 with open(opts.source_path, "w") as f: | 176 with open(opts.source_path, "w") as f: |
177 _OutputGeneratedWarningForC(f, args[1]) | 177 _OutputGeneratedWarningForC(f, args[2]) |
178 | 178 |
179 f.write('#include "base/basictypes.h"\n' | 179 f.write('#include "base/basictypes.h"\n' |
180 '#include "base/logging.h"\n' | 180 '#include "base/logging.h"\n' |
181 '#include "policy/policy_constants.h"\n' | 181 '#include "policy/policy_constants.h"\n' |
182 '\n' | 182 '\n' |
183 'namespace policy {\n\n') | 183 'namespace policy {\n\n') |
184 | 184 |
185 f.write('namespace {\n\n') | 185 f.write('namespace {\n\n') |
186 | 186 |
187 f.write('PolicyDefinitionList::Entry entries[] = {\n') | 187 f.write('PolicyDefinitionList::Entry entries[] = {\n') |
188 for (name, vtype) in _GetChromePolicyList(template_file_contents): | 188 policy_list = _GetChromePolicyList(template_file_contents) |
189 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) | 189 for (name, platforms, vtype) in policy_list: |
190 f.write('\n#if defined(OS_CHROMEOS)\n') | 190 if (platform in platforms) or (platform_wildcard in platforms): |
191 for (name, vtype) in _GetChromeOSPolicyList(template_file_contents): | 191 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) |
192 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) | 192 f.write('};\n\n') |
193 f.write('#endif\n' | |
194 '};\n\n') | |
195 | 193 |
196 f.write('PolicyDefinitionList chrome_policy_list = {\n' | 194 f.write('PolicyDefinitionList chrome_policy_list = {\n' |
197 ' entries,\n' | 195 ' entries,\n' |
198 ' entries + arraysize(entries),\n' | 196 ' entries + arraysize(entries),\n' |
199 '};\n\n') | 197 '};\n\n') |
200 | 198 |
201 f.write('// Maps a policy-type enum value to the policy name.\n' | 199 f.write('// Maps a policy-type enum value to the policy name.\n' |
202 'const char* policy_name_map[] = {\n'); | 200 'const char* policy_name_map[] = {\n'); |
203 for name in _GetPolicyNameList(template_file_contents): | 201 for name in _GetPolicyNameList(template_file_contents): |
204 f.write(' key::k%s,\n' % name) | 202 f.write(' key::k%s,\n' % name) |
205 f.write('};\n\n') | 203 f.write('};\n\n') |
206 | 204 |
207 f.write('} // namespace\n\n') | 205 f.write('} // namespace\n\n') |
208 | 206 |
209 if platform == "win": | 207 if os == "win": |
210 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' | 208 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
211 'const wchar_t kRegistrySubKey[] = ' | 209 'const wchar_t kRegistrySubKey[] = ' |
212 'L"' + CHROME_SUBKEY + '";\n' | 210 'L"' + CHROME_SUBKEY + '";\n' |
213 '#else\n' | 211 '#else\n' |
214 'const wchar_t kRegistrySubKey[] = ' | 212 'const wchar_t kRegistrySubKey[] = ' |
215 'L"' + CHROMIUM_SUBKEY + '";\n' | 213 'L"' + CHROMIUM_SUBKEY + '";\n' |
216 '#endif\n\n') | 214 '#endif\n\n') |
217 | 215 |
218 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n' | 216 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n' |
219 ' CHECK(type >= 0 && ' | 217 ' CHECK(type >= 0 && ' |
220 'static_cast<size_t>(type) < arraysize(policy_name_map));\n' | 218 'static_cast<size_t>(type) < arraysize(policy_name_map));\n' |
221 ' return policy_name_map[type];\n' | 219 ' return policy_name_map[type];\n' |
222 '}\n' | 220 '}\n' |
223 '\n' | 221 '\n' |
224 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' | 222 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' |
225 ' return &chrome_policy_list;\n' | 223 ' return &chrome_policy_list;\n' |
226 '}\n\n') | 224 '}\n\n') |
227 | 225 |
228 f.write('namespace key {\n\n') | 226 f.write('namespace key {\n\n') |
229 for policy_name in _GetPolicyNameList(template_file_contents): | 227 for policy_name in _GetPolicyNameList(template_file_contents): |
230 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) | 228 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) |
231 f.write('\n} // namespace key\n\n' | 229 f.write('\n} // namespace key\n\n' |
232 '} // namespace policy\n') | 230 '} // namespace policy\n') |
233 | 231 |
234 | 232 |
235 #------------------ policy type enumeration header -----------------# | 233 #------------------ policy type enumeration header -----------------# |
236 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): | 234 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): |
237 with open(opts.type_path, "w") as f: | 235 with open(opts.type_path, "w") as f: |
238 _OutputGeneratedWarningForC(f, args[1]) | 236 _OutputGeneratedWarningForC(f, args[1]) |
Joao da Silva
2011/11/04 17:44:24
args[2]?
Mattias Nissler (ping if slow)
2011/11/07 10:48:45
Done.
| |
239 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' | 237 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
240 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' | 238 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
241 '#pragma once\n' | 239 '#pragma once\n' |
242 '\n' | 240 '\n' |
243 'namespace policy {\n' | 241 'namespace policy {\n' |
244 '\n' | 242 '\n' |
245 'enum ConfigurationPolicyType {\n') | 243 'enum ConfigurationPolicyType {\n') |
246 for policy_name in _GetPolicyNameList(template_file_contents): | 244 for policy_name in _GetPolicyNameList(template_file_contents): |
247 f.write(' kPolicy' + policy_name + ",\n"); | 245 f.write(' kPolicy' + policy_name + ",\n"); |
248 f.write('};\n\n' | 246 f.write('};\n\n' |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 file.write(' optional PolicyOptions policy_options = 1;\n') | 296 file.write(' optional PolicyOptions policy_options = 1;\n') |
299 file.write(' optional %s %s = 2;\n' % | 297 file.write(' optional %s %s = 2;\n' % |
300 (PROTOBUF_TYPE[policy['type']], policy['name'])) | 298 (PROTOBUF_TYPE[policy['type']], policy['name'])) |
301 file.write('}\n\n') | 299 file.write('}\n\n') |
302 fields += [' optional %sProto %s = %s;\n' % | 300 fields += [' optional %sProto %s = %s;\n' % |
303 (policy['name'], policy['name'], policy['id'] + RESERVED_IDS)] | 301 (policy['name'], policy['name'], policy['id'] + RESERVED_IDS)] |
304 | 302 |
305 | 303 |
306 def _WriteProtobuf(template_file_contents, args, outfilepath): | 304 def _WriteProtobuf(template_file_contents, args, outfilepath): |
307 with open(outfilepath, 'w') as f: | 305 with open(outfilepath, 'w') as f: |
308 _OutputGeneratedWarningForC(f, args[1]) | 306 _OutputGeneratedWarningForC(f, args[1]) |
Joao da Silva
2011/11/04 17:44:24
args[2]?
Mattias Nissler (ping if slow)
2011/11/07 10:48:45
Done.
| |
309 f.write(PROTO_HEAD) | 307 f.write(PROTO_HEAD) |
310 | 308 |
311 fields = [] | 309 fields = [] |
312 f.write('// PBs for individual settings.\n\n') | 310 f.write('// PBs for individual settings.\n\n') |
313 for policy in template_file_contents['policy_definitions']: | 311 for policy in template_file_contents['policy_definitions']: |
314 if policy['type'] == 'group': | 312 if policy['type'] == 'group': |
315 for sub_policy in policy['policies']: | 313 for sub_policy in policy['policies']: |
316 _WritePolicyProto(f, sub_policy, fields) | 314 _WritePolicyProto(f, sub_policy, fields) |
317 else: | 315 else: |
318 _WritePolicyProto(f, policy, fields) | 316 _WritePolicyProto(f, policy, fields) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 file.write(' Value* value = %s(%s.%s());\n' % | 418 file.write(' Value* value = %s(%s.%s());\n' % |
421 (_CreateValue(policy['type']), proto_name, membername)) | 419 (_CreateValue(policy['type']), proto_name, membername)) |
422 file.write(' destination->Set(kPolicy%s, value);\n' % policy['name']) | 420 file.write(' destination->Set(kPolicy%s, value);\n' % policy['name']) |
423 file.write(' }\n' | 421 file.write(' }\n' |
424 ' }\n' | 422 ' }\n' |
425 ' }\n') | 423 ' }\n') |
426 | 424 |
427 | 425 |
428 def _WriteProtobufParser(template_file_contents, args, outfilepath): | 426 def _WriteProtobufParser(template_file_contents, args, outfilepath): |
429 with open(outfilepath, 'w') as f: | 427 with open(outfilepath, 'w') as f: |
430 _OutputGeneratedWarningForC(f, args[1]) | 428 _OutputGeneratedWarningForC(f, args[1]) |
Joao da Silva
2011/11/04 17:44:24
args[2]?
Mattias Nissler (ping if slow)
2011/11/07 10:48:45
Done.
| |
431 f.write(CPP_HEAD) | 429 f.write(CPP_HEAD) |
432 for policy in template_file_contents['policy_definitions']: | 430 for policy in template_file_contents['policy_definitions']: |
433 if policy['type'] == 'group': | 431 if policy['type'] == 'group': |
434 for sub_policy in policy['policies']: | 432 for sub_policy in policy['policies']: |
435 _WritePolicyCode(f, sub_policy) | 433 _WritePolicyCode(f, sub_policy) |
436 else: | 434 else: |
437 _WritePolicyCode(f, policy) | 435 _WritePolicyCode(f, policy) |
438 f.write(CPP_FOOT) | 436 f.write(CPP_FOOT) |
439 | 437 |
440 | 438 |
441 #------------------ main() -----------------------------------------# | 439 #------------------ main() -----------------------------------------# |
442 if __name__ == '__main__': | 440 if __name__ == '__main__': |
443 main(); | 441 main(); |
OLD | NEW |