def chmodR(self, perm, target, writemode):
'''
Recursively apply chmod to a directory
@author: Eric Ball
@param perm: Permissions to be applied. For information on available
permissions/modes, see os.chmod documentation at
https://docs.python.org/2/library/os.html#os.chmod
@param target: Target directory
@param writemode: [a]ppend or [o]verwrite
'''
try:
if not os.path.isdir(target):
raise TypeError(target)
else:
try:
if writemode[0] == "a":
for root, dirs, files in os.walk(target):
# Change permissions for root directory
currentPerm = os.stat(root)[0]
newPerm = currentPerm | perm
os.chmod(root, newPerm)
# Change permissions for child directories
for mydir in dirs:
currentPerm = os.stat(os.path.join(root, mydir))[0]
newPerm = currentPerm | perm
os.chmod(os.path.join(root, mydir), newPerm)
# Change permissions for all files
for myfile in files:
currentPerm = os.stat(os.path.join(root,
myfile))[0]
newPerm = currentPerm | perm
os.chmod(os.path.join(root, myfile), newPerm)
elif writemode[0] == "o":
for root, dirs, files in os.walk(target):
# Change permissions for root directory
os.chmod(root, perm)
# Change permissions for child directories
for mydir in dirs:
os.chmod(os.path.join(root, mydir), perm)
# Change permissions for all files
for myfile in files:
os.chmod(os.path.join(root, myfile), perm)
else:
raise NameError(writemode)
except NameError:
raise
except TypeError:
print "Error: Cannot chmodR target, must be a directory"
raise
except NameError:
print "Error: Invalid writemode specified. Please use [a]ppend " + \
"or [o]verwrite"
raise
except Exception:
raise
评论列表
文章目录