From 17234848ac8b8e328661b2f166c395e5ae925125 Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Wed, 31 May 2017 00:54:00 -0400 Subject: [PATCH] move mkdirSafe to auxilary --- auxilary.py | 14 +++++++++++++- sharedLogging.py | 13 ++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/auxilary.py b/auxilary.py index 2bcaccb..7823ac9 100644 --- a/auxilary.py +++ b/auxilary.py @@ -61,7 +61,19 @@ class CountdownTimer(Thread): def __del__(self): self.stop() - + +def mkdirSafe(path, logger): + ''' + Makes new dir if path does not exist, and aborts program if path exists and + path is a file not a dir. Else does nothing + ''' + if not os.path.exists(path): + os.mkdir(path) + elif os.path.isfile(path): + logger.error('%s is present but is a file (vs a directory). ' \ + 'Please (re)move this file to prevent data loss', path) + raise SystemExit + def waitForPath(path, logger=None, timeout=30): ''' Waits for a path to appear. Useful for procfs and sysfs where devices diff --git a/sharedLogging.py b/sharedLogging.py index b20f41f..c9e7fed 100644 --- a/sharedLogging.py +++ b/sharedLogging.py @@ -1,6 +1,7 @@ import logging, os from subprocess import run, PIPE, CalledProcessError from logging.handlers import TimedRotatingFileHandler, SMTPHandler +from auxilary import mkdirSafe ''' Logger conventions @@ -44,16 +45,10 @@ class GlusterFSHandler(TimedRotatingFileHandler): self._volume = volume self._options = options - logdest = mountpoint + '/logs' - - if not os.path.exists(logdest): - os.mkdir(logdest) - elif os.path.isfile(logdest): - logger.error('%s is present but is a file (vs a directory). ' \ - 'Please (re)move this file to prevent data loss', logdest) - raise SystemExit - self._mount() + + logdest = mountpoint + '/logs' + mkdirSafe(logdest, logger) super().__init__(logdest + '/pyledriver-log', when='midnight')