Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to keep setup command in venv #56

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
31 changes: 29 additions & 2 deletions cvmfs-venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,6 +80,10 @@ while [ $# -gt 0 ]; do
_setup_command="${2}"
shift 2
;;
--keep-setup)
_keep_setup=true
shift
;;
--no-system-site-packages)
_no_system_site_packages=true
shift
Expand Down Expand Up @@ -109,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
Expand Down Expand Up @@ -154,7 +170,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
Expand Down Expand Up @@ -185,8 +200,17 @@ if [ ! -d "${_venv_name}" ]; then
# for PYTHONHOME to also place the <venv>'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
)
_SET_PYTHONPATH+=$'\n'
if [ "${_keep_setup}" = true ]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe what we could do is guard this a bit so that before using _keep_setup we check to see if _setup_command is set and if not error out as the intended use case here is to use --setup <some setup command> --keep-setup.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check for the correct usage directly in the beginning such that the venv is not even created if the intended use case is not met.

_SET_PYTHONPATH+=$(eval echo "${_setup_command}")
_SET_PYTHONPATH+=$'\n'
fi

_SET_PYTHONPATH+=$(cat <<-EOT
if [ -n "\${PYTHONPATH:-}" ] ; then
_OLD_VIRTUAL_PYTHONPATH="\${PYTHONPATH:-}"
unset PYTHONPATH
Expand All @@ -197,6 +221,9 @@ fi
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 <venv>'s
# site-packages are removed from PYTHONPATH so there is no collision if
Expand Down