Chromium Code Reviews| OLD | NEW |
|---|---|
| 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') | |
|
Mattias Nissler (ping if slow)
2012/09/12 13:06:27
this is nice!
| |
| 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 Loading... | |
| 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) | |
| 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 # Tuples are sorted in lexicographical order, which will sort by policy name |
| 133 # in this case. | 140 # in this case. |
| 134 policies.sort() | 141 policies.sort() |
| 135 return policies | 142 return policies |
| 136 | 143 |
| 137 | 144 |
| 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): | 145 def _LoadJSONFile(json_file): |
| 154 with open(json_file, 'r') as f: | 146 with open(json_file, 'r') as f: |
| 155 text = f.read() | 147 text = f.read() |
| 156 return eval(text) | 148 return eval(text) |
| 157 | 149 |
| 158 | 150 |
| 159 #------------------ policy constants header ------------------------# | 151 #------------------ policy constants header ------------------------# |
| 160 def _WritePolicyConstantHeader(template_file_contents, args, opts): | 152 def _WritePolicyConstantHeader(template_file_contents, args, opts): |
| 161 os = args[0] | 153 os = args[0] |
| 162 with open(opts.header_path, 'w') as f: | 154 with open(opts.header_path, 'w') as f: |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 179 'configuration resides.\n' | 171 'configuration resides.\n' |
| 180 'extern const wchar_t kRegistryRecommendedSubKey[];\n\n') | 172 'extern const wchar_t kRegistryRecommendedSubKey[];\n\n') |
| 181 | 173 |
| 182 f.write('// Lists policy types mapped to their names and expected types.\n' | 174 f.write('// Lists policy types mapped to their names and expected types.\n' |
| 183 '// Used to initialize ConfigurationPolicyProviders.\n' | 175 '// Used to initialize ConfigurationPolicyProviders.\n' |
| 184 'struct PolicyDefinitionList {\n' | 176 'struct PolicyDefinitionList {\n' |
| 185 ' struct Entry {\n' | 177 ' struct Entry {\n' |
| 186 ' const char* name;\n' | 178 ' const char* name;\n' |
| 187 ' base::Value::Type value_type;\n' | 179 ' base::Value::Type value_type;\n' |
| 188 ' bool device_policy;\n' | 180 ' bool device_policy;\n' |
| 181 ' int id;\n' | |
| 189 ' };\n' | 182 ' };\n' |
| 190 '\n' | 183 '\n' |
| 191 ' const Entry* begin;\n' | 184 ' const Entry* begin;\n' |
| 192 ' const Entry* end;\n' | 185 ' const Entry* end;\n' |
| 193 '};\n' | 186 '};\n' |
| 194 '\n' | 187 '\n' |
| 195 '// Returns true if the given policy is deprecated.\n' | 188 '// Returns true if the given policy is deprecated.\n' |
| 196 'bool IsDeprecatedPolicy(const std::string& policy);\n' | 189 'bool IsDeprecatedPolicy(const std::string& policy);\n' |
| 197 '\n' | 190 '\n' |
| 198 '// Returns the default policy definition list for Chrome.\n' | 191 '// Returns the default policy definition list for Chrome.\n' |
| 199 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') | 192 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') |
| 200 f.write('// Key names for the policy settings.\n' | 193 f.write('// Key names for the policy settings.\n' |
| 201 'namespace key {\n\n') | 194 'namespace key {\n\n') |
| 202 for policy_name in _GetPolicyNameList(template_file_contents): | 195 for policy in _GetPolicyList(template_file_contents): |
| 203 f.write('extern const char k' + policy_name + '[];\n') | 196 f.write('extern const char k' + policy.name + '[];\n') |
| 204 f.write('\n} // namespace key\n\n' | 197 f.write('\n} // namespace key\n\n' |
| 205 '} // namespace policy\n\n' | 198 '} // namespace policy\n\n' |
| 206 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') | 199 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| 207 | 200 |
| 208 | 201 |
| 209 #------------------ policy constants source ------------------------# | 202 #------------------ policy constants source ------------------------# |
| 210 def _WritePolicyConstantSource(template_file_contents, args, opts): | 203 def _WritePolicyConstantSource(template_file_contents, args, opts): |
| 211 os = args[0] | 204 os = args[0] |
| 212 is_chromium_os = args[1] == '1' | 205 is_chromium_os = args[1] == '1' |
| 213 platform = None | 206 platform = None |
| 214 platform_wildcard = None | 207 platform_wildcard = None |
| 215 if is_chromium_os: | 208 if is_chromium_os: |
| 216 platform = 'chrome_os' | 209 platform = 'chrome_os' |
| 217 else: | 210 else: |
| 218 platform = 'chrome.' + os.lower() | 211 platform = 'chrome.' + os.lower() |
| 219 platform_wildcard = 'chrome.*' | 212 platform_wildcard = 'chrome.*' |
| 220 with open(opts.source_path, 'w') as f: | 213 with open(opts.source_path, 'w') as f: |
| 221 _OutputGeneratedWarningForC(f, args[2]) | 214 _OutputGeneratedWarningForC(f, args[2]) |
| 222 | 215 |
| 223 f.write('#include "base/basictypes.h"\n' | 216 f.write('#include "base/basictypes.h"\n' |
| 224 '#include "base/logging.h"\n' | 217 '#include "base/logging.h"\n' |
| 225 '#include "policy/policy_constants.h"\n' | 218 '#include "policy/policy_constants.h"\n' |
| 226 '\n' | 219 '\n' |
| 227 'namespace policy {\n\n') | 220 'namespace policy {\n\n') |
| 228 | 221 |
| 229 f.write('namespace {\n\n') | 222 f.write('namespace {\n\n') |
| 230 | 223 |
| 231 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') | 224 f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') |
| 232 policy_list = _GetChromePolicyList(template_file_contents) | 225 for policy in _GetPolicyList(template_file_contents): |
| 233 for (name, platforms, vtype, device_policy) in policy_list: | 226 if (platform in policy.platforms) or \ |
| 234 if (platform in platforms) or (platform_wildcard in platforms): | 227 (platform_wildcard in policy.platforms): |
| 235 f.write(' { key::k%s, Value::%s, %s },\n' % | 228 f.write( |
| 236 (name, vtype, 'true' if device_policy else 'false')) | 229 ' {{ key::k{name}, Value::{vtype}, {is_device_policy}, {id} }},\n'. |
|
Mattias Nissler (ping if slow)
2012/09/12 13:06:27
I think the previous code was easier to read.
qfel
2012/09/18 11:30:56
Done.
| |
| 230 format(name=policy.name, vtype=policy.vtype, id=policy.id, | |
| 231 is_device_policy='true' if policy.is_device_policy | |
| 232 else 'false')) | |
| 237 f.write('};\n\n') | 233 f.write('};\n\n') |
| 238 | 234 |
| 239 f.write('const PolicyDefinitionList kChromePolicyList = {\n' | 235 f.write('const PolicyDefinitionList kChromePolicyList = {\n' |
| 240 ' kEntries,\n' | 236 ' kEntries,\n' |
| 241 ' kEntries + arraysize(kEntries),\n' | 237 ' kEntries + arraysize(kEntries),\n' |
| 242 '};\n\n') | 238 '};\n\n') |
| 243 | 239 |
| 244 f.write('// List of deprecated policies.\n' | 240 f.write('// List of deprecated policies.\n' |
| 245 'const char* kDeprecatedPolicyList[] = {\n') | 241 'const char* kDeprecatedPolicyList[] = {\n') |
| 246 for name in _GetDeprecatedPolicyList(template_file_contents): | 242 for policy in _GetPolicyList(template_file_contents): |
| 247 f.write(' key::k%s,\n' % name) | 243 if policy.is_deprecated: |
| 244 f.write(' key::k%s,\n' % policy.name) | |
| 248 f.write('};\n\n') | 245 f.write('};\n\n') |
| 249 | 246 |
| 250 f.write('} // namespace\n\n') | 247 f.write('} // namespace\n\n') |
| 251 | 248 |
| 252 if os == 'win': | 249 if os == 'win': |
| 253 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' | 250 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
| 254 'const wchar_t kRegistryMandatorySubKey[] = ' | 251 'const wchar_t kRegistryMandatorySubKey[] = ' |
| 255 'L"' + CHROME_MANDATORY_SUBKEY + '";\n' | 252 'L"' + CHROME_MANDATORY_SUBKEY + '";\n' |
| 256 'const wchar_t kRegistryRecommendedSubKey[] = ' | 253 'const wchar_t kRegistryRecommendedSubKey[] = ' |
| 257 'L"' + CHROME_RECOMMENDED_SUBKEY + '";\n' | 254 'L"' + CHROME_RECOMMENDED_SUBKEY + '";\n' |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 269 ' return true;\n' | 266 ' return true;\n' |
| 270 ' }\n' | 267 ' }\n' |
| 271 ' return false;\n' | 268 ' return false;\n' |
| 272 '}\n' | 269 '}\n' |
| 273 '\n' | 270 '\n' |
| 274 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' | 271 'const PolicyDefinitionList* GetChromePolicyDefinitionList() {\n' |
| 275 ' return &kChromePolicyList;\n' | 272 ' return &kChromePolicyList;\n' |
| 276 '}\n\n') | 273 '}\n\n') |
| 277 | 274 |
| 278 f.write('namespace key {\n\n') | 275 f.write('namespace key {\n\n') |
| 279 for policy_name in _GetPolicyNameList(template_file_contents): | 276 for policy in _GetPolicyList(template_file_contents): |
| 280 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) | 277 f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name)) |
| 281 f.write('\n} // namespace key\n\n' | 278 f.write('\n} // namespace key\n\n' |
| 282 '} // namespace policy\n') | 279 '} // namespace policy\n') |
| 283 | 280 |
| 284 | 281 |
| 285 #------------------ policy protobuf --------------------------------# | 282 #------------------ policy protobuf --------------------------------# |
| 286 PROTO_HEAD = ''' | 283 PROTO_HEAD = ''' |
| 287 syntax = "proto2"; | 284 syntax = "proto2"; |
| 288 | 285 |
| 289 option optimize_for = LITE_RUNTIME; | 286 option optimize_for = LITE_RUNTIME; |
| 290 | 287 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 if policy['type'] == 'group': | 470 if policy['type'] == 'group': |
| 474 for sub_policy in policy['policies']: | 471 for sub_policy in policy['policies']: |
| 475 _WritePolicyCode(f, sub_policy) | 472 _WritePolicyCode(f, sub_policy) |
| 476 else: | 473 else: |
| 477 _WritePolicyCode(f, policy) | 474 _WritePolicyCode(f, policy) |
| 478 f.write(CPP_FOOT) | 475 f.write(CPP_FOOT) |
| 479 | 476 |
| 480 | 477 |
| 481 if __name__ == '__main__': | 478 if __name__ == '__main__': |
| 482 sys.exit(main()) | 479 sys.exit(main()) |
| OLD | NEW |