1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2023 Huawei Device Co., Ltd.
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 sys
18import os
19import argparse
20import subprocess
21import shutil
22
23
24def execute_adlt_command(args):
25    cmd = [
26        args.adlt_exe,
27        f'--root-dir={args.adlt_root_dir}',
28        '-o',
29        args.output_file,
30        args.allowed_lib_list,
31    ]
32    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
33    stdout, stderr = proc.communicate()
34    for line in stdout.splitlines():
35        print(f'[1/1] info: {line}')
36    for line in stderr.splitlines():
37        print(f'[1/1] warning: {line}')
38    if proc.returncode:
39        raise Exception(f'ReturnCode:{proc.returncode}. excute failed: {stderr}')
40
41
42def remove_origin_lib(args):
43    with open(args.allowed_lib_list, 'r') as f:
44        lines = f.readlines()
45    for line in lines:
46        so_abspath = os.path.join(args.adlt_root_dir, line.strip("\n"))
47        if os.path.exists(so_abspath):
48            os.remove(so_abspath)
49
50
51def main(argv):
52    parser = argparse.ArgumentParser()
53    parser.add_argument('--adlt-exe', required=True, help='adlt exe path')
54    parser.add_argument('--adlt-root-dir', required=True, help='adlt root dir')
55    parser.add_argument('--allowed-lib-list', required=True, help='allowed lib list')
56    parser.add_argument('--output-file', required=True, help='output file')
57    args = parser.parse_args(argv)
58    if not os.path.exists(args.output_file):
59        execute_adlt_command(args)
60        remove_origin_lib(args)
61
62
63if __name__ == '__main__':
64    sys.exit(main(sys.argv[1:]))