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.
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
)
23 from json_comment_eater
import Nom
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
),
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
)
48 parser
= optparse
.OptionParser(description
=__doc__
)
49 parser
.usage
= '%prog [options] <template_manifest_path>'
51 '-o', '--output_manifest', action
='store', metavar
='OUTPUT_MANIFEST',
52 help='File to place generated manifest')
54 '--is_guest_manifest', default
='0', action
='store', metavar
='NUM',
55 help='Whether to generate a guest mode capable manifest')
57 '--is_chromevox_classic', default
='0', action
='store', metavar
='NUM',
58 help='Whether to generate a ChromeVox Classic manifest')
60 '--is_js_compressed', default
='1', action
='store', metavar
='NUM',
61 help='Whether compressed JavaScript files are used')
63 '--set_version', action
='store', metavar
='SET_VERSION',
64 help='Set the extension version')
66 '--key', action
='store', metavar
='KEY',
67 help='Set the extension key')
69 '--version_file', action
='store', metavar
='NAME',
70 help='File with version information')
72 options
, args
= parser
.parse_args()
74 print >>sys
.stderr
, 'Expected exactly one argument'
76 if options
.output_manifest
is None:
77 print >>sys
.stderr
, '--output_manifest option must be specified'
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 ' +
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 ' +
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__':