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