python--手动生成git修改的文件的压缩包

 

git add 之后,git status 把添加的文件通过正则替换后,复制到files = [xxx]中

比如:

modified:   crawler/main.py
modified:   crawler/download.py
替换为
'crawler/main.py',
'crawler/download.py',
 
def git_pack():
    # (?:modified|new file):\s*(.*)
    # '$1',
    files = [
        'crawler/main.py',
        'crawler/download.py',
    ]
    current_path = os.path.abspath(os.path.dirname(__file__))
    print(current_path)
    parent_path = os.path.dirname(current_path)  # os.path.abspath(f'{current_path}\\..\\')
    print(parent_path)
    target_dir = r'D:\programming\python\work\pack'
    current_pack = str(datetime.datetime.now()).replace(':', '_')
    target_dir = os.path.join(target_dir, current_pack)  # f"{target_dir}\\{str(datetime.datetime.now()).replace(':', '_')}"
    for file in files:
        old_file = os.path.join(parent_path, file)  # f"{parent_path}\\{file}"
        print(f"old: {old_file}")
        new_file = os.path.join(target_dir, file)  # f"{target_dir}\\{file}"
        print(f"new: {new_file}")
        new_dir = os.path.dirname(new_file)
        if not os.path.exists(new_dir):
            os.makedirs(new_dir)
        shutil.copyfile(old_file, new_file)
    print('*' * 30)
    pack(target_dir, current_pack)


def pack(target_dir, current_pack):
    pack_file_name = os.path.join(os.path.dirname(target_dir), f'{current_pack}.zip')
    pack_zip = zipfile.ZipFile(pack_file_name, 'w')
    for dir_path, dir_names, file_names in os.walk(target_dir):
        # print(dir_path)
        for filename in file_names:
            pack_file = os.path.join(dir_path, filename)
            print(pack_file)
            pack_zip.write(pack_file, os.path.relpath(pack_file, target_dir))
    pack_zip.close()


if __name__ == '__main__':
    git_pack()

 

 

posted @ 2022-02-09 18:01  liDB  阅读(56)  评论(0编辑  收藏  举报