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