Skip to content

Commit

Permalink
fix(gui.windows): fix shortcut not created
Browse files Browse the repository at this point in the history
  • Loading branch information
maugde committed Aug 30, 2024
1 parent e334bb7 commit 5b318f2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
30 changes: 18 additions & 12 deletions src/antares_web_installer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand Down Expand Up @@ -217,17 +218,20 @@ 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)
self.update_progress(50)

# 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."
)
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions src/antares_web_installer/gui/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 0 additions & 11 deletions src/antares_web_installer/shortcuts/_win32_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 5b318f2

Please sign in to comment.