1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2020 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import argparse 20import os 21import shutil 22import sys 23from utils import makedirs 24 25 26def copytree(src, dst, symlinks=False, ignore=None): 27 for item in os.listdir(src): 28 if '.git' in item or '.repo' in item: 29 continue 30 src_path = os.path.join(src, item) 31 dst_path = os.path.join(dst, item) 32 if os.path.isdir(src_path): 33 try: 34 shutil.copytree(src_path, dst_path, symlinks) 35 except Exception as e: 36 print('Warning :{}\nIgnore invalid symlink!'.format( 37 str(e.args[0]))) 38 else: 39 shutil.copy2(src_path, dst_path) 40 41 42def main(): 43 parser = argparse.ArgumentParser( 44 description='A common directory and file copy.') 45 parser.add_argument( 46 '--src_type', 47 help='src type to copy.') 48 parser.add_argument( 49 '--src', 50 help='List the sources to copy.', 51 required=True) 52 parser.add_argument( 53 '--dest_dir', 54 help='Path that the sources should be copied to.', 55 required=True) 56 57 args = parser.parse_args() 58 59 out_dir = args.dest_dir 60 if not os.path.exists(out_dir): 61 makedirs(out_dir) 62 63 if args.src_type == "file" or os.path.isfile(args.src): 64 shutil.copy(args.src, out_dir) 65 return 0 66 67 source_dir = args.src 68 assert os.path.exists(source_dir) 69 70 copytree(source_dir, out_dir) 71 72 return 0 73 74 75if __name__ == '__main__': 76 sys.exit(main()) 77