From 80195bd1ed8a5a9cafd47920d984d0c6ecc188d6 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 22 Mar 2022 23:12:06 +0100 Subject: sbang.py: single lstat (#29670) --- lib/spack/spack/hooks/sbang.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/hooks/sbang.py b/lib/spack/spack/hooks/sbang.py index bef30d2b76..b25ca796a1 100644 --- a/lib/spack/spack/hooks/sbang.py +++ b/lib/spack/spack/hooks/sbang.py @@ -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) -- cgit v1.2.3-60-g2f50