From 6053096c55f471fad276883d71f04efe1391e196 Mon Sep 17 00:00:00 2001 From: Mudassir Chapra <37051110+muddi900@users.noreply.github.com> Date: Sat, 26 Nov 2022 11:33:07 +0000 Subject: [PATCH 1/2] Issue #611: added option to add timestamp to backups --- cement/utils/fs.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cement/utils/fs.py b/cement/utils/fs.py index d4829cc3..07aa720a 100644 --- a/cement/utils/fs.py +++ b/cement/utils/fs.py @@ -3,6 +3,7 @@ import os import tempfile import shutil +from datetime import datetime class Tmp(object): @@ -170,7 +171,7 @@ def ensure_parent_dir_exists(path): return ensure_dir_exists(parent_dir) -def backup(path, suffix='.bak'): +def backup(path, suffix='.bak', *, add_timestamp=False): """ Rename a file or directory safely without overwriting an existing backup of the same name. @@ -178,6 +179,7 @@ def backup(path, suffix='.bak'): Args: path (str): The path to the file or directory to make a backup of. suffix (str): The suffix to rename files with. + add_timestamp(bool): whether to add a timestamp to the backup suffix Returns: str: The new path of backed up file/directory @@ -194,7 +196,13 @@ def backup(path, suffix='.bak'): count = -1 new_path = None path = abspath(path) + + if add_timestamp: + timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S') + suffix = '-'.join((suffix, timestamp)) + while True: + if os.path.exists(path): if count == -1: new_path = "%s%s" % (path, suffix) From d4e81ab3084ec198a79f0d191b350dd19fe0bdc0 Mon Sep 17 00:00:00 2001 From: Mudassir Chapra <37051110+muddi900@users.noreply.github.com> Date: Sat, 26 Nov 2022 11:39:21 +0000 Subject: [PATCH 2/2] added my name to contributors --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1e7cb198..fd81bc1f 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -19,3 +19,4 @@ documentation, or testing: - Stelios Tymvios (namedLambda) - Spyros Vlachos (devspyrosv) - Joe Roberts (jjroberts) +- Mudassir Chapra(muddi900)