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