[fix] Create parents directories with proper permissions in mkdir util

This commit is contained in:
Jérôme Lebleu 2016-01-04 21:56:30 +01:00
parent 038491146f
commit 929d96fd93
2 changed files with 21 additions and 7 deletions

View file

@ -8,6 +8,8 @@
"root_required" : "You must be root to perform this action",
"instance_already_running" : "An instance is already running",
"error_see_log" : "An error occured. Please see the log for details.",
"file_exists" : "File already exists: '{path}'",
"folder_exists" : "Folder already exists: '{path}'",
"file_not_exist" : "File does not exist",
"folder_not_exist" : "Folder does not exist",

View file

@ -1,4 +1,5 @@
import os
import errno
import shutil
from pwd import getpwnam
from grp import getgrnam
@ -23,15 +24,26 @@ def mkdir(path, mode=0777, parents=False, uid=None, gid=None, force=False):
"""
if os.path.exists(path) and not force:
return
raise OSError(errno.EEXIST, m18n.g('folder_exists', path=path))
if parents:
os.makedirs(path, mode)
else:
os.mkdir(path, mode)
try:
# Create parents directories as needed
head, tail = os.path.split(path)
if not tail:
head, tail = path.split(head)
if head and tail and not os.path.exists(head):
try:
mkdir(head, mode, parents, uid, gid, force)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if tail == os.curdir:
return
# Create directory and set permissions
os.mkdir(path, mode)
if uid is not None or gid is not None:
chown(path, uid, gid)
except:
pass
def chown(path, uid=None, gid=None, recursive=False):