1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 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 16import sys 17import os 18import argparse 19import shutil 20 21sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 22from scripts.util.file_utils import read_json_file # noqa: E402 23 24 25def _get_toolchain_name(toolchain_label): 26 return toolchain_label.split(':')[1] 27 28 29def _remove_unstripped_dir(toolchain_info_file): 30 data = read_json_file(toolchain_info_file) 31 if data is None: 32 raise Exception("read file '{}' failed.".format(toolchain_info_file)) 33 platform_toolchain = data.get('platform_toolchain') 34 base_dir_list = [] 35 for key, val in platform_toolchain.items(): 36 if key == 'phone': 37 base_dir_list.append('.') 38 else: 39 toolchain_name = val.split(':')[1] 40 base_dir_list.append(toolchain_name) 41 dir_list = ['lib.unstripped', 'exe.unstripped'] 42 for _base_dir in base_dir_list: 43 for _dir_name in dir_list: 44 _dir = os.path.join(_base_dir, _dir_name) 45 if os.path.exists(_dir): 46 shutil.rmtree(_dir) 47 48 49def main(): 50 parser = argparse.ArgumentParser() 51 parser.add_argument('--backup-dir', required=True) 52 parser.add_argument('--backup-dest-dir', required=True) 53 parser.add_argument('--asan-clean', dest='asan_clean', action='store_true') 54 parser.set_defaults(asan_clean=False) 55 parser.add_argument('--removed-dir-list', nargs='*', default=[]) 56 parser.add_argument('--platforms-toolchain-info-file') 57 args = parser.parse_args() 58 59 if not os.path.exists(args.backup_dir): 60 return 0 61 62 if os.path.exists(args.backup_dest_dir): 63 shutil.rmtree(args.backup_dest_dir) 64 os.makedirs(args.backup_dest_dir, exist_ok=True) 65 66 dir_name = os.path.basename(args.backup_dir) 67 _dest_dir = os.path.join(args.backup_dest_dir, dir_name) 68 shutil.copytree(args.backup_dir, _dest_dir) 69 70 if args.asan_clean: 71 if args.removed_dir_list: 72 for _dir in args.removed_dir_list: 73 if os.path.exists(_dir): 74 shutil.rmtree(_dir) 75 if args.platforms_toolchain_info_file: 76 _remove_unstripped_dir(args.platforms_toolchain_info_file) 77 return 0 78 79 80if __name__ == '__main__': 81 sys.exit(main()) 82