Upstream version 10.39.226.0
[platform/framework/web/crosswalk.git] / src / xwalk / build / android / generate_resource_map.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Intel Corporation. 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
7 import optparse
8 import os
9 import re
10 import sys
11
12
13 def CreateResourceMap(r_java, res_map):
14   package_regex = re.compile('^package ([a-zA-Z0-9_\.]*);$')
15   package = ''
16   output_content = []
17   for line in open(r_java, 'r').readlines():
18     if not package:
19       package_match = package_regex.match(line)
20       if package_match:
21         package = package_match.group(1)
22     output_content.append(re.sub(r'\s*=\s*0x[0-9a-f]{8};', ';', line))
23   output_path = os.path.join(res_map, os.path.sep.join(package.split('.')))
24   if not os.path.isdir(output_path):
25     os.makedirs(output_path)
26   with open(os.path.join(output_path, 'R.java'), 'w') as output:
27     output.write(''.join(output_content))
28
29
30 def main():
31   parser = optparse.OptionParser()
32   info = ('The folder contains generated R.java')
33   parser.add_option('--gen-dir', help=info)
34   info = ('The folder to place resource maps')
35   parser.add_option('--resource-map-dir', help=info)
36   options, _ = parser.parse_args()
37
38   if not os.path.isdir(options.gen_dir):
39     return 1
40   for root, _, files in os.walk(options.gen_dir):
41     if 'R.java' in files:
42       r_java = os.path.join(root, 'R.java')
43       CreateResourceMap(r_java, options.resource_map_dir)
44
45
46 if __name__ == '__main__':
47   sys.exit(main())