1#
2# Copyright 2016 - The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import argparse
18import fileinput
19import os
20import os.path
21import re
22import pprint
23
24# Parses the output of make install --dry-run and generates directives in the
25# form
26#
27#   install['target'] = [ 'srcfile' ]
28#
29# This output is then fed into gen_android_mk which generates Android.mk.
30#
31# This process is split into two steps so the second step can later be replaced
32# with an Android.bp generator.
33
34
35class MakeInstallParser(object):
36    '''Parses the output of make install --dry-run.'''
37
38    def __init__(self, ltp_root):
39        self.ltp_root = ltp_root
40
41    def ParseFile(self, input_path):
42        '''Parses the text output of make install --dry-run.
43
44        Args:
45            input_text: string, output of make install --dry-run
46
47        Returns:
48            string, directives in form install['target'] = [ 'srcfile' ]
49        '''
50        pattern = re.compile(r'install -m \d+ "%s%s(.*)" "/opt/ltp/(.*)"' %
51                             (os.path.realpath(self.ltp_root), os.sep))
52        result = []
53
54        with open(input_path, 'r') as f:
55            for line in f:
56                line = line.strip()
57
58                m = pattern.match(line)
59                if not m:
60                    continue
61
62                src, target = m.groups()
63                # If the file isn't in the source tree, it's not a prebuilt
64                if not os.path.isfile(
65                        os.path.realpath(self.ltp_root) + os.sep + src):
66                    continue
67
68                result.append("install['%s'] = ['%s']" % (target, src))
69
70        return result
71
72def main():
73    arg_parser = argparse.ArgumentParser(
74        description='Parse the LTP make install --dry-run output into a list')
75    arg_parser.add_argument(
76        '--ltp-root',
77        dest='ltp_root',
78        required=True,
79        help='LTP Root dir')
80    arg_parser.add_argument(
81        '--dry-run-file',
82        dest='input_path',
83        required=True,
84        help='Path to LTP make install --dry-run output file')
85    args = arg_parser.parse_args()
86
87    make_install_parser = MakeInstallParser(args.ltp_root)
88    result = make_install_parser.ParseFile(args.input_path)
89
90    print pprint.pprint(result)
91
92if __name__ == '__main__':
93    main()