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