From 4d1fa0a4c6b9f75187f9986b47db1ad508b2b84f Mon Sep 17 00:00:00 2001 From: songsong Date: Thu, 1 Oct 2026 15:45:52 +0800 Subject: [PATCH 1/4] fix: ship importable CLI and font resources in wheels --- .github/workflows/main.yml | 8 ++++++-- .github/workflows/package.yml | 20 ++++++++++++++++++++ api/cxsecret_font.py | 8 +++++--- pyproject.toml | 13 ++++++++++++- tests/test_resources.py | 14 ++++++++++++++ 5 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/package.yml create mode 100644 tests/test_resources.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e7238872e..f25f6ea4d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,6 +2,7 @@ name: CI on: push: + pull_request: # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -17,9 +18,9 @@ jobs: name: 'win64' steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v3 + uses: actions/setup-python@v5 with: python-version: '3.13' architecture: ${{matrix.architecture}} @@ -32,6 +33,9 @@ jobs: - name: Build win bin run: pyinstaller -F main.py -n 'chaoxing' --add-data "resource;resource" --hidden-import=chardet + - name: Smoke test executable + run: ./dist/chaoxing.exe --help + - name: Create release package run: | mkdir release diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml new file mode 100644 index 000000000..e1aaf8e2d --- /dev/null +++ b/.github/workflows/package.yml @@ -0,0 +1,20 @@ +name: Installed wheel +on: [push, pull_request, workflow_dispatch] +permissions: + contents: read +jobs: + installed-wheel: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.13' + - run: pip install build + - run: python -m build + - run: pip install dist/*.whl + - name: Run installed package outside checkout + working-directory: /tmp + run: | + chaoxing --help + python -c "from api.cxsecret_font import FontHashDAO; FontHashDAO()" diff --git a/api/cxsecret_font.py b/api/cxsecret_font.py index 1445d1c51..a12ce985a 100644 --- a/api/cxsecret_font.py +++ b/api/cxsecret_font.py @@ -42,10 +42,12 @@ def resource_path(relative_path: str) -> str: # PyInstaller创建临时文件夹,定位路径 base_path = sys._MEIPASS except Exception: - # 非打包环境,使用当前目录 - base_path = os.path.abspath(".") + base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - return os.path.join(base_path, relative_path) + candidate = os.path.join(base_path, relative_path) + if os.path.isfile(candidate): + return candidate + return os.path.join(sys.prefix, "share", "chaoxing", os.path.basename(relative_path)) class FontHashDAO: diff --git a/pyproject.toml b/pyproject.toml index ad5c9e322..4c3fcc4d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,7 @@ +[build-system] +requires = ["setuptools>=68"] +build-backend = "setuptools.build_meta" + [project] name = "chaoxing" version = "3.1.3" @@ -8,7 +12,14 @@ requires-python = ">=3.13,<4.0" dynamic = ["dependencies"] [tool.setuptools] -py-modules = [] +py-modules = ["main"] +packages = ["api"] + +[tool.setuptools.data-files] +"share/chaoxing" = ["config_template.ini", "resource/font_map_table.json"] + +[project.scripts] +chaoxing = "main:main" [tool.setuptools.dynamic] dependencies = {file=["requirements.txt"]} diff --git a/tests/test_resources.py b/tests/test_resources.py new file mode 100644 index 000000000..a74a9a3ac --- /dev/null +++ b/tests/test_resources.py @@ -0,0 +1,14 @@ +import os +import tempfile +import unittest +from api.cxsecret_font import FontHashDAO + +class ResourceTest(unittest.TestCase): + def test_font_mapping_loads_outside_repository(self): + previous = os.getcwd() + try: + with tempfile.TemporaryDirectory() as directory: + os.chdir(directory) + FontHashDAO() + finally: + os.chdir(previous) From d8157488fd99bb1a2c0da46749c1972dd39b6eb1 Mon Sep 17 00:00:00 2001 From: songsong Date: Thu, 1 Oct 2026 15:59:26 +0800 Subject: [PATCH 2/4] fix: use UTF-8 for Windows CLI output --- main.py | 9 +++++++++ tests/test_console.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/test_console.py diff --git a/main.py b/main.py index d284e29e7..784821ec8 100644 --- a/main.py +++ b/main.py @@ -547,7 +547,16 @@ def format_time(num, suffix='', divisor=''): return f"{mins:02d}:{sec:02d}" +def configure_console(): + """Keep Chinese CLI output usable with Windows redirected/frozen streams.""" + if sys.platform == "win32": + for stream in (sys.stdout, sys.stderr): + if stream is not None and hasattr(stream, "reconfigure"): + stream.reconfigure(encoding="utf-8", errors="replace") + + def main(): + configure_console() """主程序入口""" try: # 初始化配置 diff --git a/tests/test_console.py b/tests/test_console.py new file mode 100644 index 000000000..10fd79a74 --- /dev/null +++ b/tests/test_console.py @@ -0,0 +1,15 @@ +import io +import sys +import unittest +from unittest import mock +import main + +class ConsoleTest(unittest.TestCase): + def test_windows_redirected_output_preserves_chinese(self): + raw = io.BytesIO() + output = io.TextIOWrapper(raw, encoding="cp1252") + with mock.patch.object(sys, "platform", "win32"), mock.patch.object(sys, "stdout", output), mock.patch.object(sys, "stderr", output): + main.configure_console() + print("中文帮助") + output.flush() + self.assertEqual(raw.getvalue().decode("utf-8"), "中文帮助\n") From 09e68e5ab1511fee106154091dadca2acd477e45 Mon Sep 17 00:00:00 2001 From: songsong Date: Thu, 1 Oct 2026 16:01:38 +0800 Subject: [PATCH 3/4] fix: locate resources for user installations --- api/cxsecret_font.py | 7 ++++++- tests/test_resources.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/cxsecret_font.py b/api/cxsecret_font.py index a12ce985a..de770ac94 100644 --- a/api/cxsecret_font.py +++ b/api/cxsecret_font.py @@ -9,6 +9,7 @@ import json import os import sys +import site from io import BytesIO from pathlib import Path from typing import Dict, IO, Optional, Union @@ -47,7 +48,11 @@ def resource_path(relative_path: str) -> str: candidate = os.path.join(base_path, relative_path) if os.path.isfile(candidate): return candidate - return os.path.join(sys.prefix, "share", "chaoxing", os.path.basename(relative_path)) + for data_root in (base_path, sys.prefix, site.getuserbase()): + installed = os.path.join(data_root, "share", "chaoxing", os.path.basename(relative_path)) + if os.path.isfile(installed): + return installed + return candidate class FontHashDAO: diff --git a/tests/test_resources.py b/tests/test_resources.py index a74a9a3ac..e8bba3b16 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -1,6 +1,9 @@ import os import tempfile import unittest +from unittest import mock +from pathlib import Path +from api import cxsecret_font from api.cxsecret_font import FontHashDAO class ResourceTest(unittest.TestCase): @@ -12,3 +15,13 @@ def test_font_mapping_loads_outside_repository(self): FontHashDAO() finally: os.chdir(previous) + + def test_user_installation_resources_load_outside_prefix(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + data = root / "user/share/chaoxing" + data.mkdir(parents=True) + (data / "font_map_table.json").write_text('{"example": "synthetic-hash"}') + with mock.patch.object(cxsecret_font, "__file__", str(root / "site/api/module.py")), mock.patch.object(cxsecret_font.sys, "prefix", str(root / "prefix")), mock.patch.object(cxsecret_font.site, "getuserbase", return_value=str(root / "user")): + dao = FontHashDAO() + self.assertEqual(dao.find_char("synthetic-hash"), "example") From abff9064da03a97507d24fa911cb485072d9c5e5 Mon Sep 17 00:00:00 2001 From: songsong Date: Thu, 1 Oct 2026 16:45:58 +0800 Subject: [PATCH 4/4] test: make console encoding check independent of newline translation --- tests/test_console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_console.py b/tests/test_console.py index 10fd79a74..59f93bc3e 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -7,7 +7,7 @@ class ConsoleTest(unittest.TestCase): def test_windows_redirected_output_preserves_chinese(self): raw = io.BytesIO() - output = io.TextIOWrapper(raw, encoding="cp1252") + output = io.TextIOWrapper(raw, encoding="cp1252", newline="\n") with mock.patch.object(sys, "platform", "win32"), mock.patch.object(sys, "stdout", output), mock.patch.object(sys, "stderr", output): main.configure_console() print("中文帮助")