OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''python %prog [options] platform chromium_os_flag template | 6 '''python %prog [options] platform chromium_os_flag template |
7 | 7 |
8 platform specifies which platform source is being generated for | 8 platform specifies which platform source is being generated for |
9 and can be one of (win, mac, linux) | 9 and can be one of (win, mac, linux) |
10 chromium_os_flag should be 1 if this is a Chromium OS build | 10 chromium_os_flag should be 1 if this is a Chromium OS build |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 'choice'), | 55 'choice'), |
56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList', | 56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList', |
57 'multi-select'), | 57 'multi-select'), |
58 } | 58 } |
59 | 59 |
60 class EnumItem: | 60 class EnumItem: |
61 def __init__(self, item): | 61 def __init__(self, item): |
62 self.caption = PolicyDetails._RemovePlaceholders(item['caption']) | 62 self.caption = PolicyDetails._RemovePlaceholders(item['caption']) |
63 self.value = item['value'] | 63 self.value = item['value'] |
64 | 64 |
65 def __init__(self, policy, os, is_chromium_os): | 65 def __init__(self, policy, version, os, is_chromium_os): |
Mattias Nissler (ping if slow)
2015/04/23 10:14:53
nit: s/version/chrome_major_version/ for clarity.
Thiemo Nagel
2015/04/23 10:56:26
Done.
| |
66 self.id = policy['id'] | 66 self.id = policy['id'] |
67 self.name = policy['name'] | 67 self.name = policy['name'] |
68 features = policy.get('features', {}) | 68 features = policy.get('features', {}) |
69 self.can_be_recommended = features.get('can_be_recommended', False) | 69 self.can_be_recommended = features.get('can_be_recommended', False) |
70 self.can_be_mandatory = features.get('can_be_mandatory', True) | 70 self.can_be_mandatory = features.get('can_be_mandatory', True) |
71 self.is_deprecated = policy.get('deprecated', False) | 71 self.is_deprecated = policy.get('deprecated', False) |
72 self.is_device_only = policy.get('device_only', False) | 72 self.is_device_only = policy.get('device_only', False) |
73 self.schema = policy.get('schema', {}) | 73 self.schema = policy.get('schema', {}) |
74 self.has_enterprise_default = 'default_for_enterprise_users' in policy | 74 self.has_enterprise_default = 'default_for_enterprise_users' in policy |
75 if self.has_enterprise_default: | 75 if self.has_enterprise_default: |
76 self.enterprise_default = policy['default_for_enterprise_users'] | 76 self.enterprise_default = policy['default_for_enterprise_users'] |
77 | 77 |
78 major_version = int(version.split('.')[0]) | |
Mattias Nissler (ping if slow)
2015/04/23 10:14:53
As far as I understand, you're passing the major v
Thiemo Nagel
2015/04/23 10:56:26
You're right. Thanks.
| |
78 expected_platform = 'chrome_os' if is_chromium_os else os.lower() | 79 expected_platform = 'chrome_os' if is_chromium_os else os.lower() |
79 self.platforms = [] | 80 self.platforms = [] |
80 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: | 81 for platform, version_range in [ p.split(':') |
81 if not version.endswith('-'): | 82 for p in policy['supported_on'] ]: |
83 split_result = version_range.split('-') | |
84 if len(split_result) != 2: | |
85 raise RuntimeError('supported_on must have exactly one dash: "%s"' % p) | |
86 (version_min, version_max) = split_result | |
87 if version_min == '': | |
88 raise RuntimeError('supported_on must define a start version: "%s"' % p) | |
89 | |
90 # Skip if the current Chromium version does not support the policy. | |
91 if (int(version_min) > major_version or | |
92 version_max != '' and int(version_max) < major_version): | |
82 continue | 93 continue |
83 | 94 |
84 if platform.startswith('chrome.'): | 95 if platform.startswith('chrome.'): |
85 platform_sub = platform[7:] | 96 platform_sub = platform[7:] |
86 if platform_sub == '*': | 97 if platform_sub == '*': |
87 self.platforms.extend(['win', 'mac', 'linux']) | 98 self.platforms.extend(['win', 'mac', 'linux']) |
88 else: | 99 else: |
89 self.platforms.append(platform_sub) | 100 self.platforms.append(platform_sub) |
90 else: | 101 else: |
91 self.platforms.append(platform) | 102 self.platforms.append(platform) |
(...skipping 28 matching lines...) Expand all Loading... | |
120 result = '' | 131 result = '' |
121 pos = 0 | 132 pos = 0 |
122 for m in PolicyDetails.PH_PATTERN.finditer(text): | 133 for m in PolicyDetails.PH_PATTERN.finditer(text): |
123 result += text[pos:m.start(0)] | 134 result += text[pos:m.start(0)] |
124 result += m.group(2) or m.group(1) | 135 result += m.group(2) or m.group(1) |
125 pos = m.end(0) | 136 pos = m.end(0) |
126 result += text[pos:] | 137 result += text[pos:] |
127 return result | 138 return result |
128 | 139 |
129 | 140 |
141 def ParseMajorVersion(version_path): | |
Mattias Nissler (ping if slow)
2015/04/23 10:14:53
nit: This should be ParseVersionFile as it doesn't
Thiemo Nagel
2015/04/23 10:56:26
Done.
| |
142 version = None | |
143 for line in open(version_path, 'r').readlines(): | |
144 key, val = line.rstrip('\r\n').split('=', 1) | |
145 if key == 'MAJOR': | |
146 version = val | |
147 break | |
148 if version is None: | |
149 raise RuntimeError('VERSION file does not contain major version.') | |
150 return version | |
151 | |
152 | |
130 def main(): | 153 def main(): |
131 parser = OptionParser(usage=__doc__) | 154 parser = OptionParser(usage=__doc__) |
132 parser.add_option('--pch', '--policy-constants-header', dest='header_path', | 155 parser.add_option('--pch', '--policy-constants-header', dest='header_path', |
133 help='generate header file of policy constants', | 156 help='generate header file of policy constants', |
134 metavar='FILE') | 157 metavar='FILE') |
135 parser.add_option('--pcc', '--policy-constants-source', dest='source_path', | 158 parser.add_option('--pcc', '--policy-constants-source', dest='source_path', |
136 help='generate source file of policy constants', | 159 help='generate source file of policy constants', |
137 metavar='FILE') | 160 metavar='FILE') |
138 parser.add_option('--cpp', '--cloud-policy-protobuf', | 161 parser.add_option('--cpp', '--cloud-policy-protobuf', |
139 dest='cloud_policy_proto_path', | 162 dest='cloud_policy_proto_path', |
140 help='generate cloud policy protobuf file', | 163 help='generate cloud policy protobuf file', |
141 metavar='FILE') | 164 metavar='FILE') |
142 parser.add_option('--csp', '--chrome-settings-protobuf', | 165 parser.add_option('--csp', '--chrome-settings-protobuf', |
143 dest='chrome_settings_proto_path', | 166 dest='chrome_settings_proto_path', |
144 help='generate chrome settings protobuf file', | 167 help='generate chrome settings protobuf file', |
145 metavar='FILE') | 168 metavar='FILE') |
146 parser.add_option('--cpd', '--cloud-policy-decoder', | 169 parser.add_option('--cpd', '--cloud-policy-decoder', |
147 dest='cloud_policy_decoder_path', | 170 dest='cloud_policy_decoder_path', |
148 help='generate C++ code decoding the cloud policy protobuf', | 171 help='generate C++ code decoding the cloud policy protobuf', |
149 metavar='FILE') | 172 metavar='FILE') |
150 parser.add_option('--ard', '--app-restrictions-definition', | 173 parser.add_option('--ard', '--app-restrictions-definition', |
151 dest='app_restrictions_path', | 174 dest='app_restrictions_path', |
152 help='generate an XML file as specified by ' | 175 help='generate an XML file as specified by ' |
153 'Android\'s App Restriction Schema', | 176 'Android\'s App Restriction Schema', |
154 metavar='FILE') | 177 metavar='FILE') |
155 | 178 |
156 (opts, args) = parser.parse_args() | 179 (opts, args) = parser.parse_args() |
157 | 180 |
158 if len(args) != 3: | 181 if len(args) != 4: |
159 print 'exactly platform, chromium_os flag and input file must be specified.' | 182 print('Exactly VERSION file, platform, chromium_os flag and ' |
183 'input file must be specified as positional parameters.') | |
160 parser.print_help() | 184 parser.print_help() |
161 return 2 | 185 return 2 |
162 | 186 |
163 os = args[0] | 187 version_path = args[0] |
164 is_chromium_os = args[1] == '1' | 188 os = args[1] |
165 template_file_name = args[2] | 189 is_chromium_os = args[2] == '1' |
190 template_file_name = args[3] | |
166 | 191 |
192 major_version = ParseMajorVersion(version_path) | |
167 template_file_contents = _LoadJSONFile(template_file_name) | 193 template_file_contents = _LoadJSONFile(template_file_name) |
168 policy_details = [ PolicyDetails(policy, os, is_chromium_os) | 194 policy_details = [ PolicyDetails(policy, major_version, os, is_chromium_os) |
169 for policy in _Flatten(template_file_contents) ] | 195 for policy in _Flatten(template_file_contents) ] |
170 sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name) | 196 sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name) |
171 | 197 |
172 def GenerateFile(path, writer, sorted=False, xml=False): | 198 def GenerateFile(path, writer, sorted=False, xml=False): |
173 if path: | 199 if path: |
174 with open(path, 'w') as f: | 200 with open(path, 'w') as f: |
175 _OutputGeneratedWarningHeader(f, template_file_name, xml) | 201 _OutputGeneratedWarningHeader(f, template_file_name, xml) |
176 writer(sorted and sorted_policy_details or policy_details, os, f) | 202 writer(sorted and sorted_policy_details or policy_details, os, f) |
177 | 203 |
178 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) | 204 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1023 # _WriteAppRestrictions body | 1049 # _WriteAppRestrictions body |
1024 f.write('<restrictions xmlns:android="' | 1050 f.write('<restrictions xmlns:android="' |
1025 'http://schemas.android.com/apk/res/android">\n\n') | 1051 'http://schemas.android.com/apk/res/android">\n\n') |
1026 for policy in policies: | 1052 for policy in policies: |
1027 if policy.is_supported and policy.restriction_type != 'invalid': | 1053 if policy.is_supported and policy.restriction_type != 'invalid': |
1028 WriteAppRestriction(policy) | 1054 WriteAppRestriction(policy) |
1029 f.write('</restrictions>') | 1055 f.write('</restrictions>') |
1030 | 1056 |
1031 if __name__ == '__main__': | 1057 if __name__ == '__main__': |
1032 sys.exit(main()) | 1058 sys.exit(main()) |
OLD | NEW |