summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHarmen Stoppels <harmenstoppels@gmail.com>2022-03-22 23:12:06 +0100
committerGitHub <noreply@github.com>2022-03-22 23:12:06 +0100
commit80195bd1ed8a5a9cafd47920d984d0c6ecc188d6 (patch)
treed168e4858f81966e2a926ef0dfb03bb4e526a496 /lib
parenteda5b854a540cf55037977655b0b77eb92ca5704 (diff)
downloadspack-80195bd1ed8a5a9cafd47920d984d0c6ecc188d6.tar.gz
spack-80195bd1ed8a5a9cafd47920d984d0c6ecc188d6.tar.bz2
spack-80195bd1ed8a5a9cafd47920d984d0c6ecc188d6.tar.xz
spack-80195bd1ed8a5a9cafd47920d984d0c6ecc188d6.zip
sbang.py: single lstat (#29670)
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/hooks/sbang.py20
1 files changed, 9 insertions, 11 deletions
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)