sbang.py: single lstat (#29670)

This commit is contained in:
Harmen Stoppels 2022-03-22 23:12:06 +01:00 committed by GitHub
parent eda5b854a5
commit 80195bd1ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,24 +160,22 @@ def filter_shebang(path):
def filter_shebangs_in_directory(directory, filenames=None):
if filenames is None:
filenames = os.listdir(directory)
is_exe = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
for file in filenames:
path = os.path.join(directory, file)
# only handle files
if not os.path.isfile(path):
# Only look at executable, non-symlink files.
try:
st = os.lstat(path)
except (IOError, OSError):
continue
# only handle executable files
st = os.stat(path)
if not st.st_mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH):
if (stat.S_ISLNK(st.st_mode) or stat.S_ISDIR(st.st_mode) or
not st.st_mode & is_exe):
continue
# only handle links that resolve within THIS package's prefix.
if os.path.islink(path):
real_path = os.path.realpath(path)
if not real_path.startswith(directory + os.sep):
continue
# test the file for a long shebang, and filter
if filter_shebang(path):
tty.debug("Patched overlong shebang in %s" % path)