Skip to content

Commit

Permalink
FIX backup binary and save with .distupg_save suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
ukablan-wpc committed Sep 25, 2024
1 parent be0996b commit e842bfa
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions pleskdistup/actions/distupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit e842bfa

Please sign in to comment.