summaryrefslogtreecommitdiffstats
path: root/chrome/tools/build/generate_policy_source.py
blob: 1051092ece054d10ab6d4524bdecdef07fd38091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/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.

'''python %prog [options] platform template

platform specifies which platform source is being generated for
  and can be one of (win, mac, linux).
template is the path to a .json policy template file.'''

from __future__ import with_statement
from optparse import OptionParser
import sys;


CHROME_SUBKEY = 'SOFTWARE\\\\Policies\\\\Google\\\\Chrome';
CHROMIUM_SUBKEY = 'SOFTWARE\\\\Policies\\\\Chromium';


def main():
  parser = OptionParser(usage=__doc__);
  parser.add_option("--pch", "--policy-constants-header", dest="header_path",
                    help="generate header file of policy constants",
                    metavar="FILE");
  parser.add_option("--pcc", "--policy-constants-source", dest="source_path",
                    help="generate source file of policy constants",
                    metavar="FILE");
  parser.add_option("--pth", "--policy-type-header", dest="type_path",
                    help="generate header file for policy type enumeration",
                    metavar="FILE");

  (opts, args) = parser.parse_args();

  if len(args) < 2 or len(args) > 2:
    print "exactly one platform and input file must be specified."
    parser.print_help()
    sys.exit(2)
  template_file_contents = _LoadJSONFile(args[1]);
  if opts.header_path is not None:
    _WritePolicyConstantHeader(template_file_contents,
                               args,
                               opts);
  if opts.source_path is not None:
    _WritePolicyConstantSource(template_file_contents,
                               args,
                               opts);
  if opts.type_path is not None:
    _WritePolicyTypeEnumerationHeader(template_file_contents,
                                      args,
                                      opts);


def _OutputGeneratedWarningForC(f, template_file_path):
    f.write('//\n'
            '// DO NOT MODIFY THIS FILE DIRECTLY!\n'
            '// IT IS GENERATED BY generate_policy_source.py\n'
            '// FROM ' + template_file_path + ' \n'
            '//\n\n')


def _WritePolicyConstantHeader(template_file_contents, args, opts):
  platform = args[0];
  with open(opts.header_path, "w") as f:
    _OutputGeneratedWarningForC(f, args[1])
    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.\nextern 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(template_file_contents, args, opts):
  platform = args[0];
  with open(opts.source_path, "w") as f:
    _OutputGeneratedWarningForC(f, args[1])
    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, args, opts):
  with open(opts.type_path, "w") as f:
    _OutputGeneratedWarningForC(f, args[1])
    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)


if __name__ == '__main__':
  main();