Break out our chmod while loop into fielutils.py

This commit is contained in:
David Stainton 2016-01-21 19:34:24 +01:00 committed by Brian Warner
parent 51ccec7a33
commit ee44732d03
2 changed files with 23 additions and 12 deletions

View File

@ -593,18 +593,8 @@ class WriteFileMixin(object):
if now is None:
now = time.time()
# ensure parent directory exists
head, tail = os.path.split(abspath_u)
fileutil.make_dirs(head)
os.chmod(head, (~ self._umask) & 0777)
while head and head != os.path.abspath(local_path_u):
head, tail = os.path.split(head)
if head == os.path.abspath(local_path_u):
break
else:
os.chmod(head, (~ self._umask) & 0777)
initial, last = os.path.split(abspath_u)
fileutil.make_dirs_with_absolute_mode(local_path_u, initial, (~ self._umask) & 0777)
fileutil.write(replacement_path_u, file_contents)
os.chmod(replacement_path_u, (~ self._umask) & 0777)

View File

@ -170,6 +170,27 @@ def is_ancestor_path(parent, dirname):
return False
return True
def make_dirs_with_absolute_mode(parent, dirname, mode,):
"""
Make directory `dirname` and chmod it to `mode` afterwards.
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 initial == os.path.abspath(parent):
os.chmod(initial, mode)
else:
return
while initial and initial != os.path.abspath(parent):
initial, last = os.path.split(initial)
if initial == os.path.abspath(parent):
break
else:
os.chmod(initial, mode)
def make_dirs(dirname, mode=0777):
"""
An idempotent version of os.makedirs(). If the dir already exists, do