Created
April 3, 2014 08:48
-
-
Save lyhapple/9950785 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DataRestore(object): | |
| def __init__(self, request): | |
| """ | |
| 数据恢复类 | |
| 系统配置--数据恢复页面主要功能: 上传--备份恢复前的--解压覆盖 | |
| """ | |
| self.request = request | |
| self.uploadFileInputName = "uploadFile" | |
| #上传文件存储的临时目录 | |
| self.strUploadTempDir = os.path.join(settings.WebTempDir, "dw-cbv4datarestore", pubfun.getTimestampStr()) | |
| #文件的全路径,包含文件名 | |
| self.filePullPath = "" | |
| def setSelfFile(self): | |
| """ | |
| 初始化时设置文件对象 | |
| :return: 文件对象 :raise Exception:获取备份文件数据失败 | |
| """ | |
| if self.uploadFileInputName in self.request.FILES: | |
| self.file = self.request.FILES[self.uploadFileInputName] | |
| self.filePullPath = os.path.join(self.strUploadTempDir, self.file.name) | |
| else: | |
| raise Exception("未取得上传文件,请刷新页面后重新尝试!") | |
| def getFileSuffix(self): | |
| """ | |
| 获取文件后缀名 | |
| :return: | |
| """ | |
| return os.path.splitext(self.file.name)[1] | |
| def checkFileSuffix(self): | |
| """ | |
| 检查文件后缀名 | |
| """ | |
| strSuffix = self.getFileSuffix() | |
| if strSuffix != ".cfmagbak": | |
| raise Exception("上传的数据备份文件后缀名称不正确!") | |
| def renameAndUnzip(self): | |
| """ | |
| 重命名上传的bak文件,并解压出来,再检查解压后的文件 | |
| :raise Exception: | |
| """ | |
| if self.filePullPath == "": | |
| raise Exception("未取得上传文件的路径!") | |
| strSuffix = self.getFileSuffix() | |
| strNewName = self.file.name.replace(strSuffix, ".zip") | |
| strNewPath = os.path.join(os.path.dirname(self.filePullPath), strNewName) | |
| os.rename(self.filePullPath, strNewPath) | |
| intUnzipRet = zbkcCompressFile.unzip_file(strNewPath, os.path.dirname(self.filePullPath)) | |
| if intUnzipRet == 0: | |
| raise Exception("解压缩文件失败,请检查上传的文件!") | |
| def uploadFile(self): | |
| """ | |
| 上传文件 | |
| :raise Exception: | |
| """ | |
| if not pubfun.handleUploadedFile(self.strUploadTempDir, self.file): | |
| raise Exception("上传数据备份文件失败!") | |
| def deCompressFileAndOverwrite(self): | |
| """ | |
| 1.解压缩DB文件,原文件名称是 .cfbak结尾的文件 | |
| 2.覆盖解压出来的文件 | |
| """ | |
| #解压文件所在目录 | |
| strBackFilePath = os.path.join(self.strUploadTempDir, "cfmagbak") | |
| #开始解压缩,把.cfbak文件解压成.cfg文件 | |
| strLogDbName = os.path.basename(settings.cfLogDbFileV4) | |
| strMainDbName = os.path.basename(settings.cfMainDbFileV4) | |
| strLogRet = zlibDeCompress(strBackFilePath, "log.cfbak", strLogDbName) | |
| strMainRet = zlibDeCompress(strBackFilePath, "main.cfbak", strMainDbName) | |
| if "error" in [strLogRet, strMainRet]: | |
| raise Exception("解压缩数据库文件失败!") | |
| #文件替换,替换失败则抛出异常提示 | |
| try: | |
| os.remove(settings.cfLogDbFileV4) | |
| os.remove(settings.cfMainDbFileV4) | |
| shutil.copyfile(os.path.join(strBackFilePath, strLogDbName), settings.cfLogDbFileV4) | |
| shutil.copyfile(os.path.join(strBackFilePath, strLogDbName), settings.cfMainDbFileV4) | |
| except: | |
| raise Exception("覆盖现有数据库文件失败!") | |
| def clearUploadTemp(self): | |
| """ | |
| 清理上传和解压出来的所有临时文件,直接删除临时目录即可 | |
| 如清理出错,则忽略 | |
| """ | |
| try: | |
| shutil.rmtree(self.strUploadTempDir) | |
| except: | |
| pass | |
| def fire(self): | |
| self.setSelfFile() | |
| self.checkFileSuffix() | |
| self.uploadFile() | |
| self.renameAndUnzip() | |
| backOldDBFile() | |
| self.deCompressFileAndOverwrite() | |
| self.clearUploadTemp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment