| 28 | |
| 29 | |
| 30 | class Zip(object): |
| 31 | def __init__(self): |
| 32 | """ |
| 33 | 检查工具的存在与是否可用 |
| 34 | """ |
| 35 | # logger.info("初始化zip模块中...") |
| 36 | if not os.environ.get(ZIP_HOME, False): |
| 37 | raise NodeConfigError("the node does not have ZIP tool, please check.") |
| 38 | self.ZIP_TOOL_NAME = "7z" |
| 39 | if platform.platform().find("debian") >= 0: |
| 40 | logger.info("platform: %s" % platform.platform()) |
| 41 | logger.info( |
| 42 | "检测到在ubuntu系统中调用zip,因此将调用7z_ubuntu。(7z_ubuntu可能存在兼容问题,如果异常,请自行安装:sudo apt-get install p7zip-full)" |
| 43 | ) |
| 44 | self.ZIP_TOOL_NAME = "7z_ubuntu" |
| 45 | |
| 46 | def compress(self, path, zip_file): |
| 47 | """ |
| 48 | 基于7z实现的压缩,默认使用16线程极速压缩 |
| 49 | :param path: 被压缩的资源路径 |
| 50 | :param zip_file: 压缩完成生成的文件 |
| 51 | :return: |
| 52 | """ |
| 53 | logger.info("zip模块执行压缩操作...") |
| 54 | args = [ |
| 55 | os.path.join(os.environ[ZIP_HOME], self.ZIP_TOOL_NAME), |
| 56 | "a", |
| 57 | "-t7z", |
| 58 | zip_file, |
| 59 | "%s/*" % path, |
| 60 | "-mmt16", |
| 61 | "-mx1", |
| 62 | ] |
| 63 | if os.path.exists(zip_file): |
| 64 | PathMgr().rmpath(zip_file) |
| 65 | try: |
| 66 | args = PathMgr().format_cmd_arg_list(args) |
| 67 | process = SubProcController( |
| 68 | args, |
| 69 | print_enable=False, |
| 70 | shell=False, |
| 71 | stdout_filepath=None, |
| 72 | stderr_filepath=None, |
| 73 | stdout_line_callback=self.compress_subprocc_log, |
| 74 | stderr_line_callback=self.compress_subprocc_log, |
| 75 | env=EnvSet().get_origin_env(), |
| 76 | ) |
| 77 | process.wait(ZIP_TIME_LIMIT) |
| 78 | except FileNotFoundError: |
| 79 | self.zipfile_compress(path, zip_file) |
| 80 | if not os.path.exists(zip_file): |
| 81 | err_msg = "compress error! zip file is empty, please check! 如果是ubuntu环境,7z_ubuntu可能存在兼容问题," \ |
| 82 | "请自行安装:sudo apt-get install p7zip-full" |
| 83 | logger.error(err_msg) |
| 84 | raise ZIPError(err_msg) |
| 85 | return True |
| 86 | |
| 87 | def decompress_by_7z(self, zip_file, path, print_enable=False): |
no outgoing calls
no test coverage detected