Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Unified Diff: chrome/tools/build/generate_policy_source.py

Issue 6002015: Policy: generate boilerplate policy type and constant code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: tweaks and right diff Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..83a8a0b3b2a670e191e575c9f1240378135eadc3
--- /dev/null
+++ b/chrome/tools/build/generate_policy_source.py
@@ -0,0 +1,139 @@
+#!/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
+ --policy-constants-header generate header file of policy constants
+ --policy-constants-source generate source file of policy constants
+ --policy-type-header 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", ["policy-constants-header",
+ "policy-constants-source",
+ "proto",
+ "policy-type-header",
+ "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 in ("--help", "-h"):
+ usage()
+ elif option == "--policy-constants-header":
+ _WritePolicyConstantHeader(template_file_contents, args[1]);
+ elif option == "--policy-constants-source":
+ _WritePolicyConstantSource(template_file_contents, args[1]);
+ elif option == "--policy-type-header":
+ _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 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.
+ f.write('//\n'
+ '// DO NOT MODIFY THIS FILE DIRECTLY!\n'
+ '// ITS IS GENERATED BY generate_policy_source.py\n'
+ '//\n\n')
+
+def _WritePolicyConstantHeader(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 defined(OS_WIN)\n'
+ '// The windows registry path policy configuration resides.\n'
+ 'extern const wchar_t kRegistrySubKey[];\n'
+ '#endif\n'
+ '\n'
+ '// 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(template_file_contents, source_path):
+ with open(source_path, "w") as f:
+ OutputGeneratedWarningForC(f)
+ f.write('#include "chrome/common/policy_constants.h"\n'
+ '\n'
+ 'namespace policy {\n'
+ '\n'
+ '#if defined(OS_WIN)\n'
+ '#if defined(GOOGLE_CHROME_BUILD)\n'
+ 'const wchar_t kRegistrySubKey[] = '
+ '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.
+ '#else\n'
+ 'const wchar_t kRegistrySubKey[] = '
+ 'L"SOFTWARE\\Policies\\Chromium";\n'
+ '#endif\n'
+ '#endif\n\nnamespace 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')
+
+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_')
+
+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()
+ globs = {}
+ 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.
+ return globs['data']
+
+if __name__ == '__main__':
+ main();
« chrome/browser/policy/configuration_policy_store_interface.h ('K') | « chrome/common_constants.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698