From ea3a36b9de5d91ffc7f4b5bad636b8906e3f5f02 Mon Sep 17 00:00:00 2001 From: Alexander Heidelbach Date: Tue, 27 Aug 2024 12:21:35 +0200 Subject: [PATCH 1/4] Add 'keep-setup' flag to pipe setup command into PATH setup before extracting OLD_* paths --- cvmfs-venv.sh | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cvmfs-venv.sh b/cvmfs-venv.sh index e8e2f69..7bf2be2 100755 --- a/cvmfs-venv.sh +++ b/cvmfs-venv.sh @@ -75,6 +75,10 @@ while [ $# -gt 0 ]; do _setup_command="${2}" shift 2 ;; + --keep-setup) + _keep-setup=false + shift + ;; --no-system-site-packages) _no_system_site_packages=true shift @@ -154,7 +158,6 @@ elif [ -f "/release_setup.sh" ]; then . /release_setup.sh fi -unset _setup_command unset _do_setup_atlas # determine text editor to use for complicated edits to the activate script @@ -185,8 +188,17 @@ if [ ! -d "${_venv_name}" ]; then # for PYTHONHOME to also place the 's site-packages at the front # of PYTHONPATH so that they are ahead of the LCG view's packages in # priority. - _SET_PYTHONPATH=$(cat <<-EOT +_SET_PYTHONPATH=$(cat <<-EOT # Added by https://github.com/matthewfeickert/cvmfs-venv +EOT +) + +if [ "${_keep-setup}" = true ]; then + _SET_PYTHONPATH+=$(eval echo "${_setup_command}") + _SET_PYTHONPATH+=$'\n' +fi + +_SET_PYTHONPATH+=$(cat <<-EOT if [ -n "\${PYTHONPATH:-}" ] ; then _OLD_VIRTUAL_PYTHONPATH="\${PYTHONPATH:-}" unset PYTHONPATH @@ -197,6 +209,8 @@ fi EOT ) + unset _setup_command + # When deactivate is being run, reset the PYTHONPATH to what is was before # activation of the Python virtual environment. This ensures that the 's # site-packages are removed from PYTHONPATH so there is no collision if From 83fe797ca7c715a1d47cebcdaa9649456927bd44 Mon Sep 17 00:00:00 2001 From: Alexander Heidelbach Date: Tue, 27 Aug 2024 12:36:02 +0200 Subject: [PATCH 2/4] Adjust syntax and fix _keep_setup value --- cvmfs-venv.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cvmfs-venv.sh b/cvmfs-venv.sh index 7bf2be2..9e449ab 100755 --- a/cvmfs-venv.sh +++ b/cvmfs-venv.sh @@ -76,7 +76,7 @@ while [ $# -gt 0 ]; do shift 2 ;; --keep-setup) - _keep-setup=false + _keep_setup=true shift ;; --no-system-site-packages) @@ -188,15 +188,16 @@ if [ ! -d "${_venv_name}" ]; then # for PYTHONHOME to also place the 's site-packages at the front # of PYTHONPATH so that they are ahead of the LCG view's packages in # priority. + echo "${_keep_setup}" _SET_PYTHONPATH=$(cat <<-EOT # Added by https://github.com/matthewfeickert/cvmfs-venv EOT ) - -if [ "${_keep-setup}" = true ]; then - _SET_PYTHONPATH+=$(eval echo "${_setup_command}") - _SET_PYTHONPATH+=$'\n' -fi +_SET_PYTHONPATH+=$'\n' + if [ "${_keep_setup}" = true ]; then +_SET_PYTHONPATH+=$(eval echo "${_setup_command}") +_SET_PYTHONPATH+=$'\n' + fi _SET_PYTHONPATH+=$(cat <<-EOT if [ -n "\${PYTHONPATH:-}" ] ; then From 4c97395fa2a0e86929352b45f30a0ac35df6deef Mon Sep 17 00:00:00 2001 From: Alexander Heidelbach Date: Tue, 27 Aug 2024 12:44:08 +0200 Subject: [PATCH 3/4] Add help string for 'keep-setup' flag --- cvmfs-venv.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cvmfs-venv.sh b/cvmfs-venv.sh index 9e449ab..18b21e4 100755 --- a/cvmfs-venv.sh +++ b/cvmfs-venv.sh @@ -10,6 +10,11 @@ Usage: cvmfs-venv [-s|--setup] [--no-system-site-packages] [--no-update] [--no-u Options: -h --help Print this help message -s --setup String of setup options to be parsed + --keep-setup + Keep the setup command in the virtual environment's + activation script. This is useful for keeping the environment + set by the setup command in the virtual environment's + activation script. --no-system-site-packages The venv module '--system-site-packages' option is used by default. While it is not recommended, this behavior can be From a98d3d4b7e5b3dcb4ddc50ec5f27643f8ffce454 Mon Sep 17 00:00:00 2001 From: Alexander Heidelbach Date: Thu, 29 Aug 2024 09:23:13 +0200 Subject: [PATCH 4/4] Add check for correct usage of keep-setup only in combination with a provided setup command --- cvmfs-venv.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cvmfs-venv.sh b/cvmfs-venv.sh index 18b21e4..d9d8898 100755 --- a/cvmfs-venv.sh +++ b/cvmfs-venv.sh @@ -118,6 +118,13 @@ done # FIXME: Find smarter way to filter guard virtual environment creation if [ -z "${_return_break}" ]; then +# Check if the keep-setup flag is set without a setup command +if [ "$_keep_setup" = true ] && [ -z "$_setup_command" ]; then + echo "ERROR: The keep-setup flag is set, but no setup command is provided." + echo " Please provide a setup command with the --setup flag or unset the keep-setup flag." + exit 1 +fi + if [ ! -z "${_setup_command}" ]; then if [ -f "/release_setup.sh" ]; then # If in Linux container @@ -193,7 +200,6 @@ if [ ! -d "${_venv_name}" ]; then # for PYTHONHOME to also place the 's site-packages at the front # of PYTHONPATH so that they are ahead of the LCG view's packages in # priority. - echo "${_keep_setup}" _SET_PYTHONPATH=$(cat <<-EOT # Added by https://github.com/matthewfeickert/cvmfs-venv EOT @@ -216,6 +222,7 @@ EOT ) unset _setup_command + unset _keep_setup # When deactivate is being run, reset the PYTHONPATH to what is was before # activation of the Python virtual environment. This ensures that the 's