Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / chromevox / tools / generate_manifest.py
blobc3a2a4c070d6ce75ecae5f95e59e43b8e3354569
1 #!/usr/bin/env python
3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 import json
8 import io
9 import optparse
10 import os
11 import sys
13 jinja2_path = os.path.normpath(os.path.join(os.path.abspath(__file__),
14 *[os.path.pardir] * 7 + ['third_party']))
15 nom_path = os.path.normpath(os.path.join(os.path.abspath(__file__),
16 *[os.path.pardir] * 7 + ['tools/json_comment_eater']))
17 version_py_path = os.path.normpath(os.path.join(os.path.abspath(__file__),
18 *[os.path.pardir] * 7 + ['build/util']))
19 sys.path.insert(0, jinja2_path)
20 sys.path.insert(0, nom_path)
21 sys.path.insert(0, version_py_path)
22 import jinja2
23 from json_comment_eater import Nom
24 import version
27 '''Generate an extension manifest based on a template.'''
29 def getChromeVersion(version_file):
30 values = version.fetch_values([version_file])
31 return version.subst_template('@MAJOR@.@MINOR@.@BUILD@.@PATCH@', values)
34 def processJinjaTemplate(input_file, output_file, context):
35 (template_path, template_name) = os.path.split(input_file)
36 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path),
37 trim_blocks=True)
38 template = env.get_template(template_name)
39 rendered = template.render(context)
40 rendered_without_comments = Nom(rendered)
41 # Simply for validation.
42 json.loads(rendered_without_comments)
43 with io.open(output_file, 'w', encoding='utf-8') as manifest_file:
44 manifest_file.write(rendered_without_comments)
47 def main():
48 parser = optparse.OptionParser(description=__doc__)
49 parser.usage = '%prog [options] <template_manifest_path>'
50 parser.add_option(
51 '-o', '--output_manifest', action='store', metavar='OUTPUT_MANIFEST',
52 help='File to place generated manifest')
53 parser.add_option(
54 '--is_guest_manifest', default='0', action='store', metavar='NUM',
55 help='Whether to generate a guest mode capable manifest')
56 parser.add_option(
57 '--is_chromevox_classic', default='0', action='store', metavar='NUM',
58 help='Whether to generate a ChromeVox Classic manifest')
59 parser.add_option(
60 '--is_js_compressed', default='1', action='store', metavar='NUM',
61 help='Whether compressed JavaScript files are used')
62 parser.add_option(
63 '--set_version', action='store', metavar='SET_VERSION',
64 help='Set the extension version')
65 parser.add_option(
66 '--key', action='store', metavar='KEY',
67 help='Set the extension key')
68 parser.add_option(
69 '--version_file', action='store', metavar='NAME',
70 help='File with version information')
72 options, args = parser.parse_args()
73 if len(args) != 1:
74 print >>sys.stderr, 'Expected exactly one argument'
75 sys.exit(1)
76 if options.output_manifest is None:
77 print >>sys.stderr, '--output_manifest option must be specified'
78 sys.exit(1)
79 if options.set_version is not None and options.version_file is not None:
80 print >>sys.stderr, ('only one of --set_version and --version_file may ' +
81 'be specified')
82 if options.set_version is None and options.version_file is None:
83 print >>sys.stderr, ('one of --set_version or --version_file option ' +
84 'must be specified')
85 sys.exit(1)
86 context = {k: v for k, v in parser.values.__dict__.items() if v is not None}
87 if options.version_file is not None:
88 context['set_version'] = getChromeVersion(options.version_file)
89 processJinjaTemplate(args[0], options.output_manifest, context)
91 if __name__ == '__main__':
92 main()