• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: str, dst: str, symlinks: bool=False, ignore=None):
27    src = os.path.normpath(src)
28    for item in os.listdir(src):
29        if '.git' in item or '.repo' in item:
30            continue
31        src_path = os.path.join(src, item)
32        dst_path = os.path.join(dst, item)
33        if os.path.isdir(src_path):
34            try:
35                shutil.copytree(src_path, dst_path, symlinks)
36            except Exception as e:
37                print('Warning :{}\nIgnore invalid symlink!'.format(
38                    str(e.args[0])))
39        else:
40            shutil.copy2(src_path, dst_path)
41
42
43def main():
44    parser = argparse.ArgumentParser(
45        description='A common directory and file copy.')
46    parser.add_argument(
47        '--src_type',
48        help='src type to copy.')
49    parser.add_argument(
50        '--src',
51        help='List the sources to copy.',
52        required=True)
53    parser.add_argument(
54        '--dest_dir',
55        help='Path that the sources should be copied to.',
56        required=True)
57
58    args = parser.parse_args()
59
60    out_dir = args.dest_dir
61    if not os.path.exists(out_dir):
62        makedirs(out_dir)
63
64    if args.src_type == "file" or os.path.isfile(args.src):
65        shutil.copy(args.src, out_dir)
66        return 0
67
68    source_dir = args.src
69    if not os.path.exists(source_dir):
70        raise Exception(f'{source_dir} is not exist!')
71    copytree(source_dir, out_dir)
72
73    return 0
74
75
76if __name__ == '__main__':
77    sys.exit(main())
78