Files
5ed2b7c6b2e2a5da50c3db9cda9e9b8e4ae4402f
devstack /inc /python

519 lines
17 KiB
Plaintext
Raw Normal View History

#
# **inc/python** - Python-related functions
#
# Support for pip/setuptools interfaces and virtual environments
#
# External functions used:
# - GetOSVersion
# - is_fedora
# - safe_chown
# Save trace setting
INC_PY_TRACE=$(set +o | grep xtrace)
set +o xtrace
# =================
# Joins bash array of extras with commas as expected by other functions
function join_extras {
local IFS=","
echo "$*"
}
# ================
# updates.
function setup_devstack_virtualenv {
# We run devstack out of a global virtualenv.
if [[ ! -d $DEVSTACK_VENV ]] ; then
# Using system site packages to enable nova to use libguestfs.
# This package is currently installed via the distro and not
# available on pypi.
python$PYTHON3_VERSION -m venv --system-site-packages $DEVSTACK_VENV
# https://review.opendev.org/c/openstack/python-openstackclient/+/920001
pip_install -U simplejson
if [[ ":$PATH:" != *":$DEVSTACK_VENV/bin:"* ]] ; then
export PATH="$DEVSTACK_VENV/bin:$PATH"
export PYTHON="$DEVSTACK_VENV/bin/python3"
fi
}
# get_pip_command
function get_pip_command {
die $LINENO "pip python version is not set."
fi
# under any circumstances.
which pip${version} || which pip${version}-python
if [ $? -ne 0 ]; then
}
function get_python_exec_prefix {
xtrace=$(set +o | grep xtrace)
if [[ -z "$os_PACKAGE" ]]; then
GetOSVersion
fi
$xtrace
echo "$DEVSTACK_VENV/bin"
else
echo "/usr/local/bin"
fi
# from the global-requirements specification.
#
# Uses globals ``REQUIREMENTS_DIR``
#
# pip_install_gr packagename
function pip_install_gr {
local name=1ドル
clean_name=$(get_from_global_requirements $name)
}
# from the global-requirements specification with extras.
#
# Uses globals ``REQUIREMENTS_DIR``
#
# pip_install_gr_extras packagename extra1,extra2,...
function pip_install_gr_extras {
local name=1ドル
local extras=2ドル
version_constraints=$(get_version_constraints_from_global_requirements $name)
pip_install $name[$extras]$version_constraints
# enable_python3_package dir [dir ...]
function enable_python3_package {
local xtrace
xtrace=$(set +o | grep xtrace)
set +o xtrace
$xtrace
}
# disable_python3_package dir [dir ...]
function disable_python3_package {
local xtrace
xtrace=$(set +o | grep xtrace)
set +o xtrace
$xtrace
}
# pip_install pip_arguments
if [[ "$offline" == "True" || -z "$@" ]]; then
$xtrace
return
fi
time_start "pip_install"
if [[ "$PIP_UPGRADE" = "True" ]] ; then
upgrade="--upgrade"
fi
GetOSVersion
fi
# Try to extract the path of the package we are installing into
# package_dir. We need this to check for test-requirements.txt,
# at least.
#
# ${!#} expands to the last positional argument to this function.
# With "extras" syntax included, our arguments might be something
# like:
# -e /path/to/fooproject[extra]
# Thus this magic line grabs just the path without extras
#
# Note that this makes no sense if this is a pypi (rather than
# local path) install; ergo you must check this path exists before
# use. Also, if we had multiple or mixed installs, we would also
# likely break. But for historical reasons, it's basically only
# the other wrapper functions in here calling this to install
# local packages, and they do so with single call per install. So
# this works (for now...)
local package_dir=${!#%\[*\]}
local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
# We have to check that the DEVSTACK_VENV exists because early
# devstack boostrapping needs to operate in a system context
# too bootstrap pip. Once pip is bootstrapped we create the
# global venv and can start to use it.
local cmd_pip=$DEVSTACK_VENV/bin/pip
local sudo_pip="env"
echo "Using python $PYTHON3_VERSION to install $package_dir"
# https://github.com/pypa/setuptools/issues/2232
# http://lists.openstack.org/pipermail/openstack-discuss/2020-August/016905.html
# this makes setuptools >=50 use the platform distutils.
# We only want to do this on global pip installs, not if
# installing in a virtualenv
local sudo_pip="sudo -H LC_ALL=en_US.UTF-8 SETUPTOOLS_USE_DISTUTILS=stdlib "
echo "Using python $PYTHON3_VERSION to install $package_dir"
cmd_pip="$cmd_pip -c $REQUIREMENTS_DIR/upper-constraints.txt"
https_proxy="${https_proxy:-}" \
no_proxy="${no_proxy:-}" \
time_stop "pip_install"
[[ "${OFFLINE}" = "True" ]] && return
if [[ -n ${PIP_VIRTUAL_ENV:=} && -d ${PIP_VIRTUAL_ENV} ]]; then
local cmd_pip=$PIP_VIRTUAL_ENV/bin/pip
local sudo_pip="env"
else
local sudo_pip="sudo -H LC_ALL=en_US.UTF-8"
# don't error if we can't uninstall, it might not be there
# get_from_global_requirements <package>
function get_from_global_requirements {
local package=1ドル
required_pkg=$(grep -i -h ^${package} $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
die $LINENO "Can't find package $package in requirements"
fi
echo $required_pkg
}
# get_version_constraints_from_global_requirements <package>
function get_version_constraints_from_global_requirements {
local package=1ドル
local required_pkg_version_constraint
# drop the package name from output (\K)
required_pkg_version_constraint=$(grep -i -h -o -P "^${package}\K.*" $REQUIREMENTS_DIR/global-requirements.txt | cut -d\# -f1)
if [[ $required_pkg_version_constraint == "" ]]; then
die $LINENO "Can't find package $package in requirements"
fi
echo $required_pkg_version_constraint
}
# get pulled in via pip dependencies.
function use_library_from_git {
local name=1ドル
local enabled=1
}
function lib_installed_from_git {
local name=1ドル
safe_name=$(python -c "from pkg_resources import safe_name; \
print(safe_name('${name}'))")
# be smart about finding the remote of the git repo the package
# was installed from. This doesn't work with zuul which clones
# repos with no remote.
#
# The best option seems to be to use "pip list" which will tell
# you the path an editable install was installed from; for example
# in response to something like
# bashate 0.5.2.dev19 /tmp/env/src/bashate
# Thus we check the third column to see if we're installed from
# some local place.
# git, we'll do a git based install, otherwise we'll punt and the
# library should be installed by a requirements pull from another
# project.
function setup_lib {
local name=1ドル
local dir=${GITDIR[$name]}
setup_install $dir
}
# punt and the library should be installed by a requirements pull from
# another project.
#
# use this for non namespaced libraries
if [[ 1ドル == -bindep* ]]; then
bindep="${1}"
shift
fi
local dir=${GITDIR[$name]}
setup_develop $bindep $dir $extras
# this should be used if you want to install globally, all libraries should
# use this, especially *oslo* ones
# setup_install project_dir [extras]
# project_dir: directory of project repo (e.g., /opt/stack/keystone)
# extras: comma-separated list of optional dependencies to install
# (e.g., ldap,memcache).
if [[ 1ドル == -bindep* ]]; then
bindep="${1}"
shift
fi
# this should be used for projects which run services, like all services
# setup_develop project_dir [extras]
# project_dir: directory of project repo (e.g., /opt/stack/keystone)
# extras: comma-separated list of optional dependencies to install
# (e.g., ldap,memcache).
if [[ 1ドル == -bindep* ]]; then
bindep="${1}"
shift
fi
# ``pip install -e`` the package, which processes the dependencies
# using pip before running `setup.py develop`
#
# future installed state of this package. This ensures when we
# install this package we get the from source version.
# project_dir: directory of project repo (e.g., /opt/stack/keystone)
# flags: pip CLI options/flags
# extras: comma-separated list of optional dependencies to install
# (e.g., ldap,memcache).
function _setup_package_with_constraints_edit {
if [[ 1ドル == -bindep* ]]; then
bindep="${1}"
shift
fi
local flags=2ドル
# "installation from path or url cannot be constrained to a version"
# error.
# REVISIT(yamamoto): Remove this when fixed in pip.
# https://github.com/pypa/pip/pull/3582
project_dir=$(cd $project_dir && pwd)
Revert "Workaround for new pip 20.3 behavior" This reverts commit 7a3a7ce876a37376fe0dca7278e41a4f46867daa and bcd0acf6c0b5d6501e91133c3a937b3fc40f7122 and part of f1ed7c77c50ac28cb58c9f7ed885c6a3e0a75403 which all cap our pip installs. Given the pip ecosystem can often incorporate major changes, tracking upstream at least generally gives us one problem at a time to solve rather than trying to handle version jumps when LTS distros update. The new dependency resolver included some changes that disallow setting URL's like "file:///path/to/project#egg=project" in constraints. Apparently the fact it used to work was an accident of the requires/constraints mechanism; it does make some sense as the URL doesn't really have a version-number that the resolver can put in an ordering graph. The _setup_package_with_constraints_edit function comment highlights what this is trying to do # Updates the constraints from REQUIREMENTS_DIR to reflect the # future installed state of this package. This ensures when we # install this package we get the from source version. In other words; if constraints has "foo==1.2.3" and Zuul has checked out "foo" for testing, we have to make sure pip doesn't choose version 1.2.3 from pypi. It seems like removing the entry from upper-requirements.txt is the important part; adding the URL path to the on-disk version was just something that seemed to work at the time, but isn't really necessary. We will install the package in question which will be the latest version (from Zuul checkout) and without the package in upper-requirements.txt nothing will try and downgrade it. Therefore the solution proposed here is to remove the adding of the URL parts. This allows us to uncap pip and restore testing with the new dependency resolver. Closes-Bug: #1906322 Change-Id: Ib9ba52147199a9d6d0293182d5db50c4a567d677
2021年07月28日 11:19:57 +10:00
# Remove this package from constraints before we install it.
# That way, later installs won't "downgrade" the install from
# source we are about to do.
name=$(awk '/^name.*=/ {print 3ドル}' $project_dir/setup.cfg)
name=$(awk '/^name =/ {gsub(/"/, "", 3ドル); print 3ドル}' $project_dir/pyproject.toml)
fi
Revert "Workaround for new pip 20.3 behavior" This reverts commit 7a3a7ce876a37376fe0dca7278e41a4f46867daa and bcd0acf6c0b5d6501e91133c3a937b3fc40f7122 and part of f1ed7c77c50ac28cb58c9f7ed885c6a3e0a75403 which all cap our pip installs. Given the pip ecosystem can often incorporate major changes, tracking upstream at least generally gives us one problem at a time to solve rather than trying to handle version jumps when LTS distros update. The new dependency resolver included some changes that disallow setting URL's like "file:///path/to/project#egg=project" in constraints. Apparently the fact it used to work was an accident of the requires/constraints mechanism; it does make some sense as the URL doesn't really have a version-number that the resolver can put in an ordering graph. The _setup_package_with_constraints_edit function comment highlights what this is trying to do # Updates the constraints from REQUIREMENTS_DIR to reflect the # future installed state of this package. This ensures when we # install this package we get the from source version. In other words; if constraints has "foo==1.2.3" and Zuul has checked out "foo" for testing, we have to make sure pip doesn't choose version 1.2.3 from pypi. It seems like removing the entry from upper-requirements.txt is the important part; adding the URL path to the on-disk version was just something that seemed to work at the time, but isn't really necessary. We will install the package in question which will be the latest version (from Zuul checkout) and without the package in upper-requirements.txt nothing will try and downgrade it. Therefore the solution proposed here is to remove the adding of the URL parts. This allows us to uncap pip and restore testing with the new dependency resolver. Closes-Bug: #1906322 Change-Id: Ib9ba52147199a9d6d0293182d5db50c4a567d677
2021年07月28日 11:19:57 +10:00
$REQUIREMENTS_DIR/upper-constraints.txt -- $name
# correctly. This helps catch errors caused by constraints mismatches.
if use_library_from_git "$project_dir"; then
if ! lib_installed_from_git "$project_dir"; then
die $LINENO "The following LIBS_FROM_GIT was not installed correctly: $project_dir"
fi
fi
# ``pip install -e`` the package, which processes the dependencies
# "pip install <flags> <project_dir>[<extras>]"
# Usage:
# setup_package [-bindep[=profile,profile]] <project_dir> <flags> [extras]
#
# -bindep : Use bindep to install dependencies; select extra profiles
# as comma separated arguments after "="
# project_dir : directory of project repo (e.g., /opt/stack/keystone)
# flags : pip CLI options/flags
# extras : comma-separated list of optional dependencies to install
# (e.g., ldap,memcache).
# See https://docs.openstack.org/pbr/latest/user/using.html#extra-requirements
local bindep_flag=""
local bindep_profiles=""
if [[ 1ドル == -bindep* ]]; then
bindep=1
IFS="=" read bindep_flag bindep_profiles <<< ${1}
shift
fi
local flags=2ドル
# if the flags variable exists, and it doesn't look like a flag,
# assume it's actually the extras list.
if [[ -n "$flags" && -z "$extras" && ! "$flags" =~ ^-.* ]]; then
extras=$flags
flags=""
fi
if [[ ! -z "$extras" ]]; then
extras="[$extras]"
fi
if [[ $bindep == 1 ]]; then
install_bindep $project_dir/bindep.txt $bindep_profiles
fi
fi
}
# TODO(frickler): drop this once all legacy uses are removed
function install_python {
export PYTHON=$(which python${PYTHON3_VERSION} 2>/dev/null)
function install_python3 {
if is_ubuntu; then
install_package python${PYTHON3_VERSION//.}
else
install_package python${PYTHON3_VERSION//.} python${PYTHON3_VERSION//.}-devel
fi
}
# intentionally old to ensure devstack-gate has control
local dstools_version=${DSTOOLS_VERSION:-0.1.2}
install_python3
sudo pip3 install -U devstack-tools==${dstools_version}
}
$INC_PY_TRACE
# Local variables:
# mode: shell-script
# End: