tar_files.py 2.2 KB
Newer Older
F
FondMemoryVVV 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2020-2021 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
import argparse
import tarfile
import shutil

copyFileCounts = 0

F
FondMemoryVVV 已提交
25 26 27 28 29
def copyFiles(sourceDir, targetDir):
    global copyFileCounts
    for f in os.listdir(sourceDir):
        sourceF = os.path.join(sourceDir, f)
        targetF = os.path.join(targetDir, f)
F
FondMemoryVVV 已提交
30 31 32 33 34 35 36 37
        if not os.path.isfile(sourceF):
            if os.path.isdir(sourceF):
                copyFiles(sourceF, targetF)
        elif os.path.isfile(sourceF):
            if os.path.exists(targetDir):
                copyFileCounts += 1
                open(targetF, "wb").write(open(sourceF, "rb").read())
            elif not os.path.exists(targetDir):
F
FondMemoryVVV 已提交
38
                os.makedirs(targetDir)
F
FondMemoryVVV 已提交
39 40
                copyFileCounts += 1
                open(targetF, "wb").write(open(sourceF, "rb").read())
F
FondMemoryVVV 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

def make_targz_one_by_one(output_filename, source_dir):
    tar = tarfile.open(output_filename,"w")
    for root,dir,files in os.walk(source_dir):
      for file in files:
          pathfile = os.path.join(root, file)
          tar.add(pathfile)
    tar.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='manual to this script')
    parser.add_argument("--input_path", type=str, default="0")
    parser.add_argument("--output_path", type=str, default="0")
    parser.add_argument("--temp_path", type=str, default="0")
    args = parser.parse_args()
    print(args.input_path)
    print(args.output_path)
    print(args.temp_path)

    copyFiles(args.input_path, args.temp_path)
    make_targz_one_by_one(args.output_path, args.temp_path)

F
FondMemoryVVV 已提交
63
    shutil.rmtree(args.temp_path) #delete middle files