python-zip-filemove-fileencryption可做工程配置

直接上代码

配置代码 configs.py

# -*- coding: utf-8 -*-

projects={}
projects["xxx_project"]={
    "password"          : "这不是密码,不要误会~",
    "cache_root_dir"    : "E:/Persistent/cache/xxx_project",
    "project_rootdir"   : "E:/projects/xxx_project",
     # 被加压缩加密之后的文件名
    "zip_filename"      : "a304157b4ca681edxxx",
    #要压缩和加密码的目录和文件列表
    "need_zip_list"     : [
        ".git",
        "xxx1_folder",
        "xxx2_folder",
        "xxx1_file",
        "xxx2_file"
    ], 
    #忽略的目录和文件列表
    "ignore_list"       : [
        "temp",
        "build"
    ], 
    #这个列表不用配置,除了zip_list和ignore_list,剩下的都属性move_list
    "need_move_list"    : [], 
}

实现代码 tool.py

# -*- coding: utf-8 -*-
import sys, os, zipfile
import shutil
import configs

# 压缩
def zip_compress(target_fileordir_list, out_file_name):
    print('compress start...')
    # file_dic = {}
    rootpath,xxx = os.path.split(target_fileordir_list[0])    
    rootpath_len = len(rootpath)
    temp_file_list = []
    for fileordir in target_fileordir_list:    
        if os.path.isfile(fileordir):
            temp_file_list.append(os.path)
        else:
            for parent,dirs,files in os.walk(fileordir):
                for name in files:
                    temp_file_list.append(os.path.join(parent, name))
                    # print('==========')
                    # print('root:'+parent)
                    # print('name:'+name)
        # print('path:'+path + '  filename:'+filename)
    out_dir,out_filename = os.path.split(out_file_name)
    if os.path.exists(out_dir) == False:
        os.makedirs(out_dir)
    zip_file = zipfile.ZipFile(out_file_name,'w',zipfile.ZIP_DEFLATED)
    for file in temp_file_list:
        arcname = file[rootpath_len:]
        zip_file.write(file,arcname)
        # print('rootpath:'+rootpath)
        # print('arcname:'+arcname)
    zip_file.close()
    print('compress end...')

# 解压缩
def zip_uncompress(target_zip_file_ame,out_dir):
    print('uncompress start...')
    zip_file = zipfile.ZipFile(target_zip_file_ame,'r')
    if os.path.isdir(out_dir)==False:
        os.mkdir(out_dir)
    for names in zip_file.namelist():
        zip_file.extract(names, out_dir,'123')
    zip_file.close()
    print('uncompress end...')

def en_decrypt_file(filepath, outpath, key):
    key_bytes = bytes(key, encoding='utf-8')
    key_bytes_len = len(key_bytes)
    # print(len(key_bytes))
    fo = open(filepath, 'rb')
    file_bytes = fo.read()
    fo.close()
    # print(len(file_bytes))
    file_bytearray = bytearray(file_bytes)
    file_bytearray_len = len(file_bytearray)
    # print(file_bytearray_len)
    # for i in range(file_bytearray_len):
    #     file_bytearray[i] ^= file_bytearray[i%key_bytes_len]
    fw = open(outpath,'wb')
    fw.write(file_bytearray)
    fw.close()


# 缓存工程
def cache_project(config):
    # 压缩
    # print('开始压缩...')
    temp_zipfile_path = os.path.join(config['cache_root_dir'], 'temp.zip~')    
    zip_list = []
    for rpath in config['need_zip_list'] or []:
        zip_list.append(os.path.join(config['project_rootdir'], rpath))
    # zip_compress(zip_list, temp_zipfile_path)
    
    # # 加密
    # print('开始加密...')
    # en_decrypt_file(temp_zipfile_path, os.path.join(config['project_rootdir'], config['zip_filename']), config['password'])
    # os.remove(temp_zipfile_path)

    # 剪切
    print('开始剪切...')
    need_move_list =  os.listdir(config['project_rootdir'])
    # print(len(need_move_list))
    for file in config['need_zip_list'] or []:
        if file in need_move_list:
            # print("need_zip_list: "+file)
            need_move_list.remove(file)
    # print(len(need_move_list))
    for file in config['ignore_list'] or []:
        if file in need_move_list:
            # print("ignore_list: "+file)
            need_move_list.remove(file)
    # print(len(need_move_list))
    for file in need_move_list or []:        
        src = os.path.join(config['project_rootdir'], file)
        dst = os.path.join(config['cache_root_dir'], file)
        print(file,src,dst)
        shutil.move(src, dst)

    # 清空工程目录
    print('开始清空工程目录...')
    # shutil.rmtree(config['project_rootdir'])
    return

# 还原工程
def recover_project(config):
    return


# 入口方法
def run():
    if len(sys.argv) <= 2 :
        print('请同时指定工程配置名和操作码\n操作码:1-表示缓存 2-表示还原')
        return

    config_name = sys.argv[1]
    operate_code = sys.argv[2]
    if config_name not in configs.projects.keys():
        print('指定的配置不存在:'+config_name)
        return
    if operate_code != '1' and  operate_code != '2':
        print('操作码只能是1和2,1-表示缓存 2-表示还原')
        return
    project_cofig = configs.projects[config_name]
    if operate_code == '1':
        cache_project(project_cofig)


if __name__ == '__main__':
    run()

命令行调用

python tool.py xxx_project 1

你可能感兴趣的:(Python3)