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 | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
what is platform? mention it in the doc string?
danno
2011/01/25 14:27:28
Done.
| |
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." | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
I suggest s/one and only/exactly/g
danno
2011/01/25 14:27:28
Done.
| |
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"): | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
Seems like the loop doesn't make sense here. If I
danno
2011/01/25 14:27:28
Done.
| |
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" | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
Why have the option in the first place then? Can a
danno
2011/01/25 14:27:28
Done.
| |
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' | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
Actually, the json file to read is passed as a com
danno
2011/01/25 14:27:28
Done.
| |
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' | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
80 chars
danno
2011/01/25 14:27:28
Done.
| |
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) | |
Mattias Nissler (ping if slow)
2011/01/25 11:42:21
Still no proper JSON parsing for policy templates?
danno
2011/01/25 14:27:28
nope!
On 2011/01/25 11:42:21, Mattias Nissler wrot
| |
146 | |
147 | |
148 if __name__ == '__main__': | |
149 main(); | |
OLD | NEW |