prefer Python 3 to Python 2 for running Spack
The Python landscape is going to be changing in 2020, and Python 2 will
be end of life. Spack should *prefer* Python 3 to Python 2 by default,
but we still need to run on systems that only have Python2 available.
This is trickier than it sounds, as on some systems, the `python` command
is `python2`; on others it's `python3`, and RHEL8 doesn't even have the
`python` command. Instead, it makes you choose `python3` or
`python2`. You can thus no longer make a simple shebang to handle all the
cases.
This commit makes the `spack` script bilingual. It is still valid
Python, but its shebang is `#!/bin/sh`, and it has a tiny bit of shell
code at the beginning to pick the right python and execute itself with
what it finds.
This has a lot of advantages. I think this will help ensure that Spack
works well in Python3 -- there are cases where we've missed things
because Python2 is still the default `python` on most systems. Also,
with this change, you do not lose the ability to execute the `spack`
script directly with a python interpreter. This is useful for forcing
your own version of python, running coverage tools, and running profiling
tools. i.e., these will not break with this change:
```console
$ python2 $(which spack) <args>
$ coverage run $(which spack) <args>
$ pyinstrument $(which spack) <args>
```
These would not work if we split `spack` into a python file and a shell
script (see #11783). So, this gives us the best of both worlds. We get
to control our interpreter *and* remain a mostly pure python executable.
2019-09-29 11:59:02 +08:00
|
|
|
#!/bin/sh
|
|
|
|
# -*- python -*-
|
2014-01-08 17:21:02 +08:00
|
|
|
#
|
2023-01-19 06:30:17 +08:00
|
|
|
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
|
2018-10-08 04:52:23 +08:00
|
|
|
# Spack Project Developers. See the top-level COPYRIGHT file for details.
|
2014-01-08 17:21:02 +08:00
|
|
|
#
|
2018-10-08 04:52:23 +08:00
|
|
|
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
|
|
|
|
prefer Python 3 to Python 2 for running Spack
The Python landscape is going to be changing in 2020, and Python 2 will
be end of life. Spack should *prefer* Python 3 to Python 2 by default,
but we still need to run on systems that only have Python2 available.
This is trickier than it sounds, as on some systems, the `python` command
is `python2`; on others it's `python3`, and RHEL8 doesn't even have the
`python` command. Instead, it makes you choose `python3` or
`python2`. You can thus no longer make a simple shebang to handle all the
cases.
This commit makes the `spack` script bilingual. It is still valid
Python, but its shebang is `#!/bin/sh`, and it has a tiny bit of shell
code at the beginning to pick the right python and execute itself with
what it finds.
This has a lot of advantages. I think this will help ensure that Spack
works well in Python3 -- there are cases where we've missed things
because Python2 is still the default `python` on most systems. Also,
with this change, you do not lose the ability to execute the `spack`
script directly with a python interpreter. This is useful for forcing
your own version of python, running coverage tools, and running profiling
tools. i.e., these will not break with this change:
```console
$ python2 $(which spack) <args>
$ coverage run $(which spack) <args>
$ pyinstrument $(which spack) <args>
```
These would not work if we split `spack` into a python file and a shell
script (see #11783). So, this gives us the best of both worlds. We get
to control our interpreter *and* remain a mostly pure python executable.
2019-09-29 11:59:02 +08:00
|
|
|
# This file is bilingual. The following shell code finds our preferred python.
|
|
|
|
# Following line is a shell no-op, and starts a multi-line Python comment.
|
|
|
|
# See https://stackoverflow.com/a/47886254
|
|
|
|
""":"
|
2021-02-13 02:52:44 +08:00
|
|
|
# prefer SPACK_PYTHON environment variable, python3, python, then python2
|
2021-05-23 06:35:07 +08:00
|
|
|
SPACK_PREFERRED_PYTHONS="python3 python python2 /usr/libexec/platform-python"
|
|
|
|
for cmd in "${SPACK_PYTHON:-}" ${SPACK_PREFERRED_PYTHONS}; do
|
2021-02-13 02:52:44 +08:00
|
|
|
if command -v > /dev/null "$cmd"; then
|
|
|
|
export SPACK_PYTHON="$(command -v "$cmd")"
|
|
|
|
exec "${SPACK_PYTHON}" "$0" "$@"
|
|
|
|
fi
|
prefer Python 3 to Python 2 for running Spack
The Python landscape is going to be changing in 2020, and Python 2 will
be end of life. Spack should *prefer* Python 3 to Python 2 by default,
but we still need to run on systems that only have Python2 available.
This is trickier than it sounds, as on some systems, the `python` command
is `python2`; on others it's `python3`, and RHEL8 doesn't even have the
`python` command. Instead, it makes you choose `python3` or
`python2`. You can thus no longer make a simple shebang to handle all the
cases.
This commit makes the `spack` script bilingual. It is still valid
Python, but its shebang is `#!/bin/sh`, and it has a tiny bit of shell
code at the beginning to pick the right python and execute itself with
what it finds.
This has a lot of advantages. I think this will help ensure that Spack
works well in Python3 -- there are cases where we've missed things
because Python2 is still the default `python` on most systems. Also,
with this change, you do not lose the ability to execute the `spack`
script directly with a python interpreter. This is useful for forcing
your own version of python, running coverage tools, and running profiling
tools. i.e., these will not break with this change:
```console
$ python2 $(which spack) <args>
$ coverage run $(which spack) <args>
$ pyinstrument $(which spack) <args>
```
These would not work if we split `spack` into a python file and a shell
script (see #11783). So, this gives us the best of both worlds. We get
to control our interpreter *and* remain a mostly pure python executable.
2019-09-29 11:59:02 +08:00
|
|
|
done
|
|
|
|
|
|
|
|
echo "==> Error: spack could not find a python interpreter!" >&2
|
|
|
|
exit 1
|
|
|
|
":"""
|
|
|
|
# Line above is a shell no-op, and ends a python multi-line comment.
|
|
|
|
# The code above runs this file with our preferred python interpreter.
|
|
|
|
|
2017-05-09 04:18:29 +08:00
|
|
|
import os
|
2021-08-17 23:52:51 +08:00
|
|
|
import os.path
|
2013-06-03 04:54:46 +08:00
|
|
|
import sys
|
2017-05-09 04:18:29 +08:00
|
|
|
|
2022-11-14 20:11:28 +08:00
|
|
|
min_python3 = (3, 6)
|
2021-03-20 23:01:23 +08:00
|
|
|
|
2022-11-14 20:11:28 +08:00
|
|
|
if sys.version_info[:2] < min_python3:
|
2014-12-20 03:09:37 +08:00
|
|
|
v_info = sys.version_info[:3]
|
2022-11-14 20:11:28 +08:00
|
|
|
msg = "Spack requires Python %d.%d or higher " % min_python3
|
2021-03-20 23:01:23 +08:00
|
|
|
msg += "You are running spack with Python %d.%d.%d." % v_info
|
|
|
|
sys.exit(msg)
|
2013-02-14 09:50:44 +08:00
|
|
|
|
|
|
|
# Find spack's location and its prefix.
|
2017-05-09 04:18:29 +08:00
|
|
|
spack_file = os.path.realpath(os.path.expanduser(__file__))
|
|
|
|
spack_prefix = os.path.dirname(os.path.dirname(spack_file))
|
2013-02-14 09:50:44 +08:00
|
|
|
|
|
|
|
# Allow spack libs to be imported in our scripts
|
2017-05-09 04:18:29 +08:00
|
|
|
spack_lib_path = os.path.join(spack_prefix, "lib", "spack")
|
|
|
|
sys.path.insert(0, spack_lib_path)
|
2016-10-13 09:25:18 +08:00
|
|
|
|
2022-11-08 07:00:22 +08:00
|
|
|
from spack_installable.main import main # noqa: E402
|
2020-11-13 04:26:23 +08:00
|
|
|
|
|
|
|
# Once we've set up the system path, run the spack main method
|
|
|
|
if __name__ == "__main__":
|
2022-11-08 07:00:22 +08:00
|
|
|
sys.exit(main())
|