Add daira's implementation of make_dirs_with_absolute_mode

This commit is contained in:
David Stainton 2016-02-15 13:21:46 +01:00 committed by Brian Warner
parent 2963e9e50c
commit d2ae4255c8

View File

@ -176,20 +176,25 @@ def make_dirs_with_absolute_mode(parent, dirname, mode):
We chmod all parent directories of `dirname` until we reach
`parent`.
"""
make_dirs(dirname)
os.chmod(dirname, mode)
initial, last = os.path.split(dirname)
if os.path.abspath(initial) == os.path.abspath(parent):
return
else:
os.chmod(initial, mode)
while initial and os.path.abspath(initial) != os.path.abspath(parent):
initial, last = os.path.split(initial)
if os.path.abspath(initial) == os.path.abspath(parent):
break
else:
os.chmod(initial, mode)
precondition_abspath(parent)
precondition_abspath(dirname)
if not is_ancestor_path(parent, dirname):
raise AssertionError("dirname must be a descendant of parent")
make_dirs(dirname)
while dirname != parent:
os.chmod(dirname, mode)
# FIXME: doesn't seem to work on Windows for long paths
old_dirname, dirname = dirname, os.path.dirname(dirname)
_assert(len(dirname) < len(old_dirname), dirname=dirname, old_dirname=old_dirname)
def is_ancestor_path(parent, dirname):
while dirname != parent:
# FIXME: doesn't seem to work on Windows for long paths
old_dirname, dirname = dirname, os.path.dirname(dirname)
if len(dirname) >= len(old_dirname):
return False
return True
def make_dirs(dirname, mode=0777):
"""