Skip to content

Commit

Permalink
fix: alembic files added as data to the package, not as script
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sylvlecl committed Sep 27, 2024
1 parent ddcabd2 commit fad5312
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
27 changes: 17 additions & 10 deletions AntaresWebLinux.spec
Original file line number Diff line number Diff line change
@@ -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={},
Expand All @@ -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,
[],
Expand Down
25 changes: 16 additions & 9 deletions AntaresWebWin.spec
Original file line number Diff line number Diff line change
@@ -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={},
Expand All @@ -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,
[],
Expand Down
15 changes: 13 additions & 2 deletions antarest/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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()

0 comments on commit fad5312

Please sign in to comment.