Simplify tool: git only stores executable bit

This commit is contained in:
Richard Berger
2020-06-26 17:03:10 -04:00
parent 66271448ae
commit 21462ede4b

View File

@ -10,7 +10,6 @@ import argparse
import stat import stat
DEFAULT_CONFIG = """ DEFAULT_CONFIG = """
permission: "rw-r--r--"
recursive: true recursive: true
include: include:
- cmake/** - cmake/**
@ -36,39 +35,14 @@ patterns:
- "requirements.txt" - "requirements.txt"
""" """
def check_permission(path, mask): def has_executable_bit(path):
st = os.stat(path) st = os.stat(path)
return bool(stat.S_IMODE(st.st_mode) == mask) return bool(st.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
def generate_permission_mask(line):
assert(len(line) == 9)
mask = 0
# USER def get_non_exec_mask(path):
if line[0] == "r": st = os.stat(path)
mask |= stat.S_IRUSR return st.st_mode & ~(stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
if line[1] == "w":
mask |= stat.S_IWUSR
if line[2] == "x":
mask |= stat.S_IXUSR
# GROUP
if line[3] == "r":
mask |= stat.S_IRGRP
if line[4] == "w":
mask |= stat.S_IWGRP
if line[5] == "x":
mask |= stat.S_IXGRP
# OTHER
if line[6] == "r":
mask |= stat.S_IROTH
if line[7] == "w":
mask |= stat.S_IWOTH
if line[8] == "x":
mask |= stat.S_IXOTH
return mask
def check_folder(directory, config, fix=False, verbose=False): def check_folder(directory, config, fix=False, verbose=False):
success = True success = True
@ -79,23 +53,20 @@ def check_folder(directory, config, fix=False, verbose=False):
path = os.path.join(directory, base_path, pattern) path = os.path.join(directory, base_path, pattern)
files += glob.glob(path, recursive=config['recursive']) files += glob.glob(path, recursive=config['recursive'])
mask = generate_permission_mask(config['permission'])
for f in files: for f in files:
path = os.path.normpath(f) path = os.path.normpath(f)
if verbose: if verbose:
print("Checking file:", path) print("Checking file:", path)
ok = check_permission(path, mask) if has_executable_bit(path):
if not ok:
print("[Error] Wrong file permissions @ {}".format(path)) print("[Error] Wrong file permissions @ {}".format(path))
if fix: if fix:
if os.access(path, os.W_OK): if os.access(path, os.W_OK):
print("Changing permissions of file {} to '{}'".format(path, config['permission'])) print("Removing executable bit of file {}".format(path))
os.chmod(path, mask) new_mask = get_non_exec_mask(path)
os.chmod(path, new_mask)
else: else:
print("[Error] Can not write permissions of file {}".format(path)) print("[Error] Can not write permissions of file {}".format(path))
success = False success = False