Skip to content

Commit ae00d91

Browse files
authored
Apply virtualenv and pip improvements to cmake-format.sh (#12704)
Apply the same virtualenv and pip detection improvements from yapf.sh (PR #11755) to cmake-format.sh for better portability across Python environments.
1 parent 3445d95 commit ae00d91

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

tools/cmake-format.sh

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,56 @@ CMAKE_FORMAT_VERSION="v0.6.13"
2626
VERSION="0.6.13"
2727

2828
function main() {
29+
# check for python3
30+
python3 - << _END_
31+
import sys
32+
33+
if sys.version_info.major < 3 or sys.version_info.minor < 8:
34+
exit(1)
35+
_END_
36+
37+
if [ $? = 1 ]; then
38+
echo "Python 3.8 or newer is not installed/enabled."
39+
exit 1
40+
fi
41+
2942
set -e # exit on error
3043

44+
if command -v pip3 &> /dev/null; then
45+
PIP_CMD="pip3"
46+
elif command -v pip &> /dev/null; then
47+
PIP_CMD="pip"
48+
else
49+
echo "pip is not installed."
50+
exit 1
51+
fi
52+
3153
if ! type virtualenv >/dev/null 2>/dev/null
3254
then
33-
pip install -q virtualenv
55+
${PIP_CMD} install -q virtualenv
3456
fi
3557

58+
if python3 -m venv --help &> /dev/null; then
59+
VENV_LIB="venv"
60+
elif python3 -m virtualenv --help &> /dev/null; then
61+
VENV_LIB="virtualenv"
62+
else
63+
echo "Neither venv nor virtualenv is available."
64+
exit 1
65+
fi
66+
67+
68+
REPO_ROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
3669
GIT_DIR=$(git rev-parse --absolute-git-dir)
3770
CMAKE_FORMAT_VENV=${CMAKE_FORMAT_VENV:-${GIT_DIR}/fmt/cmake_format_${CMAKE_FORMAT_VERSION}_venv}
3871
if [ ! -e ${CMAKE_FORMAT_VENV} ]
3972
then
40-
virtualenv ${CMAKE_FORMAT_VENV}
73+
python3 -m ${VENV_LIB} ${CMAKE_FORMAT_VENV}
4174
fi
4275
source ${CMAKE_FORMAT_VENV}/bin/activate
4376

44-
pip install -q --upgrade pip
45-
pip install -q "cmakelang==${CMAKE_FORMAT_VERSION}" pyaml
77+
${PIP_CMD} install -q --upgrade pip
78+
${PIP_CMD} install -q "cmakelang==${CMAKE_FORMAT_VERSION}" pyaml
4679

4780
ver=$(cmake-format --version 2>&1)
4881
if [ "$ver" != "$VERSION" ]

0 commit comments

Comments
 (0)