Chromium Code Reviews| Index: chrome/tools/build/generate_policy_source.py |
| diff --git a/chrome/tools/build/generate_policy_source.py b/chrome/tools/build/generate_policy_source.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..63a80eed6d4ae9a23c001a40dd880e397d4862b6 |
| --- /dev/null |
| +++ b/chrome/tools/build/generate_policy_source.py |
| @@ -0,0 +1,149 @@ |
| +#!/usr/bin/python |
| +# Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +''' |
| +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.
|
| + --policy-constants-header=out_file generate header file of policy constants |
| + --policy-constants-source=out_file generate source file of policy constants |
| + --policy-type-header=out_file generate header file for policy type |
| + enumeration |
| + --proto=out_file generate .proto file containing all |
| + template_file_contents |
| +''' |
| + |
| + |
| +from __future__ import with_statement |
| +import getopt; |
| +import sys; |
| + |
| + |
| +CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome'; |
| +CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium'; |
| + |
| + |
| +def main(): |
| + try: |
| + opts, args = getopt.getopt(sys.argv[1:], "h", ["policy-constants-header=", |
| + "policy-constants-source=", |
| + "proto=", |
| + "policy-type-header=", |
| + "help"]) |
| + except getopt.GetoptError, err: |
| + print str(err) |
| + usage() |
| + sys.exit(2) |
| + if len(args) < 2 or len(args) > 2: |
| + 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.
|
| + usage() |
| + sys.exit(2) |
| + template_file_contents = _LoadJSONFile(args[1]); |
| + platform = args[0]; |
| + for option, arg in opts: |
| + 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.
|
| + usage() |
| + sys.exit(2) |
| + elif option == "--policy-constants-header": |
| + _WritePolicyConstantHeader(platform, template_file_contents, arg); |
| + elif option == "--policy-constants-source": |
| + _WritePolicyConstantSource(platform, template_file_contents, arg); |
| + elif option == "--policy-type-header": |
| + _WritePolicyTypeEnumerationHeader(template_file_contents, arg); |
| + elif option == "--proto": |
| + # TODO(danno): generate the protobuffer. |
| + 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.
|
| + sys.exit(2) |
| + |
| + |
| +def usage(): |
| + print __doc__ |
| + |
| + |
| +def _OutputGeneratedWarningForC(f): |
| + f.write('//\n' |
| + '// DO NOT MODIFY THIS FILE DIRECTLY!\n' |
| + '// IT IS GENERATED BY generate_policy_source.py\n' |
| + '// 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.
|
| + '//\n\n') |
| + |
| + |
| +def _WritePolicyConstantHeader(platform, template_file_contents, header_path): |
| + with open(header_path, "w") as f: |
| + _OutputGeneratedWarningForC(f) |
| + f.write('#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| + '#define CHROME_COMMON_POLICY_CONSTANTS_H_\n' |
| + '#pragma once\n' |
| + '\n' |
| + 'namespace policy {\n\n') |
| + if platform == "win": |
| + 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.
|
| + 'extern const wchar_t kRegistrySubKey[];\n\n') |
| + f.write('// Key names for the policy settings.\n' |
| + 'namespace key {\n\n') |
| + for policy_name in _GetPolicyNameList(template_file_contents): |
| + f.write('extern const char k' + policy_name + '[];\n') |
| + f.write('\n} // namespace key\n\n' |
| + '} // namespace policy\n\n' |
| + '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| + |
| + |
| +def _WritePolicyConstantSource(platform, template_file_contents, source_path): |
| + with open(source_path, "w") as f: |
| + _OutputGeneratedWarningForC(f) |
| + f.write('#include "policy/policy_constants.h"\n' |
| + '\n' |
| + 'namespace policy {\n' |
| + '\n') |
| + if platform == "win": |
| + f.write('#if defined(GOOGLE_CHROME_BUILD)\n' |
| + 'const wchar_t kRegistrySubKey[] = ' |
| + 'L"' + CHROME_SUBKEY + '";\n' |
| + '#else\n' |
| + 'const wchar_t kRegistrySubKey[] = ' |
| + 'L"' + CHROMIUM_SUBKEY + '";\n' |
| + '#endif\n\n') |
| + f.write('namespace key {\n\n') |
| + for policy_name in _GetPolicyNameList(template_file_contents): |
| + f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) |
| + f.write('\n} // namespace key\n\n' |
| + '} // namespace policy\n') |
| + |
| + |
| +def _WritePolicyTypeEnumerationHeader(template_file_contents, header_path): |
| + with open(header_path, "w") as f: |
| + _OutputGeneratedWarningForC(f) |
| + f.write('#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
| + '#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n' |
| + '#pragma once\n' |
| + '\n' |
| + 'namespace policy {\n' |
| + '\n' |
| + 'enum ConfigurationPolicyType {\n') |
| + for policy_name in _GetPolicyNameList(template_file_contents): |
| + f.write(' kPolicy' + policy_name + ",\n"); |
| + f.write('};\n\n' |
| + '} // namespace policy\n\n' |
| + '#endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_\n') |
| + |
| + |
| +def _GetPolicyNameList(template_file_contents): |
| + policy_names = []; |
| + for policy in template_file_contents['policy_definitions']: |
| + if policy['type'] == 'group': |
| + for sub_policy in policy['policies']: |
| + policy_names.append(sub_policy['name']) |
| + else: |
| + policy_names.append(policy['name']) |
| + policy_names.sort() |
| + return policy_names |
| + |
| + |
| +def _LoadJSONFile(json_file): |
| + with open(json_file, "r") as f: |
| + text = f.read() |
| + 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
|
| + |
| + |
| +if __name__ == '__main__': |
| + main(); |