2017-06-02 02:17:32 -04:00
|
|
|
'''
|
|
|
|
Various helper functions and classes
|
|
|
|
'''
|
|
|
|
|
2017-06-03 17:13:45 -04:00
|
|
|
import time, os
|
2017-05-31 00:54:00 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-06-08 01:02:13 -04:00
|
|
|
def waitForPath(path, logger, timeout=30):
|
2017-05-31 00:41:56 -04:00
|
|
|
'''
|
|
|
|
Waits for a path to appear. Useful for procfs and sysfs where devices
|
|
|
|
regularly (dis)appear. Timeout given in seconds
|
|
|
|
'''
|
2017-05-30 01:00:23 -04:00
|
|
|
for i in range(0, timeout):
|
|
|
|
if os.path.exists(path):
|
|
|
|
return
|
|
|
|
time.sleep(1)
|
2017-06-08 01:02:13 -04:00
|
|
|
logger.error('Could not find %s after %s seconds', path, timeout)
|
2017-05-30 02:11:15 -04:00
|
|
|
raise SystemExit
|