Skip to content

Commit 8e4e3c9

Browse files
authored
python: rework how we compute the "command" property (spack#46850)
Some Windows Python installations may store the Python exe in Scripts/ rather than the base directory. Update `.command` to search in both locations on Windows. On all systems, the search is now done recursively from the search root: on Windows, that is the base install directory, and on other systems it is bin/.
1 parent 6d67992 commit 8e4e3c9

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/spack/llnl/util/filesystem.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"copy_mode",
4848
"filter_file",
4949
"find",
50+
"find_first",
5051
"find_headers",
5152
"find_all_headers",
5253
"find_libraries",

var/spack/repos/builtin/packages/python/package.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -858,14 +858,14 @@ def command(self):
858858
# * python
859859
#
860860
# in that order if using [email protected], for example.
861-
version = self.spec.version
862-
for ver in [version.up_to(2), version.up_to(1), ""]:
863-
if sys.platform != "win32":
864-
path = os.path.join(self.prefix.bin, "python{0}".format(ver))
865-
else:
866-
path = os.path.join(self.prefix, "python{0}.exe".format(ver))
867-
if os.path.exists(path):
868-
return Executable(path)
861+
suffixes = [self.spec.version.up_to(2), self.spec.version.up_to(1), ""]
862+
file_extension = "" if sys.platform != "win32" else ".exe"
863+
patterns = [f"python{ver}{file_extension}" for ver in suffixes]
864+
root = self.prefix.bin if sys.platform != "win32" else self.prefix
865+
path = find_first(root, files=patterns)
866+
867+
if path is not None:
868+
return Executable(path)
869869

870870
else:
871871
# Give a last try at rhel8 platform python
@@ -874,8 +874,9 @@ def command(self):
874874
if os.path.exists(path):
875875
return Executable(path)
876876

877-
msg = "Unable to locate {0} command in {1}"
878-
raise RuntimeError(msg.format(self.name, self.prefix.bin))
877+
raise RuntimeError(
878+
f"cannot to locate the '{self.name}' command in {root} or its subdirectories"
879+
)
879880

880881
@property
881882
def config_vars(self):

0 commit comments

Comments
 (0)