Skip to content

Commit

Permalink
FIX use ".conversion.bak" hardcoded extension for backups
Browse files Browse the repository at this point in the history
  • Loading branch information
ukablan-wpc committed Sep 25, 2024
1 parent 33bb2f3 commit 7b859c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
14 changes: 5 additions & 9 deletions pleskdistup/actions/distupgrade.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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__(
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
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))

def _prepare_action(self) -> action.ActionResult:
self._change_by_regexp(self.from_regexp, self.to_regexp)
Expand Down
9 changes: 5 additions & 4 deletions pleskdistup/common/src/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7b859c4

Please sign in to comment.