From 55623e4d92aa31f1148929a2325ee0a6d6934244 Mon Sep 17 00:00:00 2001 From: Umit Kablan Date: Wed, 25 Sep 2024 17:37:14 +0300 Subject: [PATCH] FIX use ".conversion.bak" hardcoded extension for backups --- pleskdistup/actions/distupgrade.py | 14 +++++--------- pleskdistup/common/src/files.py | 9 +++++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/pleskdistup/actions/distupgrade.py b/pleskdistup/actions/distupgrade.py index 41e4921..bffc343 100644 --- a/pleskdistup/actions/distupgrade.py +++ b/pleskdistup/actions/distupgrade.py @@ -1,7 +1,6 @@ # Copyright 2023-2024. WebPros International GmbH. All rights reserved. import os import re -import uuid import subprocess import typing import urllib.request @@ -152,7 +151,6 @@ class SetupAptReposRegexp(action.ActiveAction): to_regexp: str sources_list_path: str sources_list_d_path: str - backup_suffix: str _name: str def __init__( @@ -169,7 +167,6 @@ def __init__( self.sources_list_d_path = sources_list_d_path self._name = name - self.backup_suffix = "_" + str(uuid.uuid4()) @property def name(self): @@ -185,16 +182,16 @@ def _apply_replace_on_file(self, fpath: str, ptrn: re.Pattern, to_regexp: str) - changed = True if not changed: return - files.backup_file(fpath, self.backup_suffix) + files.backup_file(fpath) with open(fpath, 'w') as f: f.writelines(new_lines) def _rm_backups(self): - files.remove_backup(self.sources_list_path, self.backup_suffix, log.debug) + files.remove_backup(self.sources_list_path, log.debug) for root, _, filenames in os.walk(self.sources_list_d_path): for f in filenames: if f.endswith(".list"): - files.remove_backup(os.path.join(root, f), self.backup_suffix, log.debug) + files.remove_backup(os.path.join(root, f), log.debug) def _change_by_regexp(self, from_regexp: str, to_regexp: str) -> None: p = re.compile(from_regexp) @@ -206,12 +203,11 @@ def _change_by_regexp(self, from_regexp: str, to_regexp: str) -> None: self._apply_replace_on_file(os.path.join(root, f), p, to_regexp) def _revert_all(self): - files.restore_file_from_backup(self.sources_list_path, False, self.backup_suffix) + files.restore_file_from_backup(self.sources_list_path, False) for root, _, filenames in os.walk(self.sources_list_d_path): for f in filenames: if f.endswith(".list"): - files.restore_file_from_backup(os.path.join(root, f), - False, self.backup_suffix) + files.restore_file_from_backup(os.path.join(root, f), False) def _prepare_action(self) -> action.ActionResult: self._change_by_regexp(self.from_regexp, self.to_regexp) diff --git a/pleskdistup/common/src/files.py b/pleskdistup/common/src/files.py index 5f3933b..1e6d185 100644 --- a/pleskdistup/common/src/files.py +++ b/pleskdistup/common/src/files.py @@ -59,23 +59,24 @@ def get_last_lines(filename: PathType, n: int) -> typing.List[str]: return f.readlines()[-n:] -def backup_file(filename: str, ext: str = ".bak") -> None: +def backup_file(filename: str, ext: str = ".conversion.bak") -> None: if os.path.exists(filename): shutil.copy(filename, filename + ext) -def backup_exists(filename: str, ext: str = ".bak") -> bool: +def backup_exists(filename: str, ext: str = ".conversion.bak") -> bool: return os.path.exists(filename + ext) -def restore_file_from_backup(filename: str, remove_if_no_backup: bool = False, ext: str = ".bak") -> None: +def restore_file_from_backup(filename: str, remove_if_no_backup: bool = False, + ext: str = ".conversion.bak") -> None: if os.path.exists(filename + ext): shutil.move(filename + ext, filename) elif remove_if_no_backup and os.path.exists(filename): os.remove(filename) -def remove_backup(filename: str, ext: str = ".bak", logf = None) -> None: +def remove_backup(filename: str, logf = None, ext: str = ".conversion.bak") -> None: try: if os.path.exists(filename + ext): os.remove(filename + ext)