| 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) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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_deprecated, is_device_policy) | 78 # (name, type, list_of_platforms, is_deprecated, is_device_policy) |
| 78 def _GetPolicyDetails(policy): | 79 def _GetPolicyDetails(policy): |
| 79 name = policy['name'] | 80 name = policy['name'] |
| 80 if not TYPE_MAP.has_key(policy['type']): | 81 if not TYPE_MAP.has_key(policy['type']): |
| 81 print "Unknown policy type for %s: %s" % (name, policy['type']) | 82 print "Unknown policy type for %s: %s" % (name, policy['type']) |
| 82 sys.exit(3) | 83 sys.exit(3) |
| 83 vtype = TYPE_MAP[policy['type']] | 84 vtype = TYPE_MAP[policy['type']] |
| 84 # platforms is a list of "chrome", "chrome_os" and/or "chrome_frame". | 85 # platforms is a list of "chrome", "chrome_os" and/or "chrome_frame". |
| 85 platforms = [ x.split('.')[0].split(':')[0] for x in policy['supported_on'] ] | 86 platforms = [ x.split(':')[0] for x in policy['supported_on'] ] |
| 86 is_deprecated = policy.get('deprecated', False) | 87 is_deprecated = policy.get('deprecated', False) |
| 87 is_device_policy = policy.get('device_only', False) | 88 return (name, vtype, platforms, is_deprecated) |
| 88 return (name, vtype, platforms, is_deprecated, is_device_policy) | |
| 89 | 89 |
| 90 | 90 |
| 91 def _GetPolicyList(template_file_contents): | 91 def _GetPolicyList(template_file_contents): |
| 92 policies = []; | 92 policies = []; |
| 93 for policy in template_file_contents['policy_definitions']: | 93 for policy in template_file_contents['policy_definitions']: |
| 94 if policy['type'] == 'group': | 94 if policy['type'] == 'group': |
| 95 for sub_policy in policy['policies']: | 95 for sub_policy in policy['policies']: |
| 96 policies.append(_GetPolicyDetails(sub_policy)) | 96 policies.append(_GetPolicyDetails(sub_policy)) |
| 97 else: | 97 else: |
| 98 policies.append(_GetPolicyDetails(policy)) | 98 policies.append(_GetPolicyDetails(policy)) |
| 99 # Tuples are sorted in lexicographical order, which will sort by policy name | 99 # Tuples are sorted in lexicographical order, which will sort by policy name |
| 100 # in this case. | 100 # in this case. |
| 101 policies.sort() | 101 policies.sort() |
| 102 return policies | 102 return policies |
| 103 | 103 |
| 104 | 104 |
| 105 def _GetPolicyNameList(template_file_contents): | 105 def _GetPolicyNameList(template_file_contents): |
| 106 return [name for (name, _, _, _, _) in _GetPolicyList(template_file_contents)] | 106 return [name for (name, _, _, _) in _GetPolicyList(template_file_contents)] |
| 107 | 107 |
| 108 | 108 |
| 109 def _GetChromePolicyList(template_file_contents): | 109 def _GetChromePolicyList(template_file_contents): |
| 110 return [(name, vtype) for (name, vtype, platforms, _, is_device_only) | 110 return [(name, platforms, vtype) for (name, vtype, platforms, _) |
| 111 in _GetPolicyList(template_file_contents) | 111 in _GetPolicyList(template_file_contents)] |
| 112 if 'chrome' in platforms and not is_device_only] | |
| 113 | |
| 114 | |
| 115 def _GetChromeOSPolicyList(template_file_contents): | |
| 116 return [(name, vtype) for (name, vtype, platforms, _, is_device_only) | |
| 117 in _GetPolicyList(template_file_contents) | |
| 118 if 'chrome_os' in platforms | |
| 119 and not 'chrome' in platforms | |
| 120 and not is_device_only] | |
| 121 | 112 |
| 122 | 113 |
| 123 def _GetDeprecatedPolicyList(template_file_contents): | 114 def _GetDeprecatedPolicyList(template_file_contents): |
| 124 return [name for (name, _, _, is_deprecated, _) | 115 return [name for (name, _, _, is_deprecated) |
| 125 in _GetPolicyList(template_file_contents) | 116 in _GetPolicyList(template_file_contents) |
| 126 if is_deprecated] | 117 if is_deprecated] |
| 127 | 118 |
| 128 | 119 |
| 129 def _LoadJSONFile(json_file): | 120 def _LoadJSONFile(json_file): |
| 130 with open(json_file, "r") as f: | 121 with open(json_file, "r") as f: |
| 131 text = f.read() | 122 text = f.read() |
| 132 return eval(text) | 123 return eval(text) |
| 133 | 124 |
| 134 | 125 |
| 135 #------------------ policy constants header ------------------------# | 126 #------------------ policy constants header ------------------------# |
| 136 def _WritePolicyConstantHeader(template_file_contents, args, opts): | 127 def _WritePolicyConstantHeader(template_file_contents, args, opts): |
| 137 platform = args[0]; | 128 os = args[0]; |
| 138 with open(opts.header_path, "w") as f: | 129 with open(opts.header_path, "w") as f: |
| 139 _OutputGeneratedWarningForC(f, args[1]) | 130 _OutputGeneratedWarningForC(f, args[2]) |
| 140 | 131 |
| 141 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 132 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 142 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' | 133 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 143 '#pragma once\n' | 134 '#pragma once\n' |
| 144 '\n' | 135 '\n' |
| 145 '#include "base/values.h"\n' | 136 '#include "base/values.h"\n' |
| 146 '#include "policy/configuration_policy_type.h"\n' | 137 '#include "policy/configuration_policy_type.h"\n' |
| 147 '\n' | 138 '\n' |
| 148 'namespace policy {\n\n') | 139 'namespace policy {\n\n') |
| 149 | 140 |
| 150 if platform == "win": | 141 if os == "win": |
| 151 f.write('// The windows registry path where policy configuration ' | 142 f.write('// The windows registry path where policy configuration ' |
| 152 'resides.\n' | 143 'resides.\n' |
| 153 'extern const wchar_t kRegistrySubKey[];\n\n') | 144 'extern const wchar_t kRegistrySubKey[];\n\n') |
| 154 | 145 |
| 155 f.write('// Lists policy types mapped to their names and expected types.\n' | 146 f.write('// Lists policy types mapped to their names and expected types.\n' |
| 156 '// Used to initialize ConfigurationPolicyProviders.\n' | 147 '// Used to initialize ConfigurationPolicyProviders.\n' |
| 157 'struct PolicyDefinitionList {\n' | 148 'struct PolicyDefinitionList {\n' |
| 158 ' struct Entry {\n' | 149 ' struct Entry {\n' |
| 159 ' ConfigurationPolicyType policy_type;\n' | 150 ' ConfigurationPolicyType policy_type;\n' |
| 160 ' base::Value::Type value_type;\n' | 151 ' base::Value::Type value_type;\n' |
| (...skipping 16 matching lines...) Expand all Loading... |
| 177 'namespace key {\n\n') | 168 'namespace key {\n\n') |
| 178 for policy_name in _GetPolicyNameList(template_file_contents): | 169 for policy_name in _GetPolicyNameList(template_file_contents): |
| 179 f.write('extern const char k' + policy_name + '[];\n') | 170 f.write('extern const char k' + policy_name + '[];\n') |
| 180 f.write('\n} // namespace key\n\n' | 171 f.write('\n} // namespace key\n\n' |
| 181 '} // namespace policy\n\n' | 172 '} // namespace policy\n\n' |
| 182 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 173 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| 183 | 174 |
| 184 | 175 |
| 185 #------------------ policy constants source ------------------------# | 176 #------------------ policy constants source ------------------------# |
| 186 def _WritePolicyConstantSource(template_file_contents, args, opts): | 177 def _WritePolicyConstantSource(template_file_contents, args, opts): |
| 187 platform = args[0]; | 178 os = args[0]; |
| 179 is_chromium_os = args[1] == "1" |
| 180 platform = None |
| 181 platform_wildcard = None |
| 182 if is_chromium_os: |
| 183 platform = 'chrome_os' |
| 184 else: |
| 185 platform = 'chrome.' + os.lower() |
| 186 platform_wildcard = 'chrome.*' |
| 188 with open(opts.source_path, "w") as f: | 187 with open(opts.source_path, "w") as f: |
| 189 _OutputGeneratedWarningForC(f, args[1]) | 188 _OutputGeneratedWarningForC(f, args[2]) |
| 190 | 189 |
| 191 f.write('#include "base/basictypes.h"\n' | 190 f.write('#include "base/basictypes.h"\n' |
| 192 '#include "base/logging.h"\n' | 191 '#include "base/logging.h"\n' |
| 193 '#include "policy/policy_constants.h"\n' | 192 '#include "policy/policy_constants.h"\n' |
| 194 '\n' | 193 '\n' |
| 195 'namespace policy {\n\n') | 194 'namespace policy {\n\n') |
| 196 | 195 |
| 197 f.write('namespace {\n\n') | 196 f.write('namespace {\n\n') |
| 198 | 197 |
| 199 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') | 198 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') |
| 200 for (name, vtype) in _GetChromePolicyList(template_file_contents): | 199 policy_list = _GetChromePolicyList(template_file_contents) |
| 201 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) | 200 for (name, platforms, vtype) in policy_list: |
| 202 f.write('\n#if defined(OS_CHROMEOS)\n') | 201 if (platform in platforms) or (platform_wildcard in platforms): |
| 203 for (name, vtype) in _GetChromeOSPolicyList(template_file_contents): | 202 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) |
| 204 f.write(' { kPolicy%s, Value::%s, key::k%s },\n' % (name, vtype, name)) | 203 f.write('};\n\n') |
| 205 f.write('#endif\n' | |
| 206 '};\n\n') | |
| 207 | 204 |
| 208 f.write('const PolicyDefinitionList kChromePolicyList = {\n' | 205 f.write('const PolicyDefinitionList kChromePolicyList = {\n' |
| 209 ' kEntries,\n' | 206 ' kEntries,\n' |
| 210 ' kEntries + arraysize(kEntries),\n' | 207 ' kEntries + arraysize(kEntries),\n' |
| 211 '};\n\n') | 208 '};\n\n') |
| 212 | 209 |
| 213 f.write('// Maps a policy-type enum value to the policy name.\n' | 210 f.write('// Maps a policy-type enum value to the policy name.\n' |
| 214 'const char* kPolicyNameMap[] = {\n'); | 211 'const char* kPolicyNameMap[] = {\n'); |
| 215 for name in _GetPolicyNameList(template_file_contents): | 212 for name in _GetPolicyNameList(template_file_contents): |
| 216 f.write(' key::k%s,\n' % name) | 213 f.write(' key::k%s,\n' % name) |
| 217 f.write('};\n\n') | 214 f.write('};\n\n') |
| 218 | 215 |
| 219 f.write('// List of deprecated policies.\n' | 216 f.write('// List of deprecated policies.\n' |
| 220 'const ConfigurationPolicyType kDeprecatedPolicyList[] = {\n') | 217 'const ConfigurationPolicyType kDeprecatedPolicyList[] = {\n') |
| 221 for name in _GetDeprecatedPolicyList(template_file_contents): | 218 for name in _GetDeprecatedPolicyList(template_file_contents): |
| 222 f.write(' kPolicy%s,\n' % name) | 219 f.write(' kPolicy%s,\n' % name) |
| 223 f.write('};\n\n') | 220 f.write('};\n\n') |
| 224 | 221 |
| 225 f.write('} // namespace\n\n') | 222 f.write('} // namespace\n\n') |
| 226 | 223 |
| 227 if platform == "win": | 224 if os == "win": |
| 228 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' | 225 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
| 229 'const wchar_t kRegistrySubKey[] = ' | 226 'const wchar_t kRegistrySubKey[] = ' |
| 230 'L"' + CHROME_SUBKEY + '";\n' | 227 'L"' + CHROME_SUBKEY + '";\n' |
| 231 '#else\n' | 228 '#else\n' |
| 232 'const wchar_t kRegistrySubKey[] = ' | 229 'const wchar_t kRegistrySubKey[] = ' |
| 233 'L"' + CHROMIUM_SUBKEY + '";\n' | 230 'L"' + CHROMIUM_SUBKEY + '";\n' |
| 234 '#endif\n\n') | 231 '#endif\n\n') |
| 235 | 232 |
| 236 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n' | 233 f.write('const char* GetPolicyName(ConfigurationPolicyType type) {\n' |
| 237 ' CHECK(type >= 0 && ' | 234 ' CHECK(type >= 0 && ' |
| (...skipping 17 matching lines...) Expand all Loading... |
| 255 f.write('namespace key {\n\n') | 252 f.write('namespace key {\n\n') |
| 256 for policy_name in _GetPolicyNameList(template_file_contents): | 253 for policy_name in _GetPolicyNameList(template_file_contents): |
| 257 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) | 254 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) |
| 258 f.write('\n} // namespace key\n\n' | 255 f.write('\n} // namespace key\n\n' |
| 259 '} // namespace policy\n') | 256 '} // namespace policy\n') |
| 260 | 257 |
| 261 | 258 |
| 262 #------------------ policy type enumeration header -----------------# | 259 #------------------ policy type enumeration header -----------------# |
| 263 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): | 260 def _WritePolicyTypeEnumerationHeader(template_file_contents, args, opts): |
| 264 with open(opts.type_path, "w") as f: | 261 with open(opts.type_path, "w") as f: |
| 265 _OutputGeneratedWarningForC(f, args[1]) | 262 _OutputGeneratedWarningForC(f, args[2]) |
| 266 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' | 263 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
| 267 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' | 264 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
| 268 '#pragma once\n' | 265 '#pragma once\n' |
| 269 '\n' | 266 '\n' |
| 270 'namespace policy {\n' | 267 'namespace policy {\n' |
| 271 '\n' | 268 '\n' |
| 272 'enum ConfigurationPolicyType {\n') | 269 'enum ConfigurationPolicyType {\n') |
| 273 for policy_name in _GetPolicyNameList(template_file_contents): | 270 for policy_name in _GetPolicyNameList(template_file_contents): |
| 274 f.write(' kPolicy' + policy_name + ",\n"); | 271 f.write(' kPolicy' + policy_name + ",\n"); |
| 275 f.write('};\n\n' | 272 f.write('};\n\n' |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 file.write(' optional PolicyOptions policy_options = 1;\n') | 322 file.write(' optional PolicyOptions policy_options = 1;\n') |
| 326 file.write(' optional %s %s = 2;\n' % | 323 file.write(' optional %s %s = 2;\n' % |
| 327 (PROTOBUF_TYPE[policy['type']], policy['name'])) | 324 (PROTOBUF_TYPE[policy['type']], policy['name'])) |
| 328 file.write('}\n\n') | 325 file.write('}\n\n') |
| 329 fields += [' optional %sProto %s = %s;\n' % | 326 fields += [' optional %sProto %s = %s;\n' % |
| 330 (policy['name'], policy['name'], policy['id'] + RESERVED_IDS)] | 327 (policy['name'], policy['name'], policy['id'] + RESERVED_IDS)] |
| 331 | 328 |
| 332 | 329 |
| 333 def _WriteProtobuf(template_file_contents, args, outfilepath): | 330 def _WriteProtobuf(template_file_contents, args, outfilepath): |
| 334 with open(outfilepath, 'w') as f: | 331 with open(outfilepath, 'w') as f: |
| 335 _OutputGeneratedWarningForC(f, args[1]) | 332 _OutputGeneratedWarningForC(f, args[2]) |
| 336 f.write(PROTO_HEAD) | 333 f.write(PROTO_HEAD) |
| 337 | 334 |
| 338 fields = [] | 335 fields = [] |
| 339 f.write('// PBs for individual settings.\n\n') | 336 f.write('// PBs for individual settings.\n\n') |
| 340 for policy in template_file_contents['policy_definitions']: | 337 for policy in template_file_contents['policy_definitions']: |
| 341 if policy['type'] == 'group': | 338 if policy['type'] == 'group': |
| 342 for sub_policy in policy['policies']: | 339 for sub_policy in policy['policies']: |
| 343 _WritePolicyProto(f, sub_policy, fields) | 340 _WritePolicyProto(f, sub_policy, fields) |
| 344 else: | 341 else: |
| 345 _WritePolicyProto(f, policy, fields) | 342 _WritePolicyProto(f, policy, fields) |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 file.write(' Value* value = %s(%s.%s());\n' % | 444 file.write(' Value* value = %s(%s.%s());\n' % |
| 448 (_CreateValue(policy['type']), proto_name, membername)) | 445 (_CreateValue(policy['type']), proto_name, membername)) |
| 449 file.write(' destination->Set(kPolicy%s, value);\n' % policy['name']) | 446 file.write(' destination->Set(kPolicy%s, value);\n' % policy['name']) |
| 450 file.write(' }\n' | 447 file.write(' }\n' |
| 451 ' }\n' | 448 ' }\n' |
| 452 ' }\n') | 449 ' }\n') |
| 453 | 450 |
| 454 | 451 |
| 455 def _WriteProtobufParser(template_file_contents, args, outfilepath): | 452 def _WriteProtobufParser(template_file_contents, args, outfilepath): |
| 456 with open(outfilepath, 'w') as f: | 453 with open(outfilepath, 'w') as f: |
| 457 _OutputGeneratedWarningForC(f, args[1]) | 454 _OutputGeneratedWarningForC(f, args[2]) |
| 458 f.write(CPP_HEAD) | 455 f.write(CPP_HEAD) |
| 459 for policy in template_file_contents['policy_definitions']: | 456 for policy in template_file_contents['policy_definitions']: |
| 460 if policy['type'] == 'group': | 457 if policy['type'] == 'group': |
| 461 for sub_policy in policy['policies']: | 458 for sub_policy in policy['policies']: |
| 462 _WritePolicyCode(f, sub_policy) | 459 _WritePolicyCode(f, sub_policy) |
| 463 else: | 460 else: |
| 464 _WritePolicyCode(f, policy) | 461 _WritePolicyCode(f, policy) |
| 465 f.write(CPP_FOOT) | 462 f.write(CPP_FOOT) |
| 466 | 463 |
| 467 | 464 |
| 468 #------------------ main() -----------------------------------------# | 465 #------------------ main() -----------------------------------------# |
| 469 if __name__ == '__main__': | 466 if __name__ == '__main__': |
| 470 main(); | 467 main(); |
| OLD | NEW |