From fad53129ef6174830d0cb6d394b64d6fdc10d04c Mon Sep 17 00:00:00 2001 From: Sylvain Leclerc Date: Fri, 27 Sep 2024 09:39:58 +0200 Subject: [PATCH] fix: alembic files added as data to the package, not as script Otherwise, alembic is actually launched after the gui.py script ends. We still need to analyze dependencies of all alembic scripts though. Also, make the application exit when the server process ends. Signed-off-by: Sylvain Leclerc --- AntaresWebLinux.spec | 27 +++++++++++++++++---------- AntaresWebWin.spec | 25 ++++++++++++++++--------- antarest/gui.py | 15 +++++++++++++-- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/AntaresWebLinux.spec b/AntaresWebLinux.spec index c11ec84a2b..ec5dea60ec 100644 --- a/AntaresWebLinux.spec +++ b/AntaresWebLinux.spec @@ -1,25 +1,27 @@ # -*- mode: python ; coding: utf-8 -*- from pathlib import Path -from PyInstaller.utils.hooks import collect_dynamic_libs block_cipher = None -# We need to analyze all alembic files to be sure the migration phase works fine -migrations_dir = Path('alembic/versions') -migration_files = [str(f) for f in migrations_dir.iterdir() if f.is_file() and f.suffix == '.py'] +# We need to analyze all alembic files to be sure the migration phase works fine: +# alembic loads version files by their path, so we need to add them as "data" to the package, +# but all the dependencies they use need to be included also, wo we need to perform a +# dedicated analyse for this. +versions_dir = Path('alembic/versions') +versions_files = [str(f) for f in versions_dir.iterdir() if f.is_file() and f.suffix == '.py'] +alembic_analysis = Analysis([alembic/env.py] + versions_files) -binaries = [('./alembic.ini', './alembic.ini')] + collect_dynamic_libs('tables') - -antares_web_server_a = Analysis(['antarest/gui.py', 'alembic/env.py'] + migration_files, +antares_web_server_a = Analysis(['antarest/gui.py'], pathex=[], - binaries=binaries, - datas=[('./resources', './resources'), ('./alembic', './alembic')], + binaries=[], + datas=[('./resources', './resources'), ('./alembic', './alembic'), ('./alembic.ini', './')], hiddenimports=[ 'cmath', 'antarest.dbmodel', 'plyer.platforms.linux', 'plyer.platforms.linux.notification', 'pythonjsonlogger.jsonlogger', + 'tables', ], hookspath=['extra-hooks'], hooksconfig={}, @@ -29,8 +31,13 @@ antares_web_server_a = Analysis(['antarest/gui.py', 'alembic/env.py'] + migratio win_private_assemblies=False, cipher=block_cipher, noarchive=False) -antares_web_server_pyz = PYZ(antares_web_server_a.pure, antares_web_server_a.zipped_data, + +all_python = antares_web_server_a.pure + alembic_analysis.pure +all_zipped_data = antares_web_server_a.zipped_data + alembic_analysis.zipped_data + +antares_web_server_pyz = PYZ(all_python, all_zipped_data, cipher=block_cipher) + antares_web_server_exe = EXE(antares_web_server_pyz, antares_web_server_a.scripts, [], diff --git a/AntaresWebWin.spec b/AntaresWebWin.spec index baa0c614db..64f4672ed8 100644 --- a/AntaresWebWin.spec +++ b/AntaresWebWin.spec @@ -1,25 +1,27 @@ # -*- mode: python ; coding: utf-8 -*- from pathlib import Path -from PyInstaller.utils.hooks import collect_dynamic_libs block_cipher = None -# We need to analyze all alembic files to be sure the migration phase works fine -migrations_dir = Path('alembic/versions') -migration_files = [str(f) for f in migrations_dir.iterdir() if f.is_file() and f.suffix == '.py'] +# We need to analyze all alembic files to be sure the migration phase works fine: +# alembic loads version files by their path, so we need to add them as "data" to the package, +# but all the dependencies they use need to be included also, wo we need to perform a +# dedicated analyse for this. +versions_dir = Path('alembic/versions') +versions_files = [str(f) for f in versions_dir.iterdir() if f.is_file() and f.suffix == '.py'] +alembic_analysis = Analysis([alembic/env.py] + versions_files) -binaries = [('./alembic.ini', './alembic.ini')] + collect_dynamic_libs('tables') - -antares_web_server_a = Analysis(['antarest/gui.py', 'alembic/env.py'] + migration_files, +antares_web_server_a = Analysis(['antarest/gui.py'], pathex=[], binaries=binaries, - datas=[('./resources', './resources'), ('./alembic', './alembic')], + datas=[('./resources', './resources'), ('./alembic', './alembic'), ('./alembic.ini', './')], hiddenimports=[ 'cmath', 'antarest.dbmodel', 'plyer.platforms.win', 'plyer.platforms.win.notification', 'pythonjsonlogger.jsonlogger', + 'tables', ], hookspath=['extra-hooks'], hooksconfig={}, @@ -29,8 +31,13 @@ antares_web_server_a = Analysis(['antarest/gui.py', 'alembic/env.py'] + migratio win_private_assemblies=False, cipher=block_cipher, noarchive=False) -antares_web_server_pyz = PYZ(antares_web_server_a.pure, antares_web_server_a.zipped_data, + +all_python = antares_web_server_a.pure + alembic_analysis.pure +all_zipped_data = antares_web_server_a.zipped_data + alembic_analysis.zipped_data + +antares_web_server_pyz = PYZ(all_python, all_zipped_data, cipher=block_cipher) + antares_web_server_exe = EXE(antares_web_server_pyz, antares_web_server_a.scripts, [], diff --git a/antarest/gui.py b/antarest/gui.py index f36904a060..be68413522 100644 --- a/antarest/gui.py +++ b/antarest/gui.py @@ -15,8 +15,8 @@ import platform import time import webbrowser -from multiprocessing import Process from pathlib import Path +from threading import Thread import httpx import uvicorn @@ -88,11 +88,14 @@ def main() -> None: tray.setContextMenu(menu) app.processEvents() tray.setToolTip("AntaresWebServer") - server = Process( + + server = multiprocessing.Process( target=run_server, args=(arguments.config_file,), ) server.start() + Thread(target=monitor_server_process, args=(server, app)).start() + for _ in range(30, 0, -1): with contextlib.suppress(httpx.ConnectError): res = httpx.get("http://localhost:8080") @@ -103,5 +106,13 @@ def main() -> None: server.kill() +def monitor_server_process(server, app) -> None: + """ + Quits the application when server process ends. + """ + server.join() + app.quit() + + if __name__ == "__main__": main()