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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 metavar='FILE') | 147 metavar='FILE') |
148 parser.add_option('--cpd', '--cloud-policy-decoder', | 148 parser.add_option('--cpd', '--cloud-policy-decoder', |
149 dest='cloud_policy_decoder_path', | 149 dest='cloud_policy_decoder_path', |
150 help='generate C++ code decoding the cloud policy protobuf', | 150 help='generate C++ code decoding the cloud policy protobuf', |
151 metavar='FILE') | 151 metavar='FILE') |
152 parser.add_option('--ard', '--app-restrictions-definition', | 152 parser.add_option('--ard', '--app-restrictions-definition', |
153 dest='app_restrictions_path', | 153 dest='app_restrictions_path', |
154 help='generate an XML file as specified by ' | 154 help='generate an XML file as specified by ' |
155 'Android\'s App Restriction Schema', | 155 'Android\'s App Restriction Schema', |
156 metavar='FILE') | 156 metavar='FILE') |
157 parser.add_option('--arr', '--app-restrictions-resources', | |
158 dest='app_resources_path', | |
159 help='generate an XML file with resources supporting the ' | |
160 'restrictions defined in --app-restrictions-definition ' | |
161 'parameter', | |
162 metavar='FILE') | |
163 | 157 |
164 (opts, args) = parser.parse_args() | 158 (opts, args) = parser.parse_args() |
165 | 159 |
166 if len(args) != 3: | 160 if len(args) != 3: |
167 print 'exactly platform, chromium_os flag and input file must be specified.' | 161 print 'exactly platform, chromium_os flag and input file must be specified.' |
168 parser.print_help() | 162 parser.print_help() |
169 return 2 | 163 return 2 |
170 | 164 |
171 os = args[0] | 165 os = args[0] |
172 is_chromium_os = args[1] == '1' | 166 is_chromium_os = args[1] == '1' |
(...skipping 11 matching lines...) Expand all Loading... |
184 writer(sorted and sorted_policy_details or policy_details, os, f) | 178 writer(sorted and sorted_policy_details or policy_details, os, f) |
185 | 179 |
186 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) | 180 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) |
187 GenerateFile(opts.source_path, _WritePolicyConstantSource, sorted=True) | 181 GenerateFile(opts.source_path, _WritePolicyConstantSource, sorted=True) |
188 GenerateFile(opts.cloud_policy_proto_path, _WriteCloudPolicyProtobuf) | 182 GenerateFile(opts.cloud_policy_proto_path, _WriteCloudPolicyProtobuf) |
189 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf) | 183 GenerateFile(opts.chrome_settings_proto_path, _WriteChromeSettingsProtobuf) |
190 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder) | 184 GenerateFile(opts.cloud_policy_decoder_path, _WriteCloudPolicyDecoder) |
191 | 185 |
192 if os == 'android': | 186 if os == 'android': |
193 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True) | 187 GenerateFile(opts.app_restrictions_path, _WriteAppRestrictions, xml=True) |
194 GenerateFile(opts.app_resources_path, _WriteResourcesForPolicies, xml=True) | |
195 | 188 |
196 return 0 | 189 return 0 |
197 | 190 |
198 | 191 |
199 #------------------ shared helpers ---------------------------------# | 192 #------------------ shared helpers ---------------------------------# |
200 | 193 |
201 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style): | 194 def _OutputGeneratedWarningHeader(f, template_file_path, xml_style): |
202 left_margin = '//' | 195 left_margin = '//' |
203 if xml_style: | 196 if xml_style: |
204 left_margin = ' ' | 197 left_margin = ' ' |
(...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1000 | 993 |
1001 | 994 |
1002 def _WriteCloudPolicyDecoder(policies, os, f): | 995 def _WriteCloudPolicyDecoder(policies, os, f): |
1003 f.write(CPP_HEAD) | 996 f.write(CPP_HEAD) |
1004 for policy in policies: | 997 for policy in policies: |
1005 if policy.is_supported and not policy.is_device_only: | 998 if policy.is_supported and not policy.is_device_only: |
1006 _WritePolicyCode(f, policy) | 999 _WritePolicyCode(f, policy) |
1007 f.write(CPP_FOOT) | 1000 f.write(CPP_FOOT) |
1008 | 1001 |
1009 | 1002 |
1010 def _EscapeResourceString(raw_resource): | |
1011 if type(raw_resource) == int: | |
1012 return raw_resource | |
1013 return xml_escape(raw_resource)\ | |
1014 .replace('\\', '\\\\')\ | |
1015 .replace('\"','\\\"')\ | |
1016 .replace('\'','\\\'') | |
1017 | |
1018 def _WriteAppRestrictions(policies, os, f): | 1003 def _WriteAppRestrictions(policies, os, f): |
1019 | 1004 |
1020 def WriteRestrictionCommon(key): | 1005 def WriteRestrictionCommon(key): |
1021 f.write(' <restriction\n' | 1006 f.write(' <restriction\n' |
1022 ' android:key="%s"\n' % key) | 1007 ' android:key="%s"\n' % key) |
1023 f.write(' android:title="@string/%sTitle"\n' % key) | 1008 f.write(' android:title="@string/%sTitle"\n' % key) |
1024 f.write(' android:description="@string/%sDesc"\n' % key) | 1009 f.write(' android:description="@string/%sDesc"\n' % key) |
1025 | 1010 |
1026 def WriteItemsDefinition(key): | 1011 def WriteItemsDefinition(key): |
1027 f.write(' android:entries="@array/%sEntries"\n' % key) | 1012 f.write(' android:entries="@array/%sEntries"\n' % key) |
(...skipping 10 matching lines...) Expand all Loading... |
1038 f.write('\n\n') | 1023 f.write('\n\n') |
1039 | 1024 |
1040 # _WriteAppRestrictions body | 1025 # _WriteAppRestrictions body |
1041 f.write('<restrictions xmlns:android="' | 1026 f.write('<restrictions xmlns:android="' |
1042 'http://schemas.android.com/apk/res/android">\n\n') | 1027 'http://schemas.android.com/apk/res/android">\n\n') |
1043 for policy in policies: | 1028 for policy in policies: |
1044 if policy.is_supported and policy.restriction_type != 'invalid': | 1029 if policy.is_supported and policy.restriction_type != 'invalid': |
1045 WriteAppRestriction(policy) | 1030 WriteAppRestriction(policy) |
1046 f.write('</restrictions>') | 1031 f.write('</restrictions>') |
1047 | 1032 |
1048 | |
1049 def _WriteResourcesForPolicies(policies, os, f): | |
1050 | |
1051 # TODO(knn): Update this to support i18n. | |
1052 def WriteString(key, value): | |
1053 f.write(' <string name="%s">%s</string>\n' | |
1054 % (key, _EscapeResourceString(value))) | |
1055 | |
1056 def WriteItems(key, items): | |
1057 if items: | |
1058 f.write(' <string-array name="%sEntries">\n' % key) | |
1059 for item in items: | |
1060 f.write(' <item>%s</item>\n' % | |
1061 _EscapeResourceString(item.caption)) | |
1062 f.write(' </string-array>\n') | |
1063 f.write(' <string-array name="%sValues">\n' % key) | |
1064 for item in items: | |
1065 f.write(' <item>%s</item>\n' % _EscapeResourceString(item.value)) | |
1066 f.write(' </string-array>\n') | |
1067 | |
1068 def WriteResourceForPolicy(policy): | |
1069 policy_name = policy.name | |
1070 WriteString(policy_name + 'Title', policy.caption) | |
1071 | |
1072 # Get the first line of the policy description. | |
1073 description = policy.desc.split('\n', 1)[0] | |
1074 WriteString(policy_name + 'Desc', description) | |
1075 | |
1076 if policy.has_restriction_resources: | |
1077 WriteItems(policy_name, policy.items) | |
1078 | |
1079 # _WriteResourcesForPolicies body | |
1080 f.write('<resources>\n\n') | |
1081 for policy in policies: | |
1082 if policy.is_supported and policy.restriction_type != 'invalid': | |
1083 WriteResourceForPolicy(policy) | |
1084 f.write('</resources>') | |
1085 | |
1086 | |
1087 if __name__ == '__main__': | 1033 if __name__ == '__main__': |
1088 sys.exit(main()) | 1034 sys.exit(main()) |
OLD | NEW |