123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import os
- import platform
- import requests
- import gzip
- import shutil
- from zipfile import ZipFile
- import stat
- # 定义常量
- BASE_URL = 'https://github.com/MetaCubeX/mihomo/releases/download'
- VERSION = 'v1.18.8'
- ASSETS_DIR = './assets/bin'
- SYSTEM = platform.system().lower()
- ARCH = platform.machine()
- # 如果目录不存在,创建下载目录
- if not os.path.exists(ASSETS_DIR):
- os.makedirs(ASSETS_DIR)
- def get_download_url():
- """
- 根据系统和架构生成下载链接
- """
- if SYSTEM == 'darwin' and ARCH == 'x86_64':
- file_name = f'mihomo-darwin-amd64-{VERSION}.gz'
- elif SYSTEM == 'darwin' and ARCH == 'arm64':
- file_name = f'mihomo-darwin-arm64-{VERSION}.gz'
- elif SYSTEM == 'linux' and ARCH == 'x86_64':
- file_name = f'mihomo-linux-amd64-{VERSION}.gz'
- elif SYSTEM == 'linux' and ARCH == 'arm64':
- file_name = f'mihomo-linux-arm64-{VERSION}.gz'
- elif SYSTEM == 'windows' and ARCH == 'x86_64':
- file_name = f'mihomo-windows-amd64-{VERSION}.zip'
- else:
- raise ValueError(f"Unsupported system/architecture: {SYSTEM} {ARCH}")
- return f"{BASE_URL}/{VERSION}/{file_name}", file_name
- def download_file(url, output_path):
- """
- 下载文件
- """
- print(f"Downloading {url} ...")
- response = requests.get(url, stream=True)
- if response.status_code == 200:
- with open(output_path, 'wb') as file:
- shutil.copyfileobj(response.raw, file)
- print(f"Download complete: {output_path}")
- else:
- print(f"Failed to download {url}, status code: {response.status_code}")
- def extract_gz_file(file_path, output_path):
- """
- 解压 .gz 文件
- """
- print(f"Extracting {file_path} ...")
- with gzip.open(file_path, 'rb') as f_in:
- with open(output_path, 'wb') as f_out:
- shutil.copyfileobj(f_in, f_out)
- print(f"Extracted to {output_path}")
- def extract_zip_file(file_path, output_dir):
- """
- 解压 .zip 文件
- """
- print(f"Extracting {file_path} ...")
- with ZipFile(file_path, 'r') as zip_ref:
- zip_ref.extractall(output_dir)
- print(f"Extracted to {output_dir}")
- def add_executable_permission(file_path):
- """
- 添加可执行权限(适用于 macOS 和 Linux)
- """
- if SYSTEM != 'windows':
- print(f"Adding executable permission to {file_path}")
- st = os.stat(file_path)
- os.chmod(file_path, st.st_mode | stat.S_IEXEC)
- print(f"Executable permission added to {file_path}")
- def main():
- # 获取下载链接和文件名
- url, file_name = get_download_url()
- # 下载文件
- download_path = os.path.join(ASSETS_DIR, file_name)
- download_file(url, download_path)
- # 构建输出路径和重命名文件
- service_name = f'ccore-{SYSTEM}-{ARCH}'
- output_file = os.path.join(ASSETS_DIR, service_name)
- # 解压文件并重命名
- if file_name.endswith('.gz'):
- extract_gz_file(download_path, output_file)
- elif file_name.endswith('.zip'):
- extract_zip_file(download_path, ASSETS_DIR)
- # 添加可执行权限
- add_executable_permission(output_file)
- # 删除下载的压缩文件
- if os.path.exists(download_path):
- os.remove(download_path)
- print(f"Deleted temp file: {download_path}")
- if __name__ == "__main__":
- main()
|