Skip to content

Commit

Permalink
fix linux building
Browse files Browse the repository at this point in the history
  • Loading branch information
ElluIFX committed Dec 24, 2024
1 parent 85a02aa commit 1ccb59c
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: sayyid5416/pyinstaller@v1
with:
python_ver: "3.11"
spec: "gui_source/mdp.spec"
spec: "gui_source/mdp_linux.spec"
requirements: "gui_source/requirements_linux.txt"
upload_exe_with_name: "MDP-P906"
compression_level: 9
Expand All @@ -31,7 +31,7 @@ jobs:
uses: sayyid5416/pyinstaller@v1
with:
python_ver: "3.11"
spec: "gui_source/mdp_numba.spec"
spec: "gui_source/mdp_numba_linux.spec"
requirements: "gui_source/requirements_linux.txt"
upload_exe_with_name: "MDP-P906-Numba"
compression_level: 9
Binary file removed gui_source/en_US.qm
Binary file not shown.
106 changes: 93 additions & 13 deletions gui_source/mdp_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,100 @@
qdarktheme.enable_hi_dpi()
app = QtWidgets.QApplication(sys.argv)

# get system language
system_lang = QtCore.QLocale.system().name()
logger.info(f"System language: {system_lang}")
ENGLISH = False
if (
not system_lang.startswith("zh")
or os.environ.get("MDP_FORCE_ENGLISH") == "1"
or "--english" in sys.argv
):
trans = QtCore.QTranslator()
trans.load(os.path.join(ABS_PATH, "en_US.qm"))
app.installTranslator(trans)
ENGLISH = True
from PyQt5.QtCore import QLibraryInfo, QTranslator


def compile_ts_to_qm(ts_file: str, qm_file: str) -> bool:
"""Compile .ts file to .qm file using Qt lrelease"""
try:
from PyQt5.QtCore import QProcess

lrelease_path = os.path.join(
QLibraryInfo.location(QLibraryInfo.LibraryLocation.BinariesPath),
"lrelease",
)
logger.info(f"lrelease path: {lrelease_path}")

if not os.path.exists(lrelease_path):
# Fallback to command line tool if available
logger.warning("lrelease not found, using command line tool")
lrelease_path = "lreleasexxx"

process = QProcess()
process.start(lrelease_path, [ts_file, "-qm", qm_file])
process.waitForFinished()
logger.debug(f"lrelease process exit code: {process.exitCode()}")
return process.exitCode() == 0

except ImportError:
# Pure Python fallback using lupdate module
logger.warning("lrelease not found, fallback to pure python")
try:
import sys

from PyQt5.pylupdate_main import main as lupdate_main

old_argv = sys.argv
sys.argv = ["lrelease", ts_file, "-qm", qm_file]
try:
lupdate_main()
return True
except SystemExit:
return True # lupdate uses sys.exit(0) for success
finally:
sys.argv = old_argv

except ImportError:
logger.error("Could not find Qt lrelease tool or pylupdate module")
return False


def load_language(lang_code: str) -> bool:
"""Load language file dynamically
Args:
lang_code: Language code like 'en_US', 'zh_CN'
Returns:
bool: True if loading succeeded
"""
ts_file = os.path.join(ARG_PATH, f"{lang_code}.ts")
qm_file = os.path.join(ARG_PATH, f"{lang_code}.qm")

if not os.path.exists(ts_file):
logger.error(f"Translation source file not found: {ts_file}")
return ""

# Compile if qm doesn't exist or ts is newer
if (
not os.path.exists(qm_file)
or os.path.getmtime(ts_file) > os.path.getmtime(qm_file)
or True
):
if not compile_ts_to_qm(ts_file, qm_file):
logger.error(f"Failed to compile translation file: {ts_file}")
return ""

return qm_file


# get system language
# system_lang = QtCore.QLocale.system().name()
# logger.info(f"System language: {system_lang}")
# ENGLISH = False
# if (
# not system_lang.startswith("zh")
# or os.environ.get("MDP_FORCE_ENGLISH") == "1"
# or "--english" in sys.argv
# ):
# trans = QtCore.QTranslator()
# trans.load(os.path.join(ABS_PATH, "en_US.qm"))
# app.installTranslator(trans)
# ENGLISH = True
trans = QtCore.QTranslator()
trans.load(load_language("en_US"))
app.installTranslator(trans)
ENGLISH = True
# load custom font
_ = QtGui.QFontDatabase.addApplicationFont(FONT_PATH)
fonts = QtGui.QFontDatabase.applicationFontFamilies(_)
Expand Down
61 changes: 61 additions & 0 deletions gui_source/mdp_linux.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(
["mdp_main.py", "mdp_gui.py"],
pathex=["..", "."], # for github action
binaries=[],
datas=[
("./en_US.qm", "."),
("./SarasaFixedSC-SemiBold.ttf", "."),
("./Li-ion.csv", "."),
],
hiddenimports=["pyi_splash"],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[
"llvm",
"llvmlite",
"numba",
"matplotlib",
], #
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=True,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name="MDP-P906",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
# coll = COLLECT(
# exe,
# a.binaries,
# a.zipfiles,
# a.datas,
# strip=False,
# upx=False,
# upx_exclude=[],
# name="DP100GUI",
# )
56 changes: 56 additions & 0 deletions gui_source/mdp_numba_linux.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(
["mdp_main.py", "mdp_gui.py"],
pathex=["..", "."], # for github action
binaries=[],
datas=[
("./en_US.qm", "."),
("./SarasaFixedSC-SemiBold.ttf", "."),
("./Li-ion.csv", "."),
],
hiddenimports=["pyi_splash"],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=["matplotlib"],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=True,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name="MDP-P906-Numba",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
# coll = COLLECT(
# exe,
# a.binaries,
# a.zipfiles,
# a.datas,
# strip=False,
# upx=False,
# upx_exclude=[],
# name="DP100GUI",
# )

0 comments on commit 1ccb59c

Please sign in to comment.