Skip to content

Commit 2976166

Browse files
committed
✨zip解压模块捕获异常
1 parent 109dd8c commit 2976166

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

client/tool/util/tca_ql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __download_database(self, params, path):
7171
file_server = RetryFileServer(retry_times=2).get_server()
7272
if file_server.download_big_file(file_name, zip_file):
7373
logger.info("下载成功")
74-
Zip.decompress(zip_file, db_path)
74+
Zip.decompress_by_7z(zip_file, db_path)
7575
return True
7676
except Exception as e:
7777
logger.warning(f"下载失败 {e}")

client/util/zipmgr.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from settings.base import WORK_DIR
1818
from util.subprocc import SubProcController
1919
from util.exceptions import ZIPError, TransferModuleError, NodeConfigError
20-
from task.basic.common import subprocc_log
2120
from util.pathlib import PathMgr
2221
from util.envset import EnvSet
2322

@@ -71,8 +70,8 @@ def compress(self, path, zip_file):
7170
shell=False,
7271
stdout_filepath=None,
7372
stderr_filepath=None,
74-
stdout_line_callback=subprocc_log,
75-
stderr_line_callback=subprocc_log,
73+
stdout_line_callback=self.compress_subprocc_log,
74+
stderr_line_callback=self.compress_subprocc_log,
7675
env=EnvSet().get_origin_env(),
7776
)
7877
process.wait(ZIP_TIME_LIMIT)
@@ -97,7 +96,7 @@ def decompress_by_7z(self, zip_file, path, print_enable=False):
9796

9897
args = PathMgr().format_cmd_arg_list(args)
9998
if print_enable:
100-
output = subprocc_log
99+
output = self.subprocc_log
101100
else:
102101
output = None
103102
process = SubProcController(
@@ -107,7 +106,7 @@ def decompress_by_7z(self, zip_file, path, print_enable=False):
107106
stdout_filepath=None,
108107
stderr_filepath=None,
109108
stdout_line_callback=output,
110-
stderr_line_callback=subprocc_log,
109+
stderr_line_callback=self.subprocc_log,
111110
env=EnvSet().get_origin_env(),
112111
)
113112
process.wait(ZIP_TIME_LIMIT)
@@ -120,7 +119,7 @@ def decompress_by_7z(self, zip_file, path, print_enable=False):
120119
def decompress(self, zip_file, path):
121120
logger.info("zip模块执行解压操作...")
122121
# 20190927 bug-fixed, 防止在当前目录下删除当前目录,出现权限异常情况
123-
os.chdir("../task")
122+
os.chdir("..")
124123
if os.path.exists(path):
125124
PathMgr().rmpath(path)
126125

@@ -130,6 +129,14 @@ def decompress(self, zip_file, path):
130129
os.chdir(WORK_DIR)
131130
return True
132131

132+
def subprocc_log(self, line):
133+
logger.info(line)
134+
if line.find("Can not open the file as archive"):
135+
raise TransferModuleError("解压失败,压缩文件损坏或格式不对")
136+
137+
def compress_subprocc_log(self, line):
138+
logger.info(line)
139+
133140
@staticmethod
134141
def zipfile_compress(path, zip_file):
135142
"""
@@ -142,7 +149,7 @@ def zipfile_compress(path, zip_file):
142149
zp = zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED)
143150
for dirpath, dirnames, filenames in os.walk(path):
144151
fpath = dirpath.replace(path, "") # 这一句很重要,不replace的话,就从根目录开始复制
145-
fpath = fpath and fpath + os.sep or ""
152+
fpath = fpath and fpath + os.sep or "" # 这句话理解我也点郁闷,实现当前文件夹以及包含的所有文件的压缩
146153
for filename in filenames:
147154
zp.write(os.path.join(dirpath, filename), fpath + filename)
148155
zp.close()

0 commit comments

Comments
 (0)