1f6603c60Sopenharmony_ci#!/usr/bin/env python
2f6603c60Sopenharmony_ci# -*- coding: utf-8 -*-
3f6603c60Sopenharmony_ci"""
4f6603c60Sopenharmony_ciCopyright (c) 2020-2021 Huawei Device Co., Ltd.
5f6603c60Sopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
6f6603c60Sopenharmony_ciyou may not use this file except in compliance with the License.
7f6603c60Sopenharmony_ciYou may obtain a copy of the License at
8f6603c60Sopenharmony_ci
9f6603c60Sopenharmony_ci  http://www.apache.org/licenses/LICENSE-2.0
10f6603c60Sopenharmony_ci
11f6603c60Sopenharmony_ciUnless required by applicable law or agreed to in writing, software
12f6603c60Sopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
13f6603c60Sopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14f6603c60Sopenharmony_ciSee the License for the specific language governing permissions and
15f6603c60Sopenharmony_cilimitations under the License.
16f6603c60Sopenharmony_ci"""
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ciimport os
19f6603c60Sopenharmony_ciimport argparse
20f6603c60Sopenharmony_ciimport tarfile
21f6603c60Sopenharmony_ciimport shutil
22f6603c60Sopenharmony_ci
23f6603c60Sopenharmony_cicopy_file_counts = 0
24f6603c60Sopenharmony_ci
25f6603c60Sopenharmony_ci
26f6603c60Sopenharmony_cidef copy_files(sourcedir, targetdir):
27f6603c60Sopenharmony_ci    global copy_file_counts
28f6603c60Sopenharmony_ci    for f in os.listdir(sourcedir):
29f6603c60Sopenharmony_ci        source_f = os.path.join(sourcedir, f)
30f6603c60Sopenharmony_ci        target_f = os.path.join(targetdir, f)
31f6603c60Sopenharmony_ci        if not os.path.isfile(source_f):
32f6603c60Sopenharmony_ci            if os.path.isdir(source_f):
33f6603c60Sopenharmony_ci                copy_files(source_f, target_f)
34f6603c60Sopenharmony_ci            continue
35f6603c60Sopenharmony_ci        if f.endswith('.rsp'):
36f6603c60Sopenharmony_ci            continue
37f6603c60Sopenharmony_ci        if os.path.exists(targetdir):
38f6603c60Sopenharmony_ci            copy_file_counts += 1
39f6603c60Sopenharmony_ci            with open(source_f, "rb") as fp1:
40f6603c60Sopenharmony_ci                with open(target_f, "wb") as fp:
41f6603c60Sopenharmony_ci                    fp.write(fp1.read())
42f6603c60Sopenharmony_ci        elif not os.path.exists(targetdir):
43f6603c60Sopenharmony_ci            os.makedirs(targetdir)
44f6603c60Sopenharmony_ci            copy_file_counts += 1
45f6603c60Sopenharmony_ci            with open(source_f, "rb") as fp1:
46f6603c60Sopenharmony_ci                with open(target_f, "wb") as fp:
47f6603c60Sopenharmony_ci                    fp.write(fp1.read())
48f6603c60Sopenharmony_ci
49f6603c60Sopenharmony_ci
50f6603c60Sopenharmony_cidef make_targz_one_by_one(output_filename, source_dir):
51f6603c60Sopenharmony_ci    tar = tarfile.open(output_filename, "w")
52f6603c60Sopenharmony_ci    for root, dirs, files in os.walk(source_dir):
53f6603c60Sopenharmony_ci        for file in files:
54f6603c60Sopenharmony_ci            pathfile = os.path.join(root, file)
55f6603c60Sopenharmony_ci            tar.add(pathfile)
56f6603c60Sopenharmony_ci    tar.close()
57f6603c60Sopenharmony_ci
58f6603c60Sopenharmony_ci
59f6603c60Sopenharmony_ciif __name__ == "__main__":
60f6603c60Sopenharmony_ci    parser = argparse.ArgumentParser(description='manual to this script')
61f6603c60Sopenharmony_ci    parser.add_argument("--input_path", type=str, default="0")
62f6603c60Sopenharmony_ci    parser.add_argument("--output_path", type=str, default="0")
63f6603c60Sopenharmony_ci    parser.add_argument("--temp_path", type=str, default="0")
64f6603c60Sopenharmony_ci    args = parser.parse_args()
65f6603c60Sopenharmony_ci    print(args.input_path)
66f6603c60Sopenharmony_ci    print(args.output_path)
67f6603c60Sopenharmony_ci    print(args.temp_path)
68f6603c60Sopenharmony_ci
69f6603c60Sopenharmony_ci    copy_files(args.input_path, args.temp_path)
70f6603c60Sopenharmony_ci    make_targz_one_by_one(args.output_path, args.temp_path)
71f6603c60Sopenharmony_ci
72f6603c60Sopenharmony_ci    shutil.rmtree(args.temp_path) # delete middle files