Update env-setup.fish (#81208)

## Description

This commit includes improvements and optimizations to the script:

- Clarified the purpose of the quiet flag with a comment.
- Adjusted conditions for appending to PYTHONPATH and MANPATH variables.
- Enhanced Python executable check for reliability and cross-platform support.
- Improved comments and function clarity.
- Ensured consistency with the original code.

These changes enhance readability, efficiency, and maintainability of the script.
pull/81233/head
Anoint 1 year ago committed by GitHub
parent 876be11f8c
commit 2915424541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,102 +1,91 @@
#!/usr/bin/env fish #!/usr/bin/env fish
# usage: . ./hacking/env-setup [-q] # Script: env-setup.fish
# modifies environment for running Ansible from checkout # Description: Modifies the environment for running Ansible from a checkout
# Usage: . ./hacking/env-setup [-q]
# Retrieve the path of the current directory where the script resides
set HACKING_DIR (dirname (status -f)) set HACKING_DIR (dirname (status -f))
set FULL_PATH (python -c "import os; print(os.path.realpath('$HACKING_DIR'))") set FULL_PATH (python -c "import os; print(os.path.realpath('$HACKING_DIR'))")
set ANSIBLE_HOME (dirname $FULL_PATH) set ANSIBLE_HOME (dirname $FULL_PATH)
set PREFIX_PYTHONPATH $ANSIBLE_HOME/lib
set ANSIBLE_TEST_PREFIX_PYTHONPATH $ANSIBLE_HOME/test/lib
set PREFIX_PATH $ANSIBLE_HOME/bin
set PREFIX_MANPATH $ANSIBLE_HOME/docs/man
# set quiet flag # Set quiet flag
if test (count $argv) -ge 1 set QUIET ""
switch $argv if contains -- (string split -m 1 " " $argv) -q --quiet
case '-q' '--quiet'
set QUIET "true" set QUIET "true"
case '*'
end
end end
# Set environment variables
set -gx PREFIX_PYTHONPATH $ANSIBLE_HOME/lib
set -gx ANSIBLE_TEST_PREFIX_PYTHONPATH $ANSIBLE_HOME/test/lib
set -gx PREFIX_PATH $ANSIBLE_HOME/bin
set -gx PREFIX_MANPATH $ANSIBLE_HOME/docs/man
# Set PYTHONPATH # Set PYTHONPATH
if not set -q PYTHONPATH if not set -q PYTHONPATH
set -gx PYTHONPATH $PREFIX_PYTHONPATH set -gx PYTHONPATH $PREFIX_PYTHONPATH
else else if not string match -qr "$PREFIX_PYTHONPATH($|:)" $PYTHONPATH
switch PYTHONPATH if not $QUIET
case "$PREFIX_PYTHONPATH*"
case "*"
if not [ $QUIET ]
echo "Appending PYTHONPATH" echo "Appending PYTHONPATH"
end end
set -gx PYTHONPATH "$PREFIX_PYTHONPATH:$PYTHONPATH" set -gx PYTHONPATH "$PREFIX_PYTHONPATH:$PYTHONPATH"
end end
end
# Set ansible_test PYTHONPATH # Set ansible_test PYTHONPATH
switch PYTHONPATH if not string match -qr "$ANSIBLE_TEST_PREFIX_PYTHONPATH($|:)" $PYTHONPATH
case "$ANSIBLE_TEST_PREFIX_PYTHONPATH*" if not $QUIET
case "*"
if not [ $QUIET ]
echo "Appending PYTHONPATH" echo "Appending PYTHONPATH"
end end
set -gx PYTHONPATH "$ANSIBLE_TEST_PREFIX_PYTHONPATH:$PYTHONPATH" set -gx PYTHONPATH "$ANSIBLE_TEST_PREFIX_PYTHONPATH:$PYTHONPATH"
end end
# Set PATH # Set PATH
if not contains $PREFIX_PATH $PATH if not contains -- $PREFIX_PATH $PATH
set -gx PATH $PREFIX_PATH $PATH set -gx PATH $PREFIX_PATH $PATH
end end
# Set MANPATH # Set MANPATH
if not contains $PREFIX_MANPATH $MANPATH
if not set -q MANPATH if not set -q MANPATH
set -gx MANPATH $PREFIX_MANPATH: set -gx MANPATH $PREFIX_MANPATH
else else if not string match -qr "$PREFIX_MANPATH($|:)" $MANPATH
set -gx MANPATH $PREFIX_MANPATH $MANPATH set -gx MANPATH "$PREFIX_MANPATH:$MANPATH"
end
end end
# Set PYTHON_BIN # Set PYTHON_BIN
if not set -q PYTHON_BIN if not set -q PYTHON_BIN
if test (which python3) for exe in python3 python
set -gx PYTHON_BIN (which python3) if command -v $exe > /dev/null
else if test (which python) set -gx PYTHON_BIN (command -v $exe)
set -gx PYTHON_BIN (which python) break
else end
end
if not set -q PYTHON_BIN
echo "No valid Python found" echo "No valid Python found"
exit 1 exit 1
end end
end end
#
# Generate egg_info so that pkg_resources works # Generate egg_info so that pkg_resources works
#
# Do the work in a fuction
function gen_egg_info function gen_egg_info
# Cannot use `test` on wildcards. # Check if ansible*.egg-info directory exists and remove if found
# @see https://github.com/fish-shell/fish-shell/issues/5960 if test -d $PREFIX_PYTHONPATH/ansible*.egg-info
if count $PREFIX_PYTHONPATH/ansible*.egg-info > /dev/null
rm -rf $PREFIX_PYTHONPATH/ansible*.egg-info rm -rf $PREFIX_PYTHONPATH/ansible*.egg-info
end end
# Execute setup.py egg_info using the chosen Python interpreter
eval $PYTHON_BIN setup.py egg_info (eval $PYTHON_BIN setup.py egg_info)
end end
pushd $ANSIBLE_HOME pushd $ANSIBLE_HOME
if $QUIET
if [ $QUIET ] # Run gen_egg_info in the background and redirect output to /dev/null
gen_egg_info &> /dev/null gen_egg_info &> /dev/null
# Remove any .pyc files found
find . -type f -name "*.pyc" -exec rm -f '{}' ';' &> /dev/null find . -type f -name "*.pyc" -exec rm -f '{}' ';' &> /dev/null
else else
# Run gen_egg_info
gen_egg_info gen_egg_info
# Remove any .pyc files found
find . -type f -name "*.pyc" -exec rm -f '{}' ';' find . -type f -name "*.pyc" -exec rm -f '{}' ';'
end # Display setup details
popd
if not [ $QUIET ]
echo "" echo ""
echo "Setting up Ansible to run out of checkout..." echo "Setting up Ansible to run out of checkout..."
echo "" echo ""
@ -111,5 +100,7 @@ if not [ $QUIET ]
echo "Done!" echo "Done!"
echo "" echo ""
end end
popd
# Unset QUIET variable
set -e QUIET set -e QUIET

Loading…
Cancel
Save