From 6c2d2981651c25d44f940bb0435942056f6a71ee Mon Sep 17 00:00:00 2001 From: CoderKang Date: Thu, 18 Sep 2025 11:49:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=20magisk.py=20=E4=B8=AD?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B8=85=E7=90=86=E4=B8=B4=E6=97=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=8F=82=E6=95=B0=E4=BB=A5=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E6=B8=85=E7=90=86=E8=A1=8C=E4=B8=BA=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=9E=84=E5=BB=BA=E6=B5=81=E7=A8=8B=E7=9A=84=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- magisk.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/magisk.py b/magisk.py index f19aac3..9c37b94 100644 --- a/magisk.py +++ b/magisk.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import argparse import hashlib import os import platform @@ -150,24 +151,61 @@ class Magisk: print("==> Magisk installation completed!") - def run(self): + def cleanup(self, keep_downloads=False): + """清理临时文件和目录""" + print("==> Cleaning up temporary files...") + + # 清理解压临时目录 + if os.path.exists(self.extract_to): + shutil.rmtree(self.extract_to, ignore_errors=True) + print(f"==> Removed temporary extraction directory: {self.extract_to}") + + # 可选择性清理下载目录 + if not keep_downloads and os.path.exists(self.download_loc): + shutil.rmtree(self.download_loc, ignore_errors=True) + print(f"==> Removed download directory: {self.download_loc}") + elif keep_downloads: + print(f"==> Keeping download directory: {self.download_loc}") + + def run(self, cleanup_after=True, keep_downloads=True): """执行完整的构建流程""" try: self.download() self.extract() self.copy() + success = True except Exception as e: print(f"Error: {e}") - return False - return True + success = False + finally: + # 无论成功还是失败都清理临时文件 + if cleanup_after: + self.cleanup(keep_downloads=keep_downloads) + + return success def main(): """主函数""" + parser = argparse.ArgumentParser(description='Magisk vendor package builder') + parser.add_argument('--no-cleanup', action='store_true', + help='不清理临时文件和下载文件') + parser.add_argument('--remove-downloads', action='store_true', + help='同时删除下载的文件') + + args = parser.parse_args() + + # 根据参数确定清理行为 + cleanup_after = not args.no_cleanup + keep_downloads = not args.remove_downloads + magisk = Magisk() - success = magisk.run() + success = magisk.run(cleanup_after=cleanup_after, keep_downloads=keep_downloads) + if success: print("==> Magisk vendor package created successfully!") + if cleanup_after: + print("==> Temporary files cleaned up") else: print("==> Failed to create Magisk vendor package!") exit(1)