From e842bfaba5f91587f89a790d0e5e55ce84d11e42 Mon Sep 17 00:00:00 2001 From: Umit Kablan Date: Wed, 25 Sep 2024 11:35:42 +0300 Subject: [PATCH] FIX backup binary and save with .distupg_save suffix --- pleskdistup/actions/distupgrade.py | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pleskdistup/actions/distupgrade.py b/pleskdistup/actions/distupgrade.py index 7ef8ea7..a25eadb 100644 --- a/pleskdistup/actions/distupgrade.py +++ b/pleskdistup/actions/distupgrade.py @@ -176,47 +176,47 @@ def name(self): return self._name.format(self=self) def _apply_replace_on_file(self, fpath: str, ptrn: re.Pattern, to_regexp: str) -> None: - old_lines = [] + changed = False new_lines = [] with open(fpath) as f: for line in f: - old_lines.append(line) new_lines.append(ptrn.sub(to_regexp, line)) - if not [idx for idx, (e1, e2) in enumerate(zip(old_lines, new_lines)) if e1 != e2]: + if new_lines[-1] != line: + changed = True + if not changed: return - with open(fpath + self.backup_suffix, 'w') as f: - f.truncate(0) - f.writelines(old_lines) + with open(fpath + self.backup_suffix, 'wb') as b: + with open(fpath, 'rb') as f: + b.write(f.read()) with open(fpath, 'w') as f: - f.truncate(0) 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) as b: - with open(fpath, 'w') as f: - f.truncate(0) - f.writelines(b.readlines()) + with open(backup_path, 'rb') as b: + with open(fpath, 'wb') as f: + f.write(b.read()) os.remove(backup_path) - def _rm_backups(self): + def _mv_backups(self, new_suffix: str): backup_path = self.sources_list_path + self.backup_suffix if os.file.exists(backup_path): try: - os.remove(backup_path) + os.rename(backup_path, self.sources_list_path + new_suffix) except Exception as ex: - log.info(f"Skip failed remove backup ({backup_path}): {ex}") + log.info(f"Skip failed move backup ({backup_path}): {ex}") 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.remove(backup_path) + os.rename(backup_path, + backup_path[0:-len(self.backup_suffix)] + new_suffix) except Exception as ex: - log.info(f"Skip failed remove backup ({backup_path}): {ex}") + log.info(f"Skip failed move backup ({backup_path}): {ex}") def _change_by_regexp(self, from_regexp: str, to_regexp: str) -> None: p = re.compile(from_regexp) @@ -240,7 +240,7 @@ def _prepare_action(self) -> action.ActionResult: return action.ActionResult() def _post_action(self) -> action.ActionResult: - self._rm_backups() + self._mv_backups(".distupg_save") return action.ActionResult() def _revert_action(self) -> action.ActionResult: