Skip to content

Commit a4990bc

Browse files
committed
add utility func for prepare writeable dir.
1 parent 421a23b commit a4990bc

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

path.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ def unfrackpath(path):
2020
'''
2121
return os.path.normpath(os.path.realpath(os.path.expandvars(os.path.expanduser(path))))
2222

23+
def prepare_writeable_dir(tree, mode=0777):
24+
''' make sure a directory exists and is writeable '''
25+
26+
# modify the mode to ensure the owner at least
27+
# has read/write access to this directory
28+
mode |= 0700
29+
30+
# make sure the tree path is always expanded
31+
# and normalized and free of symlinks
32+
tree = unfrackpath(tree)
33+
34+
if not os.path.exists(tree):
35+
try:
36+
os.makedirs(tree, mode)
37+
except (IOError, OSError), e:
38+
print("Could not make dir %s: %s" % (tree, e))
39+
sys.exit(1)
40+
if not os.access(tree, os.W_OK):
41+
print("Cannot write to path %s" % tree)
42+
sys.exit(1)
43+
return tree
44+
45+
2346
def makedirs_safe(path, mode=None):
2447
'''Safe way to create dirs in muliprocess/thread environments'''
2548
if not os.path.exists(path):

0 commit comments

Comments
 (0)