1717from settings .base import WORK_DIR
1818from util .subprocc import SubProcController
1919from util .exceptions import ZIPError , TransferModuleError , NodeConfigError
20- from task .basic .common import subprocc_log
2120from util .pathlib import PathMgr
2221from 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