forked from qt-creator/qt-creator
Change-Id: I459fdaed7338f5343b760da47e9652baf78986f0 Reviewed-by: Robert Loehning <robert.loehning@digia.com>
34 lines
1.3 KiB
Python
34 lines
1.3 KiB
Python
import stat
|
|
|
|
# this function modifies all (regular) files inside the given dirPath to have specified permissions
|
|
# ATTENTION: it won't process the directory recursively
|
|
def changeFilePermissions(dirPath, readPerm, writePerm, excludeFileNames=None):
|
|
permission = 0
|
|
if readPerm:
|
|
permission |= stat.S_IREAD
|
|
if writePerm:
|
|
permission |= stat.S_IWRITE
|
|
if excludeFileNames == None:
|
|
excludeFileNames = []
|
|
elif isinstance(excludeFileNames, (str, unicode)):
|
|
excludeFileNames = [excludeFileNames]
|
|
if not isinstance(excludeFileNames, (tuple, list)):
|
|
test.warning("File names to exclude must be of type str, unicode, list, tuple or None - "
|
|
"ignoring parameter this time.")
|
|
excludeFileNames = []
|
|
if not os.path.isdir(dirPath):
|
|
test.warning("Could not find directory '%s'." % dirPath)
|
|
return False
|
|
filePaths = [os.path.join(dirPath, fileName) for fileName in os.listdir(dirPath)
|
|
if fileName not in excludeFileNames]
|
|
for filePath in filter(os.path.isfile, filePaths):
|
|
os.chmod(filePath, permission)
|
|
return True
|
|
|
|
def isWritable(pathToFile):
|
|
if os.path.exists(pathToFile):
|
|
return os.access(pathToFile, os.W_OK)
|
|
else:
|
|
test.warning("Path to check for writability does not exist.")
|
|
return False
|