Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / xwalk / build / android / merge_java_srcs.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 import shutil
12
13
14 def DoCopy(path, target_path):
15   if os.path.isfile(path):
16     package = ''
17     package_re = re.compile(
18         '^package (?P<package>([a-zA-Z0-9_]+.)*[a-zA-Z0-9_]+);$')
19     for line in open(path).readlines():
20       match = package_re.match(line)
21       if match:
22         package = match.group('package')
23         break
24     sub_path = os.path.sep.join(package.split('.'))
25     shutil.copy(path, os.path.join(target_path, sub_path))
26     return
27
28   for dirpath, _, files in os.walk(path):
29     if not files:
30       continue
31     sub_path = os.path.relpath(dirpath, path)
32     target_dirpath = os.path.join(target_path, sub_path)
33     if not os.path.isdir(target_dirpath):
34       os.makedirs(target_dirpath)
35     for f in files:
36       fpath = os.path.join(dirpath, f)
37       # "interface type;" is invalid for normal android project,
38       # It's only for chromium's build system, ignore these aidl files.
39       if f.endswith('.aidl'):
40         invalid_lines = []
41         for line in open(fpath).readlines():
42           if re.match('^interface .*;$', line):
43             invalid_lines.append(line)
44         if invalid_lines:
45           continue
46       elif not f.endswith('.java'):
47         continue
48       shutil.copy(fpath, target_dirpath)
49
50
51 def main():
52   parser = optparse.OptionParser()
53   info = ('The java source dirs to merge.')
54   parser.add_option('--dirs', help=info)
55   info = ('The target to place all the sources.')
56   parser.add_option('--target-path', help=info)
57   options, _ = parser.parse_args()
58
59   if os.path.isdir(options.target_path):
60     shutil.rmtree(options.target_path)
61   os.makedirs(options.target_path)
62
63   for path in options.dirs.split(' '):
64     if path.startswith('"') and path.endswith('"'):
65       path = eval(path)
66     DoCopy(path, options.target_path)
67
68
69 if __name__ == '__main__':
70   sys.exit(main())