You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7 KiB

import os.path
from log import logger
import unzip
import file
import db
root_path = r'F:\Temp\sjry\hj'
# 初始化数据库
db_obj = db.DbAction()
file_obj = file.FilesUnzip(root_path)
unzip_obj = unzip.UnzipFile()
# 初始化成功和失败任务列表
unzip_succeed, unzip_failed = [], []
# 开始任务
def start_unzip_task():
result = True
for folder in (set(file_obj.get_root_folder_list()) - set(unzip_succeed)):
logger.info(f'开始解压 {folder}')
all_file = file_obj.get_cate_files(os.path.join(root_path, folder)) # 整理文件返回整理后的结果
print(all_file)
# 从数据库中获取数据
data = db_obj.get_data_by_id(folder)
print(data)
if data and all_file['handle_zip']:
# 解压, 增加适用历史密码重试的功能
if unzip_obj.unzip(all_file['handle_zip'][0], data['unzip_pwd']):
file_obj.del_all_files(all_file['zip'])
else:
print('password')
print(db_obj.get_available_pwd())
for pwd in db_obj.get_available_pwd():
result = unzip_obj.unzip(all_file['handle_zip'][0], pwd)
if result:
file_obj.del_all_files(all_file['zip'])
break
# 重新获取文件检查解压结果
all_file = file_obj.get_cate_files(os.path.join(root_path, folder)) # 整理文件返回整理后的结果
print(all_file)
# 检查没哟解压的文件
if all_file['handle_zip'] or all_file['zip']:
logger.info(f'{folder} 中依然存在没有解压的文件,请检查')
unzip_failed.append(folder) if folder not in unzip_failed else ''
result = False
# 检查未知文件
elif all_file['unknown']:
logger.info("打印没有处理的文件扩展名:")
logger.info(', '.join(all_file['unknown']))
unzip_failed.append(folder) if folder not in unzip_failed else ''
result = False
# 检查打印结果
if not all_file['handle_zip'] and not all_file['zip'] and not all_file['unknown']:
db_obj.insert_pwd(data['unzip_pwd'])
unzip_succeed.append(folder) if folder not in unzip_succeed else ''
logger.info('全部文件已解压')
return result
# 整理文件夹
def start_collation_task():
logger.info('开始整理文件夹')
for folder in file_obj.get_root_folder_list():
folder_path = os.path.join(root_path, folder)
file_col_obj = file.FilesCollection(folder_path)
if db_obj.get_data_by_id(folder) and folder not in unzip_failed:
logger.info(f'开始整理 {folder}')
# 清除多余的文件
file_col_obj.clear_files()
# 整理无效文件夹
file_col_obj.move_files()
# 删除空文件夹
file_col_obj.remove_empty()
# 从数据库中获取数据
data = db_obj.get_data_by_id(folder)
# 重命名文件夹
name = str(data['id']) + '_' + data['name']
file_col_obj.rename_root_folder(root_path, name, folder_path, os.path.join(root_path, name))
def main():
n = 1
# unzip_result = False
while n <= 5:
logger.info(f'{n}轮解压任务')
unzip_result = start_unzip_task()
if unzip_result:
break
n += 1
# if unzip_result:
start_collation_task()
logger.info('失败的任务:')
logger.info(', '.join(unzip_failed))
if __name__ == '__main__':
main()