From 5ae4db96ebe0331ee7e2d6790c2903636079d9e4 Mon Sep 17 00:00:00 2001 From: Kissablecho Date: Tue, 8 Jul 2025 02:43:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Nuitka=E6=89=93=E5=8C=85?= =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E6=B5=81=E5=8F=8A=E7=9B=B8=E5=85=B3=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/nuitka_package.yml | 63 +++++++++++++ package/config.yml | 104 --------------------- package/nuitka_build.py | 132 +++++++++++++++++++++++++++ package/nuitka_config.yml | 15 +++ 4 files changed, 210 insertions(+), 104 deletions(-) create mode 100644 .github/workflows/nuitka_package.yml create mode 100644 package/nuitka_build.py create mode 100644 package/nuitka_config.yml diff --git a/.github/workflows/nuitka_package.yml b/.github/workflows/nuitka_package.yml new file mode 100644 index 0000000..3ae5098 --- /dev/null +++ b/.github/workflows/nuitka_package.yml @@ -0,0 +1,63 @@ +name: Nuitka Package(dev) + +on: + workflow_dispatch: + push: + branches: + - dev + paths: + - 'package/config.yml' + +jobs: + package-and-release: + runs-on: windows-latest + + # 添加必要的权限 + permissions: + contents: write # 允许创建发布和上传资源 + + steps: + - name: Set Timezone to China Standard Time + run: tzutil /s "China Standard Time" + shell: pwsh + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install nuitka pyyaml + + - name: Set up MinGW + uses: egor-tensin/setup-mingw@v2 + with: + platform: x64 + + - name: Fix Python encoding (Windows) + run: | + # 设置Python使用UTF-8编码 + [System.Environment]::SetEnvironmentVariable('PYTHONUTF8', '1', 'Process') + # 设置控制台编码为UTF-8 + chcp.com 65001 + + - name: Run packaging script + run: | + cd package + python nuitka_build.py nuitka_config.yml + + - name: Upload files with wildcards + uses: actions/upload-artifact@v4 + with: + name: Artifact + path: | + ./dist/*.exe + retention-days: 90 + if-no-files-found: warn + overwrite: true \ No newline at end of file diff --git a/package/config.yml b/package/config.yml index 0c8d4fa..e69de29 100644 --- a/package/config.yml +++ b/package/config.yml @@ -1,104 +0,0 @@ -- name: 'novel_crawler' - version: 'v.25.07.08' - python-file: 'novel_crawler\novel_crawler_v.25.07.08.py' - install-requirements: [ - 'beautifulsoup4', - 'requests', - 'ebooklib', - 'rich' - ] - upx: true - # onefile: 2 # 0:文件夹 1:单文件 2:两者 - icon: - windowed: False - distpath: 'dist' - output-name: '{{name}}_{{version}}_{{os}}_{{arch}}' - -- name: 'Keyboard_monitoring' - version: 'v.24.07.16' - python-file: '键盘监听\v.24.07.16.py' - install-requirements: [ - 'keyboard', - 'configparser', - 'pywin32' - ] - upx: true - # onefile: 1 - icon: - windowed: False - distpath: 'dist' - output-name: '{{name}}_{{version}}_{{os}}_{{arch}}' - -- name: 'NetEase_Cloud_Music_Download' - version: 'v.24-10-06' - python-file: '网易云音乐歌单批量下载歌曲\v.24-10-06.py' - install-requirements: [ - 'quote', - 'requests', - 'tabulate', - 'mutagen' - ] - upx: true - # onefile: 1 - icon: - windowed: False - distpath: 'dist' - output-name: '{{name}}_{{version}}_{{os}}_{{arch}}' - -- name: 'ftp_server' - version: 'v.25-05-02' - python-file: 'ftp_server\ftp_server_v.25-05-02.py' - install-requirements: [ - 'pyftpdlib' - ] - upx: true - # onefile: 1 - icon: - windowed: False - distpath: 'dist' - output-name: '{{name}}_{{version}}_{{os}}_{{arch}}' - -# - name: 'psql_terminal' -# version: -# os: [ -# 'Windows', -# 'Linux' -# ] -# python-file: 'psql_terminal\psql_terminal.py' -# install-requirements: [ -# 'psycopg2' -# ] -# upx: true -# icon: -# windowed: False -# distpath: 'dist' -# output-name: '{{name}}_{{os}}_{{arch}}' - -# - name: 'package' -# version: -# os: [ -# 'Windows', -# 'Linux' -# ] -# python-file: 'package\build.py' -# install-requirements: [ -# 'pyinstaller' -# ] -# upx: true -# icon: -# windowed: False -# distpath: 'dist' -# output-name: '{{name}}_{{version}}_{{os}}_{{arch}}' - -- name: 'sunrise_sunset_info' - version: 'v.25.05.02' - python-file: 'sunrise_sunset_info\sunrise_sunset_info_v.25.05.02.py' - install-requirements: [ - 'pyftpdlib' - ] - upx: true - # onefile: 1 # 0:文件夹 1:单文件 2:两者 - icon: - windowed: False - distpath: 'dist' - output-name: '{{name}}_{{version}}_{{os}}_{{arch}}' diff --git a/package/nuitka_build.py b/package/nuitka_build.py new file mode 100644 index 0000000..35c9a9f --- /dev/null +++ b/package/nuitka_build.py @@ -0,0 +1,132 @@ +import os +import sys +import yaml +import subprocess +import platform +import argparse +from pathlib import Path +# from zip import zip_files_and_folders + +def main(): + print("="*50) + print("="*50) + # 修复编码问题 - 设置UTF-8输出 + sys.stdout.reconfigure(encoding='utf-8') # Python 3.7+ + sys.stderr.reconfigure(encoding='utf-8') + + # 检查Python版本 + if sys.version_info < (3, 7): + print("错误: 请使用Python 3.7或更高版本") + return + + Machine = platform.system() + arrch = platform.machine() + + print(f"当前操作系统: {Machine} {arrch}") + print(f"平台详情: {platform.platform()}") + print(f"Python版本: {sys.version}") + + # 解析命令行参数 + parser = argparse.ArgumentParser(description='PyInstaller打包脚本') + parser.add_argument('config', help='配置文件路径') + args = parser.parse_args() + + # 读取配置文件 + with open(args.config, 'r', encoding='utf-8') as f: + config = yaml.safe_load(f) + + # 基础路径设置 + base_dir = Path(__file__).parent.parent # 项目根目录 + + success_count = 0 + task_error_list = [] + # 遍历所有打包任务 + for i, task in enumerate(config, start=1): + try: + print(f"\n{'='*40}") + print(f"开始打包任务: [{i}/{len(config)}] {task['name']}") + print(f"{'='*40}") + + # 解析任务参数 + python_file = base_dir / task['python-file'] + dist_path = base_dir / task['distpath'] + requirements = task.get('install-requirements', []) + use_upx = task.get('upx', False) + # onefile = task.get('onefile', 0) + icon = task.get('icon') + windows_disable_console = task.get('windows-disable-console', False) + name = task.get('name') + version = task.get('version') + output_name_template = task.get('output-name-template', '{{name}}_{{version}}_{{arch}}_{{os}}') + if arrch == "AMD64": + arrch = "x64" + output_name = output_name_template.replace('{{name}}', name).replace('{{version}}', version).replace('{{arch}}', arrch).replace('{{os}}', Machine) + + # 检查操作系统和架构 + # if Machine not in task.get('os', []): + # print(f"警告: 任务 [{i}/{len(config)} {task['name']}] 不支持当前操作系统 {Machine}") + # continue + # if arrch not in task.get('arch', []): + # print(f"警告: 任务 [{i}/{len(config)} {task['name']}] 不支持当前架构 {arrch}") + # continue + + # 检查Python文件是否存在 + if not python_file.exists(): + print(f"错误: Python文件不存在 {python_file}") + continue + + # 安装依赖 + if requirements: + print(f"安装依赖: {', '.join(requirements)}") + subprocess.run([sys.executable, '-m', 'pip', 'install'] + requirements, check=True) + + # 构建PyInstaller命令 + cmd = [ + sys.executable, '-m', 'nuitka', + f'--output-filename={output_name}.exe', + f'--output-dir={dist_path}', # 输出目录 + '--onefile', # 单文件 + '--standalone', + '--mingw64' + ] + + # 添加窗口模式选项 + if windows_disable_console: + cmd.append('--windows-disable-console') + + # 添加图标选项 + if icon: + icon_path = base_dir / icon + if icon_path.exists(): + cmd.extend(['--windows-icon-from-ico=icon.ico', str(icon_path)]) + else: + print(f"警告: 图标文件不存在 {icon_path}") + + + # 添加主Python文件 + cmd.append(str(python_file)) + + # 打印并执行命令 + print("执行命令:", ' '.join(cmd)) + result = subprocess.run(cmd) + + if result.returncode == 0: + print(f"(onefile)打包成功: {dist_path / output_name}") + success_count += 1 + else: + print(f"(onefile)打包失败,退出码: {result.returncode}") + except Exception as e: + print(f"任务[{i}/{len(config)} {task['name']}]失败: {e}") + task_error_list.append(task['name']) + continue + if success_count != 0: + print(f"打包完成,成功打包 [{success_count}/{len(config)}] 个任务") + if task_error_list != []: + print(f"打包失败的任务: {', '.join(task_error_list)}") + else: + print("打包失败,没有成功打包任何任务") + print(f"失败的任务: {', '.join(task_error_list)}") + sys.exit(0) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/package/nuitka_config.yml b/package/nuitka_config.yml new file mode 100644 index 0000000..0f7966d --- /dev/null +++ b/package/nuitka_config.yml @@ -0,0 +1,15 @@ +- name: 'novel_crawler' + version: 'v.25.07.08' + python-file: 'novel_crawler\novel_crawler_v.25.07.08.py' + install-requirements: [ + 'beautifulsoup4', + 'requests', + 'ebooklib', + 'rich' + ] + upx: true + # onefile: 2 # 0:文件夹 1:单文件 2:两者 + icon: + windows-disable-console: False + distpath: 'dist' + output-name: '{{name}}_{{version}}_nuitka_{{os}}_{{arch}}' From 0d68fae1ac15cea9733c57d71645ef1155c354d4 Mon Sep 17 00:00:00 2001 From: Kissablecho Date: Tue, 8 Jul 2025 03:07:31 +0800 Subject: [PATCH 2/3] Update nuitka_package.yml --- .github/workflows/nuitka_package.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/nuitka_package.yml b/.github/workflows/nuitka_package.yml index 3ae5098..96622f5 100644 --- a/.github/workflows/nuitka_package.yml +++ b/.github/workflows/nuitka_package.yml @@ -35,10 +35,11 @@ jobs: python -m pip install --upgrade pip pip install nuitka pyyaml - - name: Set up MinGW - uses: egor-tensin/setup-mingw@v2 - with: - platform: x64 + - name: insatll mingw + run: | + midir C:\Users\%username%\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\14.2.0posix-19.1.1-12.0.0-msvcrt-r2 + curl -o winlibs-x86_64-posix-seh-gcc-14.2.0-llvm-19.1.1-mingw-w64msvcrt-12.0.0-r2.zip https://github.com/brechtsanders/winlibs_mingw/releases/download/14.2.0posix-19.1.1-12.0.0-msvcrt-r2/winlibs-x86_64-posix-seh-gcc-14.2.0-llvm-19.1.1-mingw-w64msvcrt-12.0.0-r2.zip + tar -xf C:\Users\%username%\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\14.2.0posix-19.1.1-12.0.0-msvcrt-r2\winlibs-x86_64-posix-seh-gcc-14.2.0-llvm-19.1.1-mingw-w64msvcrt-12.0.0-r2.zip -C C:\Users\%username%\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\14.2.0posix-19.1.1-12.0.0-msvcrt-r2\ - name: Fix Python encoding (Windows) run: | @@ -60,4 +61,4 @@ jobs: ./dist/*.exe retention-days: 90 if-no-files-found: warn - overwrite: true \ No newline at end of file + overwrite: true From 9423e6df685de66cdabbe9bf85ca81f118c77594 Mon Sep 17 00:00:00 2001 From: Kissablecho Date: Tue, 8 Jul 2025 03:09:42 +0800 Subject: [PATCH 3/3] Update nuitka_package.yml --- .github/workflows/nuitka_package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nuitka_package.yml b/.github/workflows/nuitka_package.yml index 96622f5..9135274 100644 --- a/.github/workflows/nuitka_package.yml +++ b/.github/workflows/nuitka_package.yml @@ -6,7 +6,7 @@ on: branches: - dev paths: - - 'package/config.yml' + - 'package/nuitka_config.yml' jobs: package-and-release: