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