1、获取web目录文件更新,并打包。
# -*- coding: utf-8 -*-
'''
数据库脚本
CREATE TABLE `t_td_update_check_version` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`version_name` varchar(255) DEFAULT NULL,
`version_time` datetime DEFAULT NULL,
`file_lists` longtext,
`zip_file` varchar(255) DEFAULT NULL,
`status` tinyint(4) DEFAULT '0' COMMENT '0初始生成 1已处理',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4;
'''
import os,time,shutil,MySQLdb,datetime,json,MySQLdb.cursors,shutil
time_now = datetime.datetime.now()
time_now_str = time_now.strftime('%Y-%m-%d %H:%M:%S');
version_name = time_now.strftime('%Y%m%d%H%M%S');
# source_path = 'C:/Users/user/Desktop/fast'
# base_path = "E:/test/"
# version_time = '1970-01-01 00:00:00'
source_path = '/web/www/temp/'
base_path = "/web/www/oa_operation/dest/"
version_time = '2018-08-25 00:00:00'
except_path = ['/web/www/temp/runtime','/web/www/temp/upload','C:/Users/user/Desktop/fast/tt/2222/']
# 打开数据库连接
# db = MySQLdb.connect(host="localhost", port=3306,user="root", passwd="", db="op_operation", charset='utf8',cursorclass = MySQLdb.cursors.DictCursor )
db = MySQLdb.connect(host="localhost", port=3306,user="root", passwd="*******", db="oa_operation", charset='utf8',cursorclass = MySQLdb.cursors.DictCursor )
cursor = db.cursor()
sql="select * from t_td_update_check_version order by id desc limit 1"
cursor.execute(sql)
data = cursor.fetchone()
if data:
version_time = data.get('version_time').strftime("%Y-%m-%d %H:%M:%S")
print('version_time:'+version_time)
files_list = [] #更新的文件列表
version_path = base_path+version_name;
version_update_path =base_path+version_name+"/update";
if os.path.exists(version_path):
shutil.rmtree(version_path) #递归删除文件夹及内容
print('rmtree:' + version_path)
os.remove(version_path+".zip")
print('remove:' + version_path+".zip")
#检查更新
def check_update(filepath,dest_root_path):
#遍历filepath下所有文件,包括子目录
files = os.listdir(filepath)
for fi in files:
fi_d = os.path.join(filepath,fi)
if check_dir(fi_d,except_path)==0:
dest_path = os.path.join(dest_root_path,fi)
if os.path.isdir(fi_d):
check_update(fi_d,dest_path)
else:
modify_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime( os.path.join(filepath,fi_d) )))
if modify_time > version_time :
print(fi_d + "||" + modify_time + "||" + version_time)
if os.path.exists(dest_root_path)==0:
os.makedirs(dest_root_path)
shutil.copy(os.path.join(filepath,fi_d),os.path.join(dest_root_path,fi))
files_list.append(os.path.join(filepath,fi_d));
log_file = os.path.join(version_path,"log.txt")
with open(log_file, 'a+') as f:
f.write(os.path.join(version_path,fi_d) + '\n') # 加\n换行显示
#判断是否包含文件夹
def check_dir(dir,except_path):
dir = dir.replace("\\","/")
if dir in except_path:
return 1
for val in except_path:
if val in dir:
return 2
return 0
#递归遍历/root目录下所有文件
check_update(source_path,version_update_path)
if len(files_list) :
zip_file = shutil.make_archive(version_path, 'zip', version_path)
print(zip_file)
json_file_list=json.dumps(files_list)
sql = "INSERT INTO t_td_update_check_version(version_name,version_time, file_lists,zip_file) \
VALUES ('%s', '%s', '%s', '%s')" % \
(version_name, time_now_str, json_file_list,zip_file.replace("\\","/"))
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 发生错误时回滚
db.rollback()
db.close()