OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 ''' |
| 7 Usage: python generate_policy_source.py [switches] platform input_file |
| 8 --policy-constants-header=out_file generate header file of policy constants |
| 9 --policy-constants-source=out_file generate source file of policy constants |
| 10 --policy-type-header=out_file generate header file for policy type |
| 11 enumeration |
| 12 --proto=out_file generate .proto file containing all |
| 13 template_file_contents |
| 14 ''' |
| 15 |
| 16 |
| 17 from __future__ import with_statement |
| 18 import getopt; |
| 19 import sys; |
| 20 |
| 21 |
| 22 CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'; |
| 23 CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium'; |
| 24 |
| 25 |
| 26 def main(): |
| 27 try: |
| 28 opts, args = getopt.getopt(sys.argv[1:], "h", ["policy-constants-header=", |
| 29 "policy-constants-source=", |
| 30 "proto=", |
| 31 "policy-type-header=", |
| 32 "help"]) |
| 33 except getopt.GetoptError, err: |
| 34 print str(err) |
| 35 usage() |
| 36 sys.exit(2) |
| 37 if len(args) < 2 or len(args) > 2: |
| 38 print "one and only one platform and input file must be specified." |
| 39 usage() |
| 40 sys.exit(2) |
| 41 template_file_contents = _LoadJSONFile(args[1]); |
| 42 platform = args[0]; |
| 43 for option, arg in opts: |
| 44 if option in ("--help", "-h"): |
| 45 usage() |
| 46 sys.exit(2) |
| 47 elif option == "--policy-constants-header": |
| 48 _WritePolicyConstantHeader(platform, template_file_contents, arg); |
| 49 elif option == "--policy-constants-source": |
| 50 _WritePolicyConstantSource(platform, template_file_contents, arg); |
| 51 elif option == "--policy-type-header": |
| 52 _WritePolicyTypeEnumerationHeader(template_file_contents, arg); |
| 53 elif option == "--proto": |
| 54 # TODO(danno): generate the protobuffer. |
| 55 print "not implemented" |
| 56 sys.exit(2) |
| 57 |
| 58 |
| 59 def usage(): |
| 60 print __doc__ |
| 61 |
| 62 |
| 63 def _OutputGeneratedWarningForC(f): |
| 64 f.write('//\n' |
| 65 '// DO NOT MODIFY THIS FILE DIRECTLY!\n' |
| 66 '// IT IS GENERATED BY generate_policy_source.py\n' |
| 67 '// FROM policy_templates.json\n' |
| 68 '//\n\n') |
| 69 |
| 70 |
| 71 def _WritePolicyConstantHeader(platform, template_file_contents, header_path): |
| 72 with open(header_path, "w") as f: |
| 73 _OutputGeneratedWarningForC(f) |
| 74 f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 75 '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| 76 '#pragma once\n' |
| 77 '\n' |
| 78 'namespace policy {\n\n') |
| 79 if platform == "win": |
| 80 f.write('// The windows registry path where policy configuration resides.\
n' |
| 81 'extern const wchar_t kRegistrySubKey[];\n\n') |
| 82 f.write('// Key names for the policy settings.\n' |
| 83 'namespace key {\n\n') |
| 84 for policy_name in _GetPolicyNameList(template_file_contents): |
| 85 f.write('extern const char k' + policy_name + '[];\n') |
| 86 f.write('\n} // namespace key\n\n' |
| 87 '} // namespace policy\n\n' |
| 88 '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| 89 |
| 90 |
| 91 def _WritePolicyConstantSource(platform, template_file_contents, source_path): |
| 92 with open(source_path, "w") as f: |
| 93 _OutputGeneratedWarningForC(f) |
| 94 f.write('#include "policy/policy_constants.h"\n' |
| 95 '\n' |
| 96 'namespace policy {\n' |
| 97 '\n') |
| 98 if platform == "win": |
| 99 f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
| 100 'const wchar_t kRegistrySubKey[] = ' |
| 101 'L"' + CHROME_SUBKEY + '";\n' |
| 102 '#else\n' |
| 103 'const wchar_t kRegistrySubKey[] = ' |
| 104 'L"' + CHROMIUM_SUBKEY + '";\n' |
| 105 '#endif\n\n') |
| 106 f.write('namespace key {\n\n') |
| 107 for policy_name in _GetPolicyNameList(template_file_contents): |
| 108 f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) |
| 109 f.write('\n} // namespace key\n\n' |
| 110 '} // namespace policy\n') |
| 111 |
| 112 |
| 113 def _WritePolicyTypeEnumerationHeader(template_file_contents, header_path): |
| 114 with open(header_path, "w") as f: |
| 115 _OutputGeneratedWarningForC(f) |
| 116 f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
| 117 '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
| 118 '#pragma once\n' |
| 119 '\n' |
| 120 'namespace policy {\n' |
| 121 '\n' |
| 122 'enum ConfigurationPolicyType {\n') |
| 123 for policy_name in _GetPolicyNameList(template_file_contents): |
| 124 f.write(' kPolicy' + policy_name + ",\n"); |
| 125 f.write('};\n\n' |
| 126 '} // namespace policy\n\n' |
| 127 '#endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n') |
| 128 |
| 129 |
| 130 def _GetPolicyNameList(template_file_contents): |
| 131 policy_names = []; |
| 132 for policy in template_file_contents['policy_definitions']: |
| 133 if policy['type'] == 'group': |
| 134 for sub_policy in policy['policies']: |
| 135 policy_names.append(sub_policy['name']) |
| 136 else: |
| 137 policy_names.append(policy['name']) |
| 138 policy_names.sort() |
| 139 return policy_names |
| 140 |
| 141 |
| 142 def _LoadJSONFile(json_file): |
| 143 with open(json_file, "r") as f: |
| 144 text = f.read() |
| 145 return eval(text) |
| 146 |
| 147 |
| 148 if __name__ == '__main__': |
| 149 main(); |
OLD | NEW |