| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The Chromium Authors. All rights reserved. | 2 # Copyright 2017 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 '''Takes translated policy_template.json files as input, applies template | 6 '''Takes translated policy_template.json files as input, applies template |
| 7 writers and emits various template and doc files (admx, html, json etc.). | 7 writers and emits various template and doc files (admx, html, json etc.). |
| 8 ''' | 8 ''' |
| 9 | 9 |
| 10 import codecs | 10 import codecs |
| 11 import collections | 11 import collections |
| 12 import optparse | 12 import optparse |
| 13 import os | 13 import os |
| 14 import sys | 14 import sys |
| 15 | 15 |
| 16 import writer_configuration | 16 import writer_configuration |
| 17 import policy_template_generator | 17 import policy_template_generator |
| 18 | 18 |
| 19 from writers import adm_writer, adml_writer, admx_writer, \ | 19 from writers import adm_writer, adml_writer, admx_writer, \ |
| 20 chromeos_admx_writer, chromeos_adml_writer, \ |
| 20 google_admx_writer, google_adml_writer, \ | 21 google_admx_writer, google_adml_writer, \ |
| 21 android_policy_writer, reg_writer, doc_writer, \ | 22 android_policy_writer, reg_writer, doc_writer, \ |
| 22 json_writer, plist_writer, plist_strings_writer | 23 json_writer, plist_writer, plist_strings_writer |
| 23 | 24 |
| 24 def MacLanguageMap(lang): | 25 def MacLanguageMap(lang): |
| 25 '''Handles slightly different path naming convention for Macs: | 26 '''Handles slightly different path naming convention for Macs: |
| 26 - 'en-US' -> 'en' | 27 - 'en-US' -> 'en' |
| 27 - '-' -> '_' | 28 - '-' -> '_' |
| 28 | 29 |
| 29 Args: | 30 Args: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 43 WriterDesc = collections.namedtuple( | 44 WriterDesc = collections.namedtuple( |
| 44 'WriterDesc', ['type', 'is_per_language', 'encoding', 'language_map']) | 45 'WriterDesc', ['type', 'is_per_language', 'encoding', 'language_map']) |
| 45 | 46 |
| 46 | 47 |
| 47 _WRITER_DESCS = [ | 48 _WRITER_DESCS = [ |
| 48 WriterDesc('adm', True, 'utf-16', None), | 49 WriterDesc('adm', True, 'utf-16', None), |
| 49 WriterDesc('adml', True, 'utf-16', None), | 50 WriterDesc('adml', True, 'utf-16', None), |
| 50 WriterDesc('admx', False, 'utf-16', None), | 51 WriterDesc('admx', False, 'utf-16', None), |
| 51 WriterDesc('google_adml', True, 'utf-8', None), | 52 WriterDesc('google_adml', True, 'utf-8', None), |
| 52 WriterDesc('google_admx', False, 'utf-8', None), | 53 WriterDesc('google_admx', False, 'utf-8', None), |
| 54 WriterDesc('chromeos_adml', True, 'utf-8', None), |
| 55 WriterDesc('chromeos_admx', False, 'utf-8', None), |
| 53 WriterDesc('android_policy', False, 'utf-8', None), | 56 WriterDesc('android_policy', False, 'utf-8', None), |
| 54 WriterDesc('reg', False, 'utf-16', None), | 57 WriterDesc('reg', False, 'utf-16', None), |
| 55 WriterDesc('doc', True, 'utf-8', None), | 58 WriterDesc('doc', True, 'utf-8', None), |
| 56 WriterDesc('json', False, 'utf-8', None), | 59 WriterDesc('json', False, 'utf-8', None), |
| 57 WriterDesc('plist', False, 'utf-8', None), | 60 WriterDesc('plist', False, 'utf-8', None), |
| 58 WriterDesc('plist_strings', True, 'utf-8', MacLanguageMap) | 61 WriterDesc('plist_strings', True, 'utf-8', MacLanguageMap) |
| 59 ] | 62 ] |
| 60 | 63 |
| 61 | 64 |
| 62 # Template writers that are not per-language use policy_templates.json from | 65 # Template writers that are not per-language use policy_templates.json from |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 admx, google_admx, android_policy, reg, json, plist: Absolute path of the | 113 admx, google_admx, android_policy, reg, json, plist: Absolute path of the |
| 111 corresponding file types. Must NOT contain a ${lang} placeholder. There | 114 corresponding file types. Must NOT contain a ${lang} placeholder. There |
| 112 is only one output file, not one per language. | 115 is only one output file, not one per language. |
| 113 D: List of grit defines, used to assemble writer configuration. | 116 D: List of grit defines, used to assemble writer configuration. |
| 114 E, t: Grit environment variables and target platform. Unused, but | 117 E, t: Grit environment variables and target platform. Unused, but |
| 115 grit_rule.gni adds them, so OptionParser must handle them. | 118 grit_rule.gni adds them, so OptionParser must handle them. |
| 116 ''' | 119 ''' |
| 117 parser = optparse.OptionParser() | 120 parser = optparse.OptionParser() |
| 118 parser.add_option('--translations', dest='translations') | 121 parser.add_option('--translations', dest='translations') |
| 119 parser.add_option('--languages', dest='languages') | 122 parser.add_option('--languages', dest='languages') |
| 120 parser.add_option('--adm', dest='adm') | 123 parser.add_option('--adm', action='append', dest='adm') |
| 121 parser.add_option('--adml', dest='adml') | 124 parser.add_option('--adml', action='append', dest='adml') |
| 122 parser.add_option('--admx', dest='admx') | 125 parser.add_option('--admx', action='append', dest='admx') |
| 123 parser.add_option('--google_adml', dest='google_adml') | 126 parser.add_option('--chromeos_adml', action='append', dest='chromeos_adml') |
| 124 parser.add_option('--google_admx', dest='google_admx') | 127 parser.add_option('--chromeos_admx', action='append', dest='chromeos_admx') |
| 125 parser.add_option('--reg', dest='reg') | 128 parser.add_option('--google_adml', action='append', dest='google_adml') |
| 126 parser.add_option('--doc', dest='doc') | 129 parser.add_option('--google_admx', action='append', dest='google_admx') |
| 127 parser.add_option('--json', dest='json') | 130 parser.add_option('--reg', action='append', dest='reg') |
| 128 parser.add_option('--plist', dest='plist') | 131 parser.add_option('--doc', action='append', dest='doc') |
| 129 parser.add_option('--plist_strings', dest='plist_strings') | 132 parser.add_option('--json', action='append', dest='json') |
| 130 parser.add_option('--android_policy', dest='android_policy') | 133 parser.add_option('--plist', action='append', dest='plist') |
| 131 parser.add_option('-D', action='append', dest='grit_defines', default=[]) | 134 parser.add_option('--plist_strings', action='append', dest='plist_strings') |
| 132 parser.add_option('-E', action='append', dest='grit_build_env', default=[]) | 135 parser.add_option('--android_policy', action='append', dest='android_policy') |
| 133 parser.add_option('-t', action='append', dest='grit_target', default=[]) | 136 parser.add_option('-D', action='append', dest='grit_defines') |
| 137 parser.add_option('-E', action='append', dest='grit_build_env') |
| 138 parser.add_option('-t', action='append', dest='grit_target') |
| 134 options, args = parser.parse_args(argv[1:]) | 139 options, args = parser.parse_args(argv[1:]) |
| 135 | 140 |
| 136 _LANG_PLACEHOLDER = "${lang}"; | 141 _LANG_PLACEHOLDER = "${lang}"; |
| 137 assert _LANG_PLACEHOLDER in options.translations | 142 assert _LANG_PLACEHOLDER in options.translations |
| 138 | 143 |
| 139 languages = filter(bool, options.languages.split(',')) | 144 languages = filter(bool, options.languages.split(',')) |
| 140 assert _DEFAULT_LANGUAGE in languages | 145 assert _DEFAULT_LANGUAGE in languages |
| 141 | 146 |
| 142 config = _GetWriterConfiguration(options.grit_defines); | 147 config = _GetWriterConfiguration(options.grit_defines); |
| 143 | 148 |
| 144 # For each language, load policy data once and run all writers on it. | 149 # For each language, load policy data once and run all writers on it. |
| 145 for lang in languages: | 150 for lang in languages: |
| 146 # Load the policy data. | 151 # Load the policy data. |
| 147 policy_templates_json_path = \ | 152 policy_templates_json_path = \ |
| 148 options.translations.replace(_LANG_PLACEHOLDER, lang) | 153 options.translations.replace(_LANG_PLACEHOLDER, lang) |
| 149 with codecs.open(policy_templates_json_path, 'r', 'utf-16') as policy_file: | 154 with codecs.open(policy_templates_json_path, 'r', 'utf-16') as policy_file: |
| 150 policy_data = eval(policy_file.read()) | 155 policy_data = eval(policy_file.read()) |
| 151 | 156 |
| 152 # Preprocess the policy data. | 157 # Preprocess the policy data. |
| 153 policy_generator = \ | 158 policy_generator = \ |
| 154 policy_template_generator.PolicyTemplateGenerator(config, policy_data) | 159 policy_template_generator.PolicyTemplateGenerator(config, policy_data) |
| 155 | 160 |
| 156 for writer_desc in _WRITER_DESCS: | 161 for writer_desc in _WRITER_DESCS: |
| 157 # For writer types that are not per language (e.g. admx), only do it once. | 162 # For writer types that are not per language (e.g. admx), only do it once. |
| 158 if (not writer_desc.is_per_language and lang != _DEFAULT_LANGUAGE): | 163 if (not writer_desc.is_per_language and lang != _DEFAULT_LANGUAGE): |
| 159 continue | 164 continue |
| 160 | 165 |
| 161 # Was the current writer type passed as argument, e.g. --admx <path>? | 166 # Was the current writer type passed as argument, e.g. --admx <path>? |
| 162 output_path = getattr(options, writer_desc.type, '') | 167 # Note that all paths are arrays and we loop over all of them. |
| 163 if (not output_path): | 168 output_paths = getattr(options, writer_desc.type, '') |
| 169 if (not output_paths): |
| 164 continue | 170 continue |
| 171 for output_path in output_paths: |
| 172 # Substitute language placeholder in output file. |
| 173 if (writer_desc.is_per_language): |
| 174 assert _LANG_PLACEHOLDER in output_path |
| 175 mapped_lang = writer_desc.language_map(lang) \ |
| 176 if writer_desc.language_map else lang |
| 177 output_path = output_path.replace(_LANG_PLACEHOLDER, mapped_lang) |
| 178 else: |
| 179 assert _LANG_PLACEHOLDER not in output_path |
| 165 | 180 |
| 166 # Substitute language placeholder in output file. | 181 # Run the template writer on th policy data. |
| 167 if (writer_desc.is_per_language): | 182 writer = GetWriter(writer_desc.type, config) |
| 168 assert _LANG_PLACEHOLDER in output_path | 183 output_data = policy_generator.GetTemplateText(writer) |
| 169 mapped_lang = \ | |
| 170 writer_desc.language_map(lang) if writer_desc.language_map else lang | |
| 171 output_path = output_path.replace(_LANG_PLACEHOLDER, mapped_lang) | |
| 172 else: | |
| 173 assert _LANG_PLACEHOLDER not in output_path | |
| 174 | 184 |
| 175 # Run the template writer on th policy data. | 185 # Make output directory if it doesn't exist yet. |
| 176 writer = GetWriter(writer_desc.type, config) | 186 output_dir = os.path.split(output_path)[0] |
| 177 output_data = policy_generator.GetTemplateText(writer) | 187 if not os.path.exists(output_dir): |
| 188 os.makedirs(output_dir) |
| 178 | 189 |
| 179 # Make output directory if it doesn't exist yet. | 190 # Write output file. |
| 180 output_dir = os.path.split(output_path)[0] | 191 with codecs.open(output_path, 'w', writer_desc.encoding) as output_file: |
| 181 if not os.path.exists(output_dir): | 192 output_file.write(output_data) |
| 182 os.makedirs(output_dir) | |
| 183 | |
| 184 # Write output file. | |
| 185 with codecs.open(output_path, 'w', writer_desc.encoding) as output_file: | |
| 186 output_file.write(output_data) | |
| 187 | 193 |
| 188 | 194 |
| 189 if '__main__' == __name__: | 195 if '__main__' == __name__: |
| 190 sys.exit(main(sys.argv)) | 196 sys.exit(main(sys.argv)) |
| OLD | NEW |