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..1b10a74a166de720dda018ea196dcd61e1229ac1 |
| --- /dev/null |
| +++ b/chrome/tools/build/generate_policy_source.py |
| @@ -0,0 +1,153 @@ |
| +#!/usr/bin/python2.4 |
| +# Copyright (c) 2010 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: pyhton generate_policy_source.py [switches] input_file output_file |
| + -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.
|
| + --cc generate source file of policy constants |
| + --pte generate header file for policy type enumeration |
| + --proto generate .proto file containing all template_file_contents |
| +''' |
| + |
| +import getopt; |
| +import sys; |
| + |
| +def main(): |
| + try: |
| + opts, args = getopt.getopt(sys.argv[1:], "h", ["cc", "proto", "pte", "help"]) |
| + except getopt.GetoptError, err: |
| + print str(err) # will print something like "option -a not recognized" |
| + usage() |
| + sys.exit(2) |
| + if len(opts) < 1 or len(opts) > 1: |
| + print "one and only one option must be specified." |
| + usage() |
| + sys.exit(2) |
| + if len(args) < 2 or len(args) > 2: |
| + print "both an input and output file must be specified." |
| + usage() |
| + sys.exit(2) |
| + template_file_contents = _LoadJSONFile(args[0]); |
| + option, arg = opts[0] |
| + if option == "--help": |
| + usage() |
| + elif option == "-h": |
| + _WritePolicyConstantHeader(template_file_contents, args[1]); |
| + elif option == "--cc": |
| + _WritePolicyConstantSource(template_file_contents, args[1]); |
| + elif option == "--pte": |
| + _WritePolicyTypeEnumerationHeader(template_file_contents, args[1]); |
| + elif option == "--proto": |
| + # TODO(danno): generate the protobuffer. |
| + print "not implemented" |
| + sys.exit(2) |
| + |
| +def usage(): |
| + print __doc__ |
| + |
| +def _WritePolicyConstantHeader(template_file_contents, header_path): |
| + with open(header_path, "w") as f: |
| + f.write('''#ifndef CHROME_COMMON_POLICY_CONSTANTS_H_ |
| +#define CHROME_COMMON_POLICY_CONSTANTS_H_ |
| +#pragma once |
| + |
| +// WARNING: This file is generated, to not modify it directly. |
| + |
| +#include "build/build_config.h" |
| + |
| +namespace policy { |
| + |
| +#if defined(OS_WIN) |
| +// The windows registry path we read the policy configuration from. |
| +extern const wchar_t kRegistrySubKey[]; |
| +#endif |
| + |
| +// Key names for the policy settings. |
| +namespace key { |
| + |
| +''') |
|
gfeher
2011/01/04 12:58:06
These multi-line string constants make it difficul
danno
2011/01/11 13:03:48
Done.
|
| + for policy_name in _GetPolicyNameList(template_file_contents): |
| + f.write('extern const char k' + policy_name + '[];\n') |
| + f.write(''' |
| +} // namespace key |
| + |
| +} // namespace policy |
| + |
| +#endif // CHROME_COMMON_POLICY_CONSTANTS_H_ |
| +''') |
| + f.closed |
|
gfeher
2011/01/04 12:58:06
f.close() ?
danno
2011/01/11 13:03:48
Done.
|
| + |
| +def _WritePolicyConstantSource(template_file_contents, header_path): |
| + with open(header_path, "w") as f: |
| + f.write('''#include "chrome/common/policy_constants.h" |
| + |
| +// WARNING: This file is generated, to not modify it directly. |
| + |
| +namespace policy { |
| + |
| +#if defined(OS_WIN) |
| +#if defined(GOOGLE_CHROME_BUILD) |
| +const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome"; |
| +#else |
| +const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Chromium"; |
| +#endif |
| +#endif |
| + |
| +namespace key { |
| + |
| +''') |
| + for policy_name in _GetPolicyNameList(template_file_contents): |
| + f.write('const char k' + policy_name) |
| + 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.
|
| + f.write(''' |
| +} // namespace key |
| + |
| +} // namespace policy |
| +''') |
| + f.closed |
| + |
| +def _WritePolicyTypeEnumerationHeader(template_file_contents, header_path): |
| + with open(header_path, "w") as f: |
| + f.write('''#ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_ |
| +#define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_ |
| +#pragma once |
| + |
| +// WARNING: This file is generated, to not modify it directly. |
| + |
| +namespace policy { |
| + |
| +enum ConfigurationPolicyType { |
| +''') |
| + for policy_name in _GetPolicyNameList(template_file_contents): |
| + f.write(' kPolicy' + policy_name + ",\n"); |
| + f.write('''}; |
| + |
| +} // namespace policy |
| + |
| +#endif // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_TYPE_H_ |
| +''') |
| + f.closed |
| + |
| +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() |
| + 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.
|
| + globs = {} |
| + exec('data = ' + text, globs) |
| + return globs['data'] |
| + |
| +if __name__ == '__main__': |
| + main(); |