Skip to content

Commit

Permalink
utilize files.*backup..s although we lost some abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ukablan-wpc committed Sep 25, 2024
1 parent b3a3cb1 commit 33bb2f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
39 changes: 9 additions & 30 deletions pleskdistup/actions/distupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,38 +185,16 @@ def _apply_replace_on_file(self, fpath: str, ptrn: re.Pattern, to_regexp: str) -
changed = True
if not changed:
return
with open(fpath + self.backup_suffix, 'wb') as b:
with open(fpath, 'rb') as f:
b.write(f.read())
files.backup_file(fpath, self.backup_suffix)
with open(fpath, 'w') as f:
f.writelines(new_lines)

def _revert_file(self, fpath: str):
backup_path = fpath + self.backup_suffix
if not os.path.exists(backup_path):
return
with open(backup_path, 'rb') as b:
with open(fpath, 'wb') as f:
f.write(b.read())
os.remove(backup_path)

def _mv_backups(self, new_suffix: str):
backup_path = self.sources_list_path + self.backup_suffix
if os.path.exists(backup_path):
try:
os.rename(backup_path, self.sources_list_path + new_suffix)
except Exception as ex:
log.info(f"Skip failed move backup ({backup_path}): {ex}")
def _rm_backups(self):
files.remove_backup(self.sources_list_path, self.backup_suffix, log.debug)
for root, _, filenames in os.walk(self.sources_list_d_path):
for f in filenames:
if not f.endswith(self.backup_suffix):
continue
backup_path = os.path.join(root, f)
try:
os.rename(backup_path,
backup_path[0:-len(self.backup_suffix)] + new_suffix)
except Exception as ex:
log.info(f"Skip failed move backup ({backup_path}): {ex}")
if f.endswith(".list"):
files.remove_backup(os.path.join(root, f), self.backup_suffix, log.debug)

def _change_by_regexp(self, from_regexp: str, to_regexp: str) -> None:
p = re.compile(from_regexp)
Expand All @@ -228,19 +206,20 @@ 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):
self._revert_file(self.sources_list_path)
files.restore_file_from_backup(self.sources_list_path, False, self.backup_suffix)
for root, _, filenames in os.walk(self.sources_list_d_path):
for f in filenames:
if f.endswith(".list"):
self._revert_file(os.path.join(root, f))
files.restore_file_from_backup(os.path.join(root, f),
False, self.backup_suffix)

def _prepare_action(self) -> action.ActionResult:
self._change_by_regexp(self.from_regexp, self.to_regexp)
packages.update_package_list()
return action.ActionResult()

def _post_action(self) -> action.ActionResult:
self._mv_backups(".distupg_save")
self._rm_backups()
return action.ActionResult()

def _revert_action(self) -> action.ActionResult:
Expand Down
25 changes: 15 additions & 10 deletions pleskdistup/common/src/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,30 @@ def get_last_lines(filename: PathType, n: int) -> typing.List[str]:
return f.readlines()[-n:]


def backup_file(filename: str) -> None:
def backup_file(filename: str, ext: str = ".bak") -> None:
if os.path.exists(filename):
shutil.copy(filename, filename + ".bak")
shutil.copy(filename, filename + ext)


def backup_exists(filename: str) -> bool:
return os.path.exists(filename + ".bak")
def backup_exists(filename: str, ext: str = ".bak") -> bool:
return os.path.exists(filename + ext)


def restore_file_from_backup(filename: str, remove_if_no_backup: bool = False) -> None:
if os.path.exists(filename + ".bak"):
shutil.move(filename + ".bak", filename)
def restore_file_from_backup(filename: str, remove_if_no_backup: bool = False, ext: str = ".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) -> None:
if os.path.exists(filename + ".bak"):
os.remove(filename + ".bak")
def remove_backup(filename: str, ext: str = ".bak", logf = None) -> None:
try:
if os.path.exists(filename + ext):
os.remove(filename + ext)
except Exception as ex:
if logf is None:
raise
logf(f"failed to remove backup ({filename}): {ex}")


def __get_files_recursive(path: str) -> typing.Iterator[str]:
Expand Down

0 comments on commit 33bb2f3

Please sign in to comment.