Reformat files with black

This commit is contained in:
deajan 2025-01-24 19:30:33 +01:00
parent e41bf5294b
commit 73ae70d9f2
4 changed files with 65 additions and 14 deletions

View file

@ -0,0 +1,50 @@
:: Example upgrade script for NPBackup that will be pushed server side
:: The following variables will be overwritten by the upgrade process
:: {CURRENT_DIR} - The current directory of the distribution
:: {backup_dist} - A directory where we try to move / copy the current distribution
:: {upgrade_dist} - The directory where the new distribution is extracted to after download
:: {log_file} - The log file where the output of this script will be written
:: {original_args} - The arguments that were passed to the upgrade script
setlocal EnableDelayedExpansion
echo "Launching upgrade" >> "{log_file}" 2>&1
echo "Moving current dist from {CURRENT_DIR} to {backup_dist}" >> "{log_file}" 2>&1
move /Y "{CURRENT_DIR}" "{backup_dist}" >> "{log_file}" 2>&1
IF !ERRORLEVEL! NEQ 0 (
echo "Moving current dist failed. Trying to copy it." >> "{log_file}" 2>&1
xcopy /S /Y /I "{CURRENT_DIR}\*" "{backup_dist}" >> "{log_file}" 2>&1
echo "Now trying to overwrite current dist with upgrade dist" >> "{log_file}" 2>&1
xcopy /S /Y /I "{upgrade_dist}\*" "{CURRENT_DIR}" >> "{log_file}" 2>&1
set REPLACE_METHD=overwrite
) ELSE (
echo "Moving upgraded dist from {upgrade_dist} to {CURRENT_DIR}" >> "{log_file}" 2>&1
move /Y "{upgrade_dist}" "{CURRENT_DIR}" >> "{log_file}" 2>&1
echo "Copying optional configuration files from {backup_dist} to {CURRENT_DIR}" >> "{log_file}" 2>&1
xcopy /S /Y /I "{backup_dist}\*conf" {CURRENT_DIR} > NUL 2>&1
set REPLACE_METHOD=move
)
echo "Loading new executable {CURRENT_EXECUTABLE} --check-config {original_args}" >> "{log_file}" 2>&1
"{CURRENT_EXECUTABLE}" --check-config {original_args}> "{log_file}" 2>&1
IF !ERRORLEVEL! NEQ 0 (
echo "New executable failed. Rolling back" >> "{log_file}" 2>&1
IF "%REPLACE_METHOD%"=="overwrite" (
echo "Overwrite method used. Overwrite back" >> "{log_file}" 2>&1
xcopy /S /Y /I "{backup_dist}\*" "{CURRENT_DIR}" >> "{log_file}" 2>&1
) ELSE (
echo "Move method used. Move back" >> "{log_file}" 2>&1
rd /S /Q "{CURRENT_DIR}" >> "{log_file}" 2>&1 &
move /Y "{backup_dist}" "{CURRENT_DIR}" >> "{log_file}" 2>&1
)
) ELSE (
echo "Upgrade successful" >> "{log_file}" 2>&1
rd /S /Q "{backup_dist}" >> "{log_file}" 2>&1
:: f'rd /S /Q "{upgrade_dist}" >> "{log_file}" 2>&1 # Since we move this, we don't need to delete it
del /F /S /Q "{downloaded_archive}" >> "{log_file}" 2>&1
echo "Running new version as planned:" >> "{log_file}" 2>&1
echo "{CURRENT_EXECUTABLE} {original_args}" >> "{log_file}" 2>&1
"{CURRENT_EXECUTABLE}" {original_args}'
)
echo "Upgrade script run finished" >> "{log_file}" 2>&1

View file

@ -325,13 +325,15 @@ def auto_upgrader(
try:
# We must replace the script variables with actual values
with open(file_info["script"]["local_fs_path"], "r") as fh:
script_content = fh.read()
script_content.replace("{CURRENT_DIR}", CURRENT_DIR)
script_content.replace("{CURRENT_EXECUTABLE}", CURRENT_EXECUTABLE)
script_content.replace("{upgrade_dist}", upgrade_dist)
script_content.replace("{backup_dist}", backup_dist)
script_content.replace("{log_file}", log_file)
script_content.replace("{original_args}", " ".join(sys.argv[1:]))
script_content = (
fh.read()
.replace("{CURRENT_DIR}", CURRENT_DIR)
.replace("{CURRENT_EXECUTABLE}", CURRENT_EXECUTABLE)
.replace("{upgrade_dist}", upgrade_dist)
.replace("{backup_dist}", backup_dist)
.replace("{log_file}", log_file)
.replace("{original_args}", " ".join(sys.argv[1:]))
)
with open(file_info["script"]["local_fs_path"], "w") as fh:
fh.write(script_content)
except OSError as exc:
@ -422,5 +424,5 @@ def auto_upgrader(
log_file,
)
logger.debug(cmd)
deferred_command(cmd, defer_time=UPGRADE_DEFER_TIME)
# deferred_command(cmd, defer_time=UPGRADE_DEFER_TIME)
sys.exit(0)

View file

@ -76,9 +76,7 @@ else:
try:
if not args.log_file:
logger = logger_get_logger(
config_dict["http_server"]["log_file"], debug=_DEBUG
)
logger = logger_get_logger(config_dict["http_server"]["log_file"], debug=_DEBUG)
except (AttributeError, KeyError, IndexError, TypeError):
pass

View file

@ -37,7 +37,9 @@ def is_enabled(config_dict) -> bool:
return not os.path.isfile(path)
def _get_path_from_target_id(config_dict, target_id: ClientTargetIdentification) -> Tuple[str, str]:
def _get_path_from_target_id(
config_dict, target_id: ClientTargetIdentification
) -> Tuple[str, str]:
"""
Determine specific or generic upgrade path depending on target_id sent by client
@ -122,8 +124,7 @@ def get_current_version(
def get_file(
config_dict: dict,
file: FileGet, content: bool = False
config_dict: dict, file: FileGet, content: bool = False
) -> Optional[Union[FileSend, bytes, bool]]:
_, archive_path, script_path = _get_path_from_target_id(config_dict, file)