From 5b318f2dbc7747b3c56dc17458c239e9ebd12a5a Mon Sep 17 00:00:00 2001 From: Maurane GLAUDE Date: Fri, 30 Aug 2024 11:00:31 +0200 Subject: [PATCH] fix(gui.windows): fix shortcut not created --- src/antares_web_installer/app.py | 30 +++++++++++-------- src/antares_web_installer/gui/controller.py | 18 +++++++++++ .../shortcuts/_win32_shell.py | 11 ------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/antares_web_installer/app.py b/src/antares_web_installer/app.py index e9f038b..364b993 100644 --- a/src/antares_web_installer/app.py +++ b/src/antares_web_installer/app.py @@ -21,7 +21,7 @@ from antares_web_installer.shortcuts import create_shortcut, get_desktop # List of files and directories to exclude during installation -COMMON_EXCLUDED_FILES = {"config.prod.yaml", "config.yaml", "examples", "logs", "matrices", "tmp"} +COMMON_EXCLUDED_FILES = {"config.prod.yaml", "config.yaml", "examples", "logs", "matrices", "tmp", "*.zip"} POSIX_EXCLUDED_FILES = COMMON_EXCLUDED_FILES | {"AntaresWebWorker"} WINDOWS_EXCLUDED_FILES = COMMON_EXCLUDED_FILES | {"AntaresWebWorker.exe"} EXCLUDED_FILES = POSIX_EXCLUDED_FILES if os.name == "posix" else WINDOWS_EXCLUDED_FILES @@ -47,6 +47,7 @@ class App: progress: float = dataclasses.field(init=False) nb_steps: int = dataclasses.field(init=False) completed_step: int = dataclasses.field(init=False) + version: str = dataclasses.field(init=False) def __post_init__(self): # Prepare the path to the executable which is located in the target directory @@ -123,15 +124,15 @@ def install_files(self): if self.target_dir.is_dir() and list(self.target_dir.iterdir()): logger.info("Existing files were found. Proceed checking old version...") # check app version - version = self.check_version() - logger.info(f"Old application version : {version}.") + old_version = self.check_version() + logger.info(f"Old application version : {old_version}.") self.update_progress(25) # update config file logger.info("Update configuration file...") src_config_path = self.source_dir.joinpath("config.yaml") target_config_path = self.target_dir.joinpath("config.yaml") - update_config(src_config_path, target_config_path, version) + update_config(src_config_path, target_config_path, old_version) logger.info("Configuration file updated.") self.update_progress(50) @@ -143,8 +144,8 @@ def install_files(self): # check new version of the application logger.info("Check new application version...") - version = self.check_version() - logger.info(f"New application version : {version}.") + self.version = self.check_version() + logger.info(f"New application version : {self.version}.") self.update_progress(100) else: @@ -217,9 +218,12 @@ def create_shortcuts(self): Create a local server icon and a browser icon on desktop and """ # prepare a shortcut into the desktop directory + desktop_path = Path(get_desktop()) + logger.info("Generating server shortcut on desktop...") - shortcut_name = SHORTCUT_NAMES[os.name] - shortcut_path = Path(get_desktop()).joinpath(shortcut_name) + shortcut_name, shortcut_ext = SHORTCUT_NAMES[os.name].split('.') + new_shortcut_name = f"{shortcut_name}-{self.version}.{shortcut_ext}" + shortcut_path = desktop_path.joinpath(new_shortcut_name) # if the shortcut already exists, remove it shortcut_path.unlink(missing_ok=True) @@ -227,7 +231,7 @@ def create_shortcuts(self): # shortcut generation logger.info( - f"Shortcut will be created in {shortcut_path}, " + f"Shortcut {new_shortcut_name} will be created in {shortcut_path}, " f"linked to '{self.server_path}' " f"and located in '{self.target_dir}' directory." ) @@ -242,7 +246,8 @@ def create_shortcuts(self): except com_error as e: raise InstallError("Impossible to create a new shortcut: {}\nSkip shortcut creation".format(e)) else: - logger.info("Server shortcut was successfully created.") + assert shortcut_path in list(desktop_path.iterdir()) + logger.info(f"Server shortcut {shortcut_path} was successfully created.") self.update_progress(100) def start_server(self): @@ -287,8 +292,9 @@ def start_server(self): nb_attempts += 1 if nb_attempts == max_attempts: try: - server_process.wait(timeout=5) - except subprocess.TimeoutExpired as e: + httpx.get("http://localhost:8080/", timeout=1) + except httpx.ConnectTimeout: + logger.error("Impossible to launch Antares Web Server after {nb_attempts} attempts.") raise InstallError(f"Impossible to launch Antares Web Server after {nb_attempts} attempts: {e}") time.sleep(5) diff --git a/src/antares_web_installer/gui/controller.py b/src/antares_web_installer/gui/controller.py index f038c13..218bcc7 100644 --- a/src/antares_web_installer/gui/controller.py +++ b/src/antares_web_installer/gui/controller.py @@ -90,18 +90,36 @@ def init_log_file_handler(self): # initialize file handler logger def init_console_handler(self, callback): + """ + + @param callback: + @return: + """ console_handler = ConsoleHandler(callback) self.logger.addHandler(console_handler) def init_progress_handler(self, callback): + """ + Initialize handler log of progress. + The logs generated will be shown on the window console during installation + @param callback: function that is used to update logs + """ progress_logger = ProgressHandler(callback) self.logger.addHandler(progress_logger) def run(self) -> None: + """ + start program + @return: + """ self.view.update_view() super().run() def install(self, callback: typing.Callable): + """ + Run App.install method + @param callback: function that is used to update logs + """ self.init_log_file_handler() self.logger.debug("file logger initialized.") self.init_console_handler(callback) diff --git a/src/antares_web_installer/shortcuts/_win32_shell.py b/src/antares_web_installer/shortcuts/_win32_shell.py index e1e7c6d..205095a 100644 --- a/src/antares_web_installer/shortcuts/_win32_shell.py +++ b/src/antares_web_installer/shortcuts/_win32_shell.py @@ -56,14 +56,6 @@ def create_shortcut( if isinstance(arguments, str): arguments = [arguments] if arguments else [] - target_parent, target_name = str(target).rsplit("\\", maxsplit=1) - new_target_name = "Antares Web Server.lnk" - new_target = os.path.join(target_parent, new_target_name) - - # remove any existing shortcut - if os.path.exists(new_target): - os.remove(new_target) - wscript = _WSHELL.CreateShortCut(str(target)) wscript.TargetPath = str(exe_path) wscript.Arguments = " ".join(arguments) @@ -74,6 +66,3 @@ def create_shortcut( if icon_path: wscript.IconLocation = str(icon_path) wscript.save() - - # add spaces to shortcut name - os.rename(target, new_target)