From 935ac75f6c7d086ce17ed933ed251a1d1ee010ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakobus=20Sch=C3=BCrz?= Date: Sun, 22 Mar 2020 05:09:29 +0100 Subject: [PATCH] add check if mountpoint exists --- workdirfs.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/workdirfs.py b/workdirfs.py index 39c5247..0401f4d 100755 --- a/workdirfs.py +++ b/workdirfs.py @@ -16,6 +16,9 @@ import shutil import json import traceback +import random +import string + from pathlib import Path try: @@ -308,11 +311,22 @@ def main(args): if mountpoint.is_symlink(): mountpoint.unlink(); if mountpoint.exists(): - mountpoint.rename(mountpoint + datetime.now().strftime("%Y-%m-%d") - + ''.join(random.choice( - string.ascii_uppercase + string.digits) for _ in range(6)) - + ".bak") - mountpoint.mkdir() + if mountpoint.is_dir(): + if os.listdir(mountpoint): + mountpoint.rename(str(mountpoint) + "-" + datetime.now().strftime("%Y-%m-%d") + + "-" + + ''.join(random.choice( + string.ascii_uppercase + string.digits) for _ in range(6)) + + ".bak") + else: + mountpoint.rename(str(mountpoint) + "-" + datetime.now().strftime("%Y-%m-%d") + + "-" + + ''.join(random.choice( + string.ascii_uppercase + string.digits) for _ in range(6)) + + ".bak") + + mountpoint.mkdir(mode=0o700, parents=True, exist_ok=True) + # if not (os.path.isdir(mountpoint) or os.path.exists(mountpoint)): # if Path(mountpoint).is_symlink(): # os.path.remove