Chromium Code Reviews| 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 -h generate header file of policy constants | |
|
gfeher
2011/01/04 12:58:06
Nit: turn this into --something?
danno
2011/01/11 13:03:48
Done.
| |
| 9 --cc generate source file of policy constants | |
| 10 --pte generate header file for policy type enumeration | |
| 11 --proto generate .proto file containing all template_file_contents | |
| 12 ''' | |
| 13 | |
| 14 import getopt; | |
| 15 import sys; | |
| 16 | |
| 17 def main(): | |
| 18 try: | |
| 19 opts, args = getopt.getopt(sys.argv[1:], "h", ["cc", "proto", "pte", "help"] ) | |
| 20 except getopt.GetoptError, err: | |
| 21 print str(err) # will print something like "option -a not recognized" | |
| 22 usage() | |
| 23 sys.exit(2) | |
| 24 if len(opts) < 1 or len(opts) > 1: | |
| 25 print "one and only one option must be specified." | |
| 26 usage() | |
| 27 sys.exit(2) | |
| 28 if len(args) < 2 or len(args) > 2: | |
| 29 print "both an input and output file must be specified." | |
| 30 usage() | |
| 31 sys.exit(2) | |
| 32 template_file_contents = _LoadJSONFile(args[0]); | |
| 33 option, arg = opts[0] | |
| 34 if option == "--help": | |
| 35 usage() | |
| 36 elif option == "-h": | |
| 37 _WritePolicyConstantHeader(template_file_contents, args[1]); | |
| 38 elif option == "--cc": | |
| 39 _WritePolicyConstantSource(template_file_contents, args[1]); | |
| 40 elif option == "--pte": | |
| 41 _WritePolicyTypeEnumerationHeader(template_file_contents, args[1]); | |
| 42 elif option == "--proto": | |
| 43 # TODO(danno): generate the protobuffer. | |
| 44 print "not implemented" | |
| 45 sys.exit(2) | |
| 46 | |
| 47 def usage(): | |
| 48 print __doc__ | |
| 49 | |
| 50 def _WritePolicyConstantHeader(template_file_contents, header_path): | |
| 51 with open(header_path, "w") as f: | |
| 52 f.write('''#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_ | |
| 53 #define CHROME_COMMON_POLICY_CONSTANTS_H_ | |
| 54 #pragma once | |
| 55 | |
| 56 // WARNING: This file is generated, to not modify it directly. | |
| 57 | |
| 58 #include "build/build_config.h" | |
| 59 | |
| 60 namespace policy { | |
| 61 | |
| 62 #if defined(OS_WIN) | |
| 63 // The windows registry path we read the policy configuration from. | |
| 64 extern const wchar_t kRegistrySubKey[]; | |
| 65 #endif | |
| 66 | |
| 67 // Key names for the policy settings. | |
| 68 namespace key { | |
| 69 | |
| 70 ''') | |
|
gfeher
2011/01/04 12:58:06
These multi-line string constants make it difficul
danno
2011/01/11 13:03:48
Done.
| |
| 71 for policy_name in _GetPolicyNameList(template_file_contents): | |
| 72 f.write('extern const char k' + policy_name + '[];\n') | |
| 73 f.write(''' | |
| 74 } // namespace key | |
| 75 | |
| 76 } // namespace policy | |
| 77 | |
| 78 #endif // CHROME_COMMON_POLICY_CONSTANTS_H_ | |
| 79 ''') | |
| 80 f.closed | |
|
gfeher
2011/01/04 12:58:06
f.close() ?
danno
2011/01/11 13:03:48
Done.
| |
| 81 | |
| 82 def _WritePolicyConstantSource(template_file_contents, header_path): | |
| 83 with open(header_path, "w") as f: | |
| 84 f.write('''#include "chrome/common/policy_constants.h" | |
| 85 | |
| 86 // WARNING: This file is generated, to not modify it directly. | |
| 87 | |
| 88 namespace policy { | |
| 89 | |
| 90 #if defined(OS_WIN) | |
| 91 #if defined(GOOGLE_CHROME_BUILD) | |
| 92 const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; | |
| 93 #else | |
| 94 const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Chromium"; | |
| 95 #endif | |
| 96 #endif | |
| 97 | |
| 98 namespace key { | |
| 99 | |
| 100 ''') | |
| 101 for policy_name in _GetPolicyNameList(template_file_contents): | |
| 102 f.write('const char k' + policy_name) | |
| 103 f.write('[] = "' + policy_name + '";\n') | |
|
gfeher
2011/01/04 12:58:06
I suggest the following for the above two lines:
f
danno
2011/01/11 13:03:48
Done.
| |
| 104 f.write(''' | |
| 105 } // namespace key | |
| 106 | |
| 107 } // namespace policy | |
| 108 ''') | |
| 109 f.closed | |
| 110 | |
| 111 def _WritePolicyTypeEnumerationHeader(template_file_contents, header_path): | |
| 112 with open(header_path, "w") as f: | |
| 113 f.write('''#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_ | |
| 114 #define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_ | |
| 115 #pragma once | |
| 116 | |
| 117 // WARNING: This file is generated, to not modify it directly. | |
| 118 | |
| 119 namespace policy { | |
| 120 | |
| 121 enum ConfigurationPolicyType { | |
| 122 ''') | |
| 123 for policy_name in _GetPolicyNameList(template_file_contents): | |
| 124 f.write(' kPolicy' + policy_name + ",\n"); | |
| 125 f.write('''}; | |
| 126 | |
| 127 } // namespace policy | |
| 128 | |
| 129 #endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_ | |
| 130 ''') | |
| 131 f.closed | |
| 132 | |
| 133 def _GetPolicyNameList(template_file_contents): | |
| 134 policy_names = []; | |
| 135 for policy in template_file_contents['policy_definitions']: | |
| 136 if policy['type'] == 'group': | |
| 137 for sub_policy in policy['policies']: | |
| 138 policy_names.append(sub_policy['name']) | |
| 139 else: | |
| 140 policy_names.append(policy['name']) | |
| 141 policy_names.sort() | |
| 142 return policy_names | |
| 143 | |
| 144 def _LoadJSONFile(json_file): | |
| 145 with open(json_file, "r") as f: | |
| 146 text = f.read() | |
| 147 f.closed | |
|
gfeher
2011/01/04 12:58:06
Didn't you mean f.close() ?
gfeher
2011/01/04 13:13:17
Okay, sorry, now I've looked up the with statement
danno
2011/01/11 13:03:48
Done.
danno
2011/01/11 13:03:48
Done.
| |
| 148 globs = {} | |
| 149 exec('data = ' + text, globs) | |
| 150 return globs['data'] | |
| 151 | |
| 152 if __name__ == '__main__': | |
| 153 main(); | |
| OLD | NEW |