Skip to content

Commit

Permalink
fix tests and common default extension definition
Browse files Browse the repository at this point in the history
  • Loading branch information
ukablan-wpc committed Sep 26, 2024
1 parent 7b859c4 commit c8b4fd9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pleskdistup/actions/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _prepare_action(self) -> action.ActionResult:
return action.ActionResult()

def _post_action(self) -> action.ActionResult:
path_to_backup = os.path.join(self.temp_directory, "dovecot.conf.bak")
path_to_backup = os.path.join(self.temp_directory, "dovecot.conf" + files.DEFAULT_BACKUP_EXTENSION)
if os.path.exists(self.dovecot_config_path):
shutil.copy(self.dovecot_config_path, path_to_backup)
motd.add_finish_ssh_login_message(f"The dovecot configuration '{self.dovecot_config_path}' has been restored from original distro. Modern configuration was placed in '{path_to_backup}'.\n")
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 @@ -11,6 +11,7 @@

PathType = typing.Union[os.PathLike, str]

DEFAULT_BACKUP_EXTENSION = ".conversion.bak"

def replace_string(filename: str, original_substring: str, new_substring: str) -> None:
with open(filename, "r") as original, open(filename + ".next", "w") as dst:
Expand Down Expand Up @@ -59,24 +60,24 @@ def get_last_lines(filename: PathType, n: int) -> typing.List[str]:
return f.readlines()[-n:]


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


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


def restore_file_from_backup(filename: str, remove_if_no_backup: bool = False,
ext: str = ".conversion.bak") -> None:
ext: str = DEFAULT_BACKUP_EXTENSION) -> 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, logf = None, ext: str = ".conversion.bak") -> None:
def remove_backup(filename: str, logf = None, ext: str = DEFAULT_BACKUP_EXTENSION) -> None:
try:
if os.path.exists(filename + ext):
os.remove(filename + ext)
Expand Down
8 changes: 4 additions & 4 deletions pleskdistup/common/src/motd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def restore_ssh_login_message(motd_path: str = MOTD_PATH) -> None:

def add_inprogress_ssh_login_message(message: str, motd_path: str = MOTD_PATH) -> None:
try:
if not os.path.exists(motd_path + ".bak"):
if not files.backup_exists(motd_path):
if os.path.exists(motd_path):
files.backup_file(motd_path)
else:
with open(motd_path + ".bak", "a") as motd:
with open(motd_path + files.DEFAULT_BACKUP_EXTENSION, "a") as motd:
pass

with open(motd_path, "a") as motd:
Expand All @@ -39,8 +39,8 @@ def add_inprogress_ssh_login_message(message: str, motd_path: str = MOTD_PATH) -
def add_finish_ssh_login_message(message: str, motd_path: str = MOTD_PATH) -> None:
try:
if not os.path.exists(motd_path + ".next"):
if os.path.exists(motd_path + ".bak"):
shutil.copy(motd_path + ".bak", motd_path + ".next")
if os.path.exists(motd_path + files.DEFAULT_BACKUP_EXTENSION):
shutil.copy(motd_path + files.DEFAULT_BACKUP_EXTENSION, motd_path + ".next")

with open(motd_path + ".next", "a") as motd:
motd.write(FINISH_INTRODUCE_MESSAGE)
Expand Down
9 changes: 5 additions & 4 deletions pleskdistup/common/tests/motdtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import tempfile

import src.motd as motd
import src.files


class InprogressSshLoginMessageTests(unittest.TestCase):
def setUp(self):
self.motd_path = tempfile.mktemp()

def tearDown(self):
for path in [self.motd_path, self.motd_path + ".bak"]:
for path in [self.motd_path, self.motd_path + files.DEFAULT_BACKUP_EXTENSION]:
if os.path.exists(path):
os.remove(path)

Expand All @@ -37,7 +38,7 @@ def test_old_backed_up(self):
with open(self.motd_path) as motd_file:
self.assertEqual(motd_file.read(), "old\nnew\n")

with open(self.motd_path + ".bak") as motd_file:
with open(self.motd_path + files.DEFAULT_BACKUP_EXTENSION) as motd_file:
self.assertEqual(motd_file.read(), "old\n")

def test_restore(self):
Expand All @@ -57,7 +58,7 @@ def setUp(self):
self.motd_path = tempfile.mktemp()

def tearDown(self):
for path in [self.motd_path, self.motd_path + ".bak", self.motd_path + ".next"]:
for path in [self.motd_path, self.motd_path + files.DEFAULT_BACKUP_EXTENSION, self.motd_path + ".next"]:
if os.path.exists(path):
os.remove(path)

Expand Down Expand Up @@ -106,7 +107,7 @@ def test_backed_up_message_saved(self):
===============================================================================
""".format(motd.MOTD_PATH)

with open(self.motd_path + ".bak", "w") as motd_file:
with open(self.motd_path + files.DEFAULT_BACKUP_EXTENSION, "w") as motd_file:
motd_file.write("old\n")

motd.add_inprogress_ssh_login_message("new\n", self.motd_path)
Expand Down

0 comments on commit c8b4fd9

Please sign in to comment.