|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +通用 SFTP 上传脚本 |
| 4 | +支持平台:windows_x86、macos_x86_64、macos_aarch64、ubuntu_x86_64、ubuntu_aarch64 |
| 5 | +用法: |
| 6 | + python upload.py windows_x86 |
| 7 | + python upload.py macos_x86_64 |
| 8 | + ... |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import time |
| 13 | +import argparse |
| 14 | + |
| 15 | +import utils |
| 16 | + |
| 17 | +# -------------------- 配置区 -------------------- |
| 18 | +APP_NAME = "Qt-App" |
| 19 | +VERSION = "0.0.1" |
| 20 | +BUILD_DATE = time.strftime("%Y%m%d", time.localtime()) |
| 21 | + |
| 22 | +# 本地 releases 目录 |
| 23 | +RELEASES_PATH = os.path.join(os.path.dirname(__file__), "releases") |
| 24 | + |
| 25 | +# 远端公共根目录 |
| 26 | +REMOTE_ROOT = "/mnt/data/homes/admin/packages/qt-app" |
| 27 | + |
| 28 | +# 各平台配置 |
| 29 | +PLATFORMS = { |
| 30 | + "windows_x86": { |
| 31 | + "src_file": f"{APP_NAME}_installer.exe", |
| 32 | + "dst_tpl": f"{APP_NAME}_{VERSION}_{BUILD_DATE}_x86.exe", |
| 33 | + "remote": f"{REMOTE_ROOT}/windows", |
| 34 | + }, |
| 35 | + "macos_x86_64": { |
| 36 | + "src_file": f"{APP_NAME}.dmg", |
| 37 | + "dst_tpl": f"{APP_NAME}_{VERSION}_{BUILD_DATE}_x86_64.dmg", |
| 38 | + "remote": f"{REMOTE_ROOT}/macos/x86_64", |
| 39 | + }, |
| 40 | + "macos_aarch64": { |
| 41 | + "src_file": f"{APP_NAME}.dmg", |
| 42 | + "dst_tpl": f"{APP_NAME}_{VERSION}_{BUILD_DATE}_aarch64.dmg", |
| 43 | + "remote": f"{REMOTE_ROOT}/macos/aarch64", |
| 44 | + }, |
| 45 | + "ubuntu_x86_64": { |
| 46 | + "src_file": f"{APP_NAME}.deb", |
| 47 | + "dst_tpl": f"{APP_NAME}_{VERSION}_{BUILD_DATE}_x86_64.deb", |
| 48 | + "remote": f"{REMOTE_ROOT}/ubuntu/x86_64", |
| 49 | + }, |
| 50 | + "ubuntu_aarch64": { |
| 51 | + "src_file": f"{APP_NAME}.deb", |
| 52 | + "dst_tpl": f"{APP_NAME}_{VERSION}_{BUILD_DATE}_aarch64.deb", |
| 53 | + "remote": f"{REMOTE_ROOT}/ubuntu/aarch64", |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | +# SFTP 连接信息 |
| 58 | +SFTP_SERVER = "192.168.1.111" |
| 59 | +SFTP_PORT = 22 |
| 60 | +SFTP_USER = "root" |
| 61 | +SFTP_PWD = "123456" |
| 62 | +# -------------------- 逻辑区 -------------------- |
| 63 | + |
| 64 | + |
| 65 | +def upload_platform(platform_key: str) -> None: |
| 66 | + if platform_key not in PLATFORMS: |
| 67 | + raise ValueError(f"未知的 platform_key: {platform_key}") |
| 68 | + |
| 69 | + cfg = PLATFORMS[platform_key] |
| 70 | + |
| 71 | + src_path = os.path.join(RELEASES_PATH, cfg["src_file"]) |
| 72 | + dst_name = cfg["dst_tpl"] |
| 73 | + dst_path = os.path.join(RELEASES_PATH, dst_name) |
| 74 | + |
| 75 | + # 若目标文件已存在,先删除(utils.remove 内部已做存在性检查) |
| 76 | + utils.remove(dst_path) |
| 77 | + # 重命名 |
| 78 | + os.rename(src_path, dst_path) |
| 79 | + |
| 80 | + # 生成 update.json |
| 81 | + json_path = os.path.join(RELEASES_PATH, "update.json") |
| 82 | + utils.generate_json(VERSION, dst_path, dst_name, json_path) |
| 83 | + |
| 84 | + # 上传主安装包 |
| 85 | + utils.sftp_upload_file( |
| 86 | + SFTP_SERVER, |
| 87 | + SFTP_PORT, |
| 88 | + SFTP_USER, |
| 89 | + SFTP_PWD, |
| 90 | + dst_path, |
| 91 | + f"{cfg['remote']}/{dst_name}", |
| 92 | + ) |
| 93 | + |
| 94 | + # 上传 update.json |
| 95 | + utils.sftp_upload_file( |
| 96 | + SFTP_SERVER, |
| 97 | + SFTP_PORT, |
| 98 | + SFTP_USER, |
| 99 | + SFTP_PWD, |
| 100 | + json_path, |
| 101 | + f"{cfg['remote']}/update.json", |
| 102 | + ) |
| 103 | + |
| 104 | + print(f"[{platform_key}] 上传完成 ✔") |
| 105 | + |
| 106 | + |
| 107 | +def main() -> None: |
| 108 | + parser = argparse.ArgumentParser(description="通用 SFTP 上传脚本") |
| 109 | + parser.add_argument( |
| 110 | + "platform", |
| 111 | + choices=list(PLATFORMS.keys()), |
| 112 | + help="要上传的平台标识", |
| 113 | + ) |
| 114 | + args = parser.parse_args() |
| 115 | + upload_platform(args.platform) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
| 120 | + |
| 121 | +# MacOS |
| 122 | +# sudo spctl -vvv --assess --type execute /Applications/Qt-App.app |
| 123 | +# 输出如下: |
| 124 | +# /Applications/Qt-App.app: accepted |
| 125 | +# source=Notarized Developer ID |
| 126 | +# origin=Developer ID Application: ********(*******) |
| 127 | + |
| 128 | +# Ubuntu |
| 129 | +# dpkg -r Qt-App |
| 130 | +# dpkg -i Qt-App_0.0.1_20250810.deb |
| 131 | +# dpkg -i --force-overwrite Qt-App_0.0.1_20250810.deb |
0 commit comments