generate_manifest.py revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1#!/usr/bin/env python 2 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. 6 7import json 8import io 9import optparse 10import os 11import sys 12 13jinja2_path = os.path.normpath(os.path.join(os.path.abspath(__file__), 14 *[os.path.pardir] * 7 + ['third_party'])) 15nom_path = os.path.normpath(os.path.join(os.path.abspath(__file__), 16 *[os.path.pardir] * 7 + ['tools/json_comment_eater'])) 17sys.path.insert(0, jinja2_path) 18sys.path.insert(0, nom_path) 19import jinja2 20from json_comment_eater import Nom 21 22 23'''Generate an extension manifest based on a template.''' 24 25 26def processJinjaTemplate(input_file, output_file, context): 27 (template_path, template_name) = os.path.split(input_file) 28 env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path)) 29 template = env.get_template(template_name) 30 rendered = template.render(context) 31 rendered_without_comments = Nom(rendered) 32 # Simply for validation. 33 json.loads(rendered_without_comments) 34 with io.open(output_file, 'w', encoding='utf-8') as manifest_file: 35 manifest_file.write(rendered_without_comments) 36 37 38def main(): 39 parser = optparse.OptionParser(description=__doc__) 40 parser.usage = '%prog [options] <template_manifest_path>' 41 parser.add_option( 42 '-o', '--output_manifest', action='store', metavar='OUTPUT_MANIFEST', 43 help='File to place generated manifest') 44 parser.add_option( 45 '-g', '--is_guest_manifest', action='store', metavar='GUEST_MANIFEST', 46 help='Generate a guest mode capable manifest') 47 48 options, args = parser.parse_args() 49 if len(args) != 1: 50 print >>sys.stderr, 'Expected exactly one argument' 51 sys.exit(1) 52 processJinjaTemplate(args[0], options.output_manifest, parser.values.__dict__) 53 54if __name__ == '__main__': 55 main() 56