Skip to content

Instantly share code, notes, and snippets.

@bkhanale
Created June 15, 2019 03:34
Show Gist options
  • Save bkhanale/b958f16a1ef983dde7754ce1d110f196 to your computer and use it in GitHub Desktop.
Save bkhanale/b958f16a1ef983dde7754ce1d110f196 to your computer and use it in GitHub Desktop.
import os
import stat
from coalib.bears.LocalBear import LocalBear
from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
class FileModeBear(LocalBear):
def run(self,
filename,
file,
filemode: str,
):
"""
The bear will check if the file has required permissions provided by
the user.
:param filename:
Name of the file that needs to be checked.
:param file:
File that needs to be checked in the form of a list of strings.
:param filemode:
Filemode to check, e.g. `rw`, `rwx`, etc.
"""
st = os.stat(filename)
permissions = {'r': stat.S_IRUSR,
'w': stat.S_IWUSR,
'x': stat.S_IXUSR,
}
for char in filemode:
if char not in permissions:
raise ValueError('Unable to recognize character `{}` in '
'filemode `{}`.'.format(char, filemode))
mode = st.st_mode
for char in filemode:
if not mode & permissions[char]:
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(stat.filemode(
mode)))
return [Result.from_values(origin=self,
message=message,
severity=RESULT_SEVERITY.INFO,
file=filename)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment