Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / build / android / gyp / generate_resource_rewriter.py
blob2c892b6cc8d992a28183658423f16a24a69f431d
1 #!/usr/bin/env python
3 # Copyright (c) 2015 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 """Generate ResourceRewriter.java which overwrites the given package's
8 resource id.
9 """
11 import argparse
12 import os
13 import sys
15 from util import build_utils
17 # Import jinja2 from third_party/jinja2
18 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
19 '..',
20 '..',
21 '..',
22 'third_party')))
23 import jinja2
25 RESOURCE_REWRITER="""/* AUTO-GENERATED FILE. DO NOT MODIFY. */
27 package {{ package }};
28 /**
29 * Helper class used to fix up resource ids.
31 class ResourceRewriter {
32 /**
33 * Rewrite the R 'constants' for the WebView.
35 public static void rewriteRValues(final int packageId) {
36 {% for res_package in res_packages %}
37 {{ res_package }}.R.onResourcesLoaded(packageId);
38 {% endfor %}
41 """
43 def ParseArgs(args):
44 """Parses command line options.
46 Returns:
47 An Namespace from argparse.parse_args()
48 """
49 parser = argparse.ArgumentParser(prog='generate_resource_rewriter')
51 parser.add_argument('--package-name',
52 required=True,
53 help='The package name of ResourceRewriter.')
54 parser.add_argument('--dep-packages',
55 required=True,
56 help='A list of packages whose resource id will be'
57 'overwritten in ResourceRewriter.')
58 parser.add_argument('--output-dir',
59 required=True,
60 help='A output directory of generated'
61 ' ResourceRewriter.java')
63 return parser.parse_args(args)
66 def CreateResourceRewriter(package, res_packages, output_dir):
67 output_dir = os.path.join(output_dir, *package.split('.'))
68 build_utils.MakeDirectory(output_dir)
69 java_path = os.path.join(output_dir, 'ResourceRewriter.java')
70 template = jinja2.Template(RESOURCE_REWRITER,
71 trim_blocks=True,
72 lstrip_blocks=True)
73 output = template.render(package=package, res_packages=res_packages)
74 with open(java_path, 'w') as f:
75 f.write(output)
78 def main():
79 options = ParseArgs(sys.argv[1:])
81 CreateResourceRewriter(
82 options.package_name,
83 build_utils.ParseGypList(options.dep_packages),
84 options.output_dir)
85 return 0
87 if __name__ == '__main__':
88 sys.exit(main())