fix: 优化 magisk.py 中的主机架构检测逻辑,默认返回 x86_64,并简化清理功能,移除不必要的命令行参数支持。

This commit is contained in:
2025-09-18 11:51:54 +08:00
parent 6c2d298165
commit 991faf2887

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import hashlib import hashlib
import os import os
import platform import platform
@@ -37,16 +36,10 @@ def get_host_arch():
"armv8l": ("arm", 32) "armv8l": ("arm", 32)
} }
if machine in mapping: if machine in mapping:
# if mapping[machine] == "x86_64":
# with open("/proc/cpuinfo") as f:
# if "sse4_2" not in f.read():
# print("x86_64 CPU does not support SSE4.2, falling back to x86...")
# return ("x86", 32)
return mapping[machine] return mapping[machine]
# 如果不在映射中,默认返回 arm64适用于 Android 构建)
print(f"Warning: platform.machine '{machine}' architecture not recognized, defaulting to arm64") print(f"Warning: platform.machine '{machine}' architecture not recognized, defaulting to arm64")
return ("arm64", 64) return ("x86_64", 64)
def run_command(cmd): def run_command(cmd):
@@ -67,7 +60,7 @@ class Magisk:
self.extract_to = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp") self.extract_to = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp")
self.copy_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "magisk") self.copy_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "magisk")
self.magisk_dir = os.path.join(self.copy_dir, "system", "etc", "init", "magisk") self.magisk_dir = os.path.join(self.copy_dir, "system", "etc", "init", "magisk")
self.machine = get_host_arch() # 返回 (arch, bits) 元组 self.machine = get_host_arch()
def download(self): def download(self):
"""下载 Magisk APK""" """下载 Magisk APK"""
@@ -151,7 +144,7 @@ class Magisk:
print("==> Magisk installation completed!") print("==> Magisk installation completed!")
def cleanup(self, keep_downloads=False): def cleanup(self):
"""清理临时文件和目录""" """清理临时文件和目录"""
print("==> Cleaning up temporary files...") print("==> Cleaning up temporary files...")
@@ -160,14 +153,12 @@ class Magisk:
shutil.rmtree(self.extract_to, ignore_errors=True) shutil.rmtree(self.extract_to, ignore_errors=True)
print(f"==> Removed temporary extraction directory: {self.extract_to}") print(f"==> Removed temporary extraction directory: {self.extract_to}")
# 可选择性清理下载目录 # 清理下载目录
if not keep_downloads and os.path.exists(self.download_loc): if os.path.exists(self.download_loc):
shutil.rmtree(self.download_loc, ignore_errors=True) shutil.rmtree(self.download_loc, ignore_errors=True)
print(f"==> Removed download directory: {self.download_loc}") 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): def run(self):
"""执行完整的构建流程""" """执行完整的构建流程"""
try: try:
self.download() self.download()
@@ -178,33 +169,18 @@ class Magisk:
print(f"Error: {e}") print(f"Error: {e}")
success = False success = False
finally: finally:
# 无论成功还是失败都清理临时文件 self.cleanup()
if cleanup_after:
self.cleanup(keep_downloads=keep_downloads)
return success return success
def main(): 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() magisk = Magisk()
success = magisk.run(cleanup_after=cleanup_after, keep_downloads=keep_downloads) success = magisk.run()
if success: if success:
print("==> Magisk vendor package created successfully!") print("==> Magisk vendor package created successfully!")
if cleanup_after:
print("==> Temporary files cleaned up") print("==> Temporary files cleaned up")
else: else:
print("==> Failed to create Magisk vendor package!") print("==> Failed to create Magisk vendor package!")