From de23d56da6318ddbb61561eca11541747e5091f1 Mon Sep 17 00:00:00 2001 From: Stephen Nicholas Swatman Date: Tue, 17 May 2022 13:51:29 +0200 Subject: [PATCH] feat: Increase robustness of Python setup script (#1249) We've previously run into some minor issues with the Python setup script. This is only natural because shell scripting is hell, but we can still try to make it a little more robust. To this end, this commit adds two new features to the setup script: 1. The setup script now supports ZSH as well as Bash, so it should also work on Mac operating systems. 2. The script now tells the user which version of Python the bindings were set up for, so they don't run into versioning conflicts. --- Examples/Python/CMakeLists.txt | 3 +-- Examples/Python/setup.sh.in | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Examples/Python/CMakeLists.txt b/Examples/Python/CMakeLists.txt index 1e20296f85f..0775640a6a1 100644 --- a/Examples/Python/CMakeLists.txt +++ b/Examples/Python/CMakeLists.txt @@ -145,10 +145,9 @@ else() endif() add_custom_target(ActsPythonGlueCode) -configure_file(setup.sh.in ${_python_dir}/setup.sh COPYONLY) +configure_file(setup.sh.in ${_python_dir}/setup.sh @ONLY) install(FILES setup.sh.in DESTINATION "python" RENAME setup.sh) - foreach(f ${py_files}) set(_target ${_python_dir}/acts/${f}) get_filename_component(_dir ${_target} DIRECTORY) diff --git a/Examples/Python/setup.sh.in b/Examples/Python/setup.sh.in index 87af52b1e2c..3f92bc5a627 100644 --- a/Examples/Python/setup.sh.in +++ b/Examples/Python/setup.sh.in @@ -1,3 +1,28 @@ -python_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +# This file is part of the Acts project. +# +# Copyright (C) 2021-2022 CERN for the benefit of the Acts project +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. -export PYTHONPATH=$python_dir:$PYTHONPATH +# This script sets up the ACTS Python bindings in the environment in a somewhat +# robust way. + +if [ -n "$ZSH_VERSION" ]; then + export PYTHONPATH=${0:a:h}:$PYTHONPATH +elif [ -n "$BASH_VERSION" ]; then + python_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + export PYTHONPATH=$python_dir:$PYTHONPATH +else + # If the current shell is not ZSH or Bash, we can't guarantee that the + # script will work, so we throw an error. + echo "ERROR: neither ZSH nor Bash was detected, other shells are not supported. The environment has not been modified." + exit 1 +fi + +# This message might seem excessive, but the Acts bindings are only installed +# for one Python version, and it is not trivial for the user to find out which. +# Thus, we let them know explicitly so they can more easily debug any potential +# import errors. +echo "INFO: Acts Python @Python_VERSION_MAJOR@.@Python_VERSION_MINOR@ bindings setup complete."