Skip to content

Commit

Permalink
Merge pull request #17 from Numerlor/2.0.6
Browse files Browse the repository at this point in the history
2.0.6
  • Loading branch information
Numerlor authored Jan 13, 2022
2 parents 3c1bdb8 + c8f1d4c commit 96d81f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion auto_neutron/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# noinspection PyUnresolvedReferences
from __feature__ import snake_case, true_property # noqa: F401

VERSION = "2.0.5"
VERSION = "2.0.6"
APP = "Auto_Neutron"
ORG = "Numerlor"
APPID = f"{ORG}|{APP}|{VERSION}"
Expand Down
67 changes: 37 additions & 30 deletions auto_neutron/self_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import io
import logging
import os
import shutil
import subprocess # noqa S404
import sys
import typing as t
Expand Down Expand Up @@ -37,14 +37,6 @@
TEMP_NAME = "temp_auto_neutron"
EXECUTABLE_PATH = Path(sys.argv[0])

MOVE_DIRECTORY_SCRIPT = r"""
Start-Sleep -Milliseconds 200
Remove-Item -Recurse "{target_path}"
Move-Item "{temp_path}" "{target_path}"
Start-Process -FilePath "{target_path}\Auto_Neutron.exe" -WorkingDirectory "{target_path}"
Exit
"""


class Updater(QtCore.QObject):
"""Check for a new release, and prompt the user for download if one is found and not skipped."""
Expand Down Expand Up @@ -72,6 +64,13 @@ def check_update(self) -> None:
) # Try to delete old executable
except OSError as e:
log.warning("Unable to delete temp executable.", exc_info=e)
temp_dir = EXECUTABLE_PATH.parent.with_name(TEMP_NAME)

if temp_dir.exists():
try:
shutil.rmtree(temp_dir)
except OSError as e:
log.warning("Unable to delete temp directory files.", exc_info=e)

def _show_ask_dialog(self, release_json: dict[str, t.Any]) -> None:
"""Show the download confirmation dialog."""
Expand Down Expand Up @@ -150,11 +149,13 @@ def _create_new_and_restart(self, reply: QtNetwork.QNetworkReply) -> None:
"""
Create the new executable/directory from the reply data and start it.
In the one dir mode, the directory is downloaded into a temp dir and a powershell script
that deletes the old one, moves the new one into its place and starts the new executable is used.
In the one directory move, the current contents of this directory are moved to the `TEMP_NAME` directory next
to it, and the new contents are unpacked into the original.
For one file, the current executable is renamed to the `TEMP_NAME` name and a new one is created in its place.
For one file, the current executable is renamed to the `TEMP_NAME` name and a new one is created in its place,
no deletion is done in this case and is left up to the new process.
After a successful download and moving, the app immediately exits
and any temp cleanup is left to the new process.
"""
try:
if reply.error() is QtNetwork.QNetworkReply.NetworkError.NoError:
Expand Down Expand Up @@ -183,28 +184,34 @@ def _create_new_and_restart(self, reply: QtNetwork.QNetworkReply) -> None:
self._show_error_window(_("Unable to create new executable: ") + str(e))
return

subprocess.Popen(str(EXECUTABLE_PATH)) # noqa S603
else:
dir_path = EXECUTABLE_PATH.parent
temp_path = dir_path.with_name(TEMP_NAME)

try:
ZipFile(io.BytesIO(download_bytes)).extractall(path=temp_path)
temp_path.mkdir(exist_ok=True)
except OSError as e:
self._show_error_window(_("Unable to extract new directory: ") + str(e))
self._show_error_window(
_("Unable to create temporary directory: ") + str(e)
)
return

for file in dir_path.glob("*"):
try:
shutil.move(file, temp_path)
except OSError as e:
self._show_error_window(
_("Unable to move files into temporary directory: ") + str(e)
)
return

try:
ZipFile(io.BytesIO(download_bytes)).extractall(path=dir_path)
except OSError as e:
self._show_error_window(
_("Unable to extract new release files: ") + str(e)
)
return
subprocess.Popen( # noqa S603, S607
[
"powershell.exe",
"-NonInteractive",
"-NoExit",
"-WindowStyle",
"Normal",
"-c",
MOVE_DIRECTORY_SCRIPT.format(
target_path=dir_path, temp_path=temp_path
),
],
cwd=os.environ["USERPROFILE"],
)

subprocess.Popen(str(EXECUTABLE_PATH)) # noqa S603
QtWidgets.QApplication.instance().exit()
4 changes: 3 additions & 1 deletion auto_neutron/settings/toml_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class TOMLSettings:

def __init__(self, file_path: Path):
self.path = file_path
self.load_from_file()
self._settings_dict = RecursiveDefaultDict()
with suppress(FileNotFoundError):
self.load_from_file()

# region value
@t.overload
Expand Down

0 comments on commit 96d81f6

Please sign in to comment.