diff --git a/sdk_container/src/third_party/portage-stable/eclass/distutils-r1.eclass b/sdk_container/src/third_party/portage-stable/eclass/distutils-r1.eclass index d07dfac246..ed368da798 100644 --- a/sdk_container/src/third_party/portage-stable/eclass/distutils-r1.eclass +++ b/sdk_container/src/third_party/portage-stable/eclass/distutils-r1.eclass @@ -1,4 +1,4 @@ -# Copyright 1999-2021 Gentoo Authors +# Copyright 1999-2022 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: distutils-r1.eclass @@ -42,7 +42,7 @@ # relevant to the packages using distutils-r1. # # For more information, please see the Python Guide: -# https://dev.gentoo.org/~mgorny/python-guide/ +# https://projects.gentoo.org/python/guide/ case "${EAPI:-0}" in [0-5]) @@ -55,7 +55,7 @@ case "${EAPI:-0}" in ;; esac -# @ECLASS-VARIABLE: DISTUTILS_OPTIONAL +# @ECLASS_VARIABLE: DISTUTILS_OPTIONAL # @DEFAULT_UNSET # @DESCRIPTION: # If set to a non-null value, distutils part in the ebuild will @@ -67,7 +67,7 @@ esac # distutils-r1 default phase functions or call the build system # manually. -# @ECLASS-VARIABLE: DISTUTILS_SINGLE_IMPL +# @ECLASS_VARIABLE: DISTUTILS_SINGLE_IMPL # @DEFAULT_UNSET # @DESCRIPTION: # If set to a non-null value, the ebuild will support setting a single @@ -78,28 +78,95 @@ esac # to be exported. It must be run in order for the eclass functions # to function properly. -# @ECLASS-VARIABLE: DISTUTILS_USE_SETUPTOOLS +# @ECLASS_VARIABLE: DISTUTILS_USE_PEP517 +# @PRE_INHERIT +# @DEFAULT_UNSET +# @DESCRIPTION: +# Enable the PEP 517 mode for the specified build system. In this mode, +# the complete build and install is done in python_compile(), +# a venv-style install tree is provided to python_test(), +# and python_install() just merges the temporary install tree +# into the real fs. +# +# This mode is recommended for Python packages. However, some packages +# using custom hacks on top of distutils/setuptools may not install +# correctly in this mode. Please verify the list of installed files +# when using it. +# +# The variable specifies the build system used. Currently, +# the following values are supported: +# +# - flit - flit_core backend +# +# - flit_scm - flit_scm backend +# +# - hatchling - hatchling backend (from hatch) +# +# - jupyter - jupyter_packaging backend +# +# - maturin - maturin backend +# +# - pbr - pbr backend +# +# - pdm - pdm.pep517 backend +# +# - poetry - poetry-core backend +# +# - setuptools - distutils or setuptools (incl. legacy mode) +# +# - sip - sipbuild backend +# +# - standalone - standalone build systems without external deps +# (used for bootstrapping). +# +# The variable needs to be set before the inherit line. The eclass +# adds appropriate build-time dependencies and verifies the value. + +# @ECLASS_VARIABLE: DISTUTILS_USE_SETUPTOOLS +# @DEFAULT_UNSET # @PRE_INHERIT # @DESCRIPTION: # Controls adding dev-python/setuptools dependency. The allowed values # are: # # - no -- do not add the dependency (pure distutils package) +# # - bdepend -- add it to BDEPEND (the default) +# # - rdepend -- add it to BDEPEND+RDEPEND (e.g. when using pkg_resources) +# # - pyproject.toml -- use pyproject2setuptools to install a project # using pyproject.toml (flit, poetry...) +# # - manual -- do not add the dependency and suppress the checks # (assumes you will take care of doing it correctly) # # This variable is effective only if DISTUTILS_OPTIONAL is disabled. -# It needs to be set before the inherit line. -: ${DISTUTILS_USE_SETUPTOOLS:=bdepend} +# It is available only in non-PEP517 mode. It needs to be set before +# the inherit line. + +# @ECLASS_VARIABLE: DISTUTILS_DEPS +# @OUTPUT_VARIABLE +# @DESCRIPTION: +# This is an eclass-generated build-time dependency string for the build +# system packages. This string is automatically appended to BDEPEND +# unless DISTUTILS_OPTIONAL is used. This variable is available only +# in PEP 517 mode. +# +# Example use: +# @CODE +# DISTUTILS_OPTIONAL=1 +# # ... +# RDEPEND="${PYTHON_DEPS}" +# BDEPEND=" +# ${PYTHON_DEPS} +# ${DISTUTILS_DEPS}" +# @CODE if [[ ! ${_DISTUTILS_R1} ]]; then [[ ${EAPI} == 6 ]] && inherit eutils xdg-utils -inherit multiprocessing toolchain-funcs +inherit multibuild multiprocessing toolchain-funcs if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then inherit python-r1 @@ -117,25 +184,87 @@ if [[ ! ${_DISTUTILS_R1} ]]; then _distutils_set_globals() { local rdep bdep - local setuptools_dep='>=dev-python/setuptools-42.0.2[${PYTHON_USEDEP}]' + if [[ ${DISTUTILS_USE_PEP517} ]]; then + if [[ ${DISTUTILS_USE_SETUPTOOLS} ]]; then + die "DISTUTILS_USE_SETUPTOOLS is not used in PEP517 mode" + fi - case ${DISTUTILS_USE_SETUPTOOLS} in - no|manual) - ;; - bdepend) - bdep+=" ${setuptools_dep}" - ;; - rdepend) - bdep+=" ${setuptools_dep}" - rdep+=" ${setuptools_dep}" - ;; - pyproject.toml) - bdep+=' >=dev-python/pyproject2setuppy-18[${PYTHON_USEDEP}]' - ;; - *) - die "Invalid DISTUTILS_USE_SETUPTOOLS=${DISTUTILS_USE_SETUPTOOLS}" - ;; - esac + bdep=' + >=dev-python/gpep517-3[${PYTHON_USEDEP}]' + case ${DISTUTILS_USE_PEP517} in + flit) + bdep+=' + >=dev-python/flit_core-3.7.1[${PYTHON_USEDEP}]' + ;; + flit_scm) + bdep+=' + dev-python/flit_scm[${PYTHON_USEDEP}]' + ;; + hatchling) + bdep+=' + >=dev-python/hatchling-0.22.0[${PYTHON_USEDEP}]' + ;; + jupyter) + bdep+=' + >=dev-python/jupyter_packaging-0.11.1[${PYTHON_USEDEP}]' + ;; + maturin) + bdep+=' + >=dev-util/maturin-0.12.7[${PYTHON_USEDEP}]' + ;; + pbr) + bdep+=' + >=dev-python/pbr-5.8.0-r1[${PYTHON_USEDEP}]' + ;; + pdm) + bdep+=' + >=dev-python/pdm-pep517-0.12.3[${PYTHON_USEDEP}]' + ;; + poetry) + bdep+=' + >=dev-python/poetry-core-1.0.8[${PYTHON_USEDEP}]' + ;; + setuptools) + bdep+=' + >=dev-python/setuptools-60.5.0[${PYTHON_USEDEP}] + dev-python/wheel[${PYTHON_USEDEP}]' + ;; + sip) + bdep+=' + >=dev-python/sip-6.5.0-r1[${PYTHON_USEDEP}]' + ;; + standalone) + ;; + *) + die "Unknown DISTUTILS_USE_PEP517=${DISTUTILS_USE_PEP517}" + ;; + esac + elif [[ ${DISTUTILS_OPTIONAL} ]]; then + if [[ ${DISTUTILS_USE_SETUPTOOLS} ]]; then + eqawarn "QA Notice: DISTUTILS_USE_SETUPTOOLS is not used when DISTUTILS_OPTIONAL" + eqawarn "is enabled." + fi + else + local setuptools_dep='>=dev-python/setuptools-42.0.2[${PYTHON_USEDEP}]' + + case ${DISTUTILS_USE_SETUPTOOLS:-bdepend} in + no|manual) + ;; + bdepend) + bdep+=" ${setuptools_dep}" + ;; + rdepend) + bdep+=" ${setuptools_dep}" + rdep+=" ${setuptools_dep}" + ;; + pyproject.toml) + bdep+=' >=dev-python/pyproject2setuppy-22[${PYTHON_USEDEP}]' + ;; + *) + die "Invalid DISTUTILS_USE_SETUPTOOLS=${DISTUTILS_USE_SETUPTOOLS}" + ;; + esac + fi if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then bdep=${bdep//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}} @@ -145,18 +274,34 @@ _distutils_set_globals() { [[ -n ${rdep} ]] && rdep="$(python_gen_cond_dep "${rdep}")" fi - RDEPEND="${PYTHON_DEPS} ${rdep}" - if [[ ${EAPI} != 6 ]]; then - BDEPEND="${PYTHON_DEPS} ${bdep}" - else - DEPEND="${PYTHON_DEPS} ${bdep}" + if [[ ${DISTUTILS_USE_PEP517} ]]; then + if [[ ${DISTUTILS_DEPS+1} ]]; then + if [[ ${DISTUTILS_DEPS} != "${bdep}" ]]; then + eerror "DISTUTILS_DEPS have changed between inherits!" + eerror "Before: ${DISTUTILS_DEPS}" + eerror "Now : ${bdep}" + die "DISTUTILS_DEPS integrity check failed" + fi + else + DISTUTILS_DEPS=${bdep} + readonly DISTUTILS_DEPS + fi + fi + + if [[ ! ${DISTUTILS_OPTIONAL} ]]; then + RDEPEND="${PYTHON_DEPS} ${rdep}" + if [[ ${EAPI} != 6 ]]; then + BDEPEND="${PYTHON_DEPS} ${bdep}" + else + DEPEND="${PYTHON_DEPS} ${bdep}" + fi + REQUIRED_USE=${PYTHON_REQUIRED_USE} fi - REQUIRED_USE=${PYTHON_REQUIRED_USE} } -[[ ! ${DISTUTILS_OPTIONAL} ]] && _distutils_set_globals +_distutils_set_globals unset -f _distutils_set_globals -# @ECLASS-VARIABLE: PATCHES +# @ECLASS_VARIABLE: PATCHES # @DEFAULT_UNSET # @DESCRIPTION: # An array containing patches to be applied to the sources before @@ -172,7 +317,7 @@ unset -f _distutils_set_globals # PATCHES=( "${FILESDIR}"/${P}-make-gentoo-happy.patch ) # @CODE -# @ECLASS-VARIABLE: DOCS +# @ECLASS_VARIABLE: DOCS # @DEFAULT_UNSET # @DESCRIPTION: # An array containing documents installed using dodoc. The files listed @@ -188,7 +333,7 @@ unset -f _distutils_set_globals # DOCS=( NEWS README ) # @CODE -# @ECLASS-VARIABLE: HTML_DOCS +# @ECLASS_VARIABLE: HTML_DOCS # @DEFAULT_UNSET # @DESCRIPTION: # An array containing documents installed using dohtml. The files @@ -202,7 +347,7 @@ unset -f _distutils_set_globals # HTML_DOCS=( doc/html/. ) # @CODE -# @ECLASS-VARIABLE: DISTUTILS_IN_SOURCE_BUILD +# @ECLASS_VARIABLE: DISTUTILS_IN_SOURCE_BUILD # @DEFAULT_UNSET # @DESCRIPTION: # If set to a non-null value, in-source builds will be enabled. @@ -218,7 +363,7 @@ unset -f _distutils_set_globals # 'build --build-base ${BUILD_DIR}' to enforce keeping & using built # files in the specific root. -# @ECLASS-VARIABLE: DISTUTILS_ALL_SUBPHASE_IMPLS +# @ECLASS_VARIABLE: DISTUTILS_ALL_SUBPHASE_IMPLS # @DEFAULT_UNSET # @DESCRIPTION: # An array of patterns specifying which implementations can be used @@ -226,11 +371,8 @@ unset -f _distutils_set_globals # (allowing any implementation). If multiple values are specified, # implementations matching any of the patterns will be accepted. # -# The patterns can be either fnmatch-style patterns (matched via bash -# == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate -# appropriately all enabled Python 2/3 implementations (alike -# python_is_python3). Remember to escape or quote the fnmatch patterns -# to prevent accidental shell filename expansion. +# For the pattern syntax, please see _python_impl_matches +# in python-utils-r1.eclass. # # If the restriction needs to apply conditionally to a USE flag, # the variable should be set conditionally as well (e.g. in an early @@ -249,11 +391,22 @@ unset -f _distutils_set_globals # } # @CODE -# @ECLASS-VARIABLE: DISTUTILS_ARGS +# @ECLASS_VARIABLE: DISTUTILS_ARGS # @DEFAULT_UNSET # @DESCRIPTION: -# An array containing options to be passed to setup.py. They are passed -# before the default arguments, i.e. before the first command. +# An array containing options to be passed to the build system. +# Supported by a subset of build systems used by the eclass. +# +# For setuptools, the arguments will be passed as first parameters +# to setup.py invocations (via esetup.py), as well as to the PEP517 +# backend. For future compatibility, only global options should be used +# and specifying commands should be avoided. +# +# For sip, the options are passed to the PEP517 backend in a form +# resembling sip-build calls. Options taking arguments need to +# be specified in the "--key=value" form, while flag options as "--key". +# If an option takes multiple arguments, it can be specified multiple +# times, same as for sip-build. # # Example: # @CODE @@ -297,7 +450,7 @@ distutils_enable_sphinx() { _DISTUTILS_SPHINX_PLUGINS=( "${@}" ) local deps autodoc=1 d - deps="dev-python/sphinx[\${PYTHON_USEDEP}]" + deps=">=dev-python/sphinx-4.4.0[\${PYTHON_USEDEP}]" for d; do if [[ ${d} == --no-autodoc ]]; then autodoc= @@ -319,13 +472,17 @@ distutils_enable_sphinx() { python_check_deps() { use doc || return 0 + local p - for p in dev-python/sphinx "${_DISTUTILS_SPHINX_PLUGINS[@]}"; do - has_version "${p}[${PYTHON_USEDEP}]" || return 1 + for p in ">=dev-python/sphinx-4.4.0" \ + "${_DISTUTILS_SPHINX_PLUGINS[@]}" + do + python_has_version "${p}[${PYTHON_USEDEP}]" || + return 1 done } else - deps="dev-python/sphinx" + deps=">=dev-python/sphinx-4.4.0" fi sphinx_compile_all() { @@ -393,6 +550,9 @@ distutils_enable_tests() { _DISTUTILS_TEST_INSTALL= case ${1} in --install) + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} --install is not implemented in PEP517 mode" + fi _DISTUTILS_TEST_INSTALL=1 shift ;; @@ -405,7 +565,7 @@ distutils_enable_tests() { test_pkg=">=dev-python/nose-1.3.7-r4" ;; pytest) - test_pkg=">=dev-python/pytest-4.5.0" + test_pkg=">=dev-python/pytest-7.0.1" ;; setup.py) ;; @@ -465,11 +625,18 @@ esetup.py() { _python_check_EPYTHON - [[ ${BUILD_DIR} ]] && _distutils-r1_create_setup_cfg + if [[ ${BUILD_DIR} && ! ${DISTUTILS_USE_PEP517} ]]; then + _distutils-r1_create_setup_cfg + fi local setup_py=( setup.py ) if [[ ${DISTUTILS_USE_SETUPTOOLS} == pyproject.toml ]]; then setup_py=( -m pyproject2setuppy ) + elif [[ ! -f setup.py ]]; then + if [[ ! -f setup.cfg ]]; then + die "${FUNCNAME}: setup.py nor setup.cfg not found" + fi + setup_py=( -c "from setuptools import setup; setup()" ) fi if [[ ${EAPI} != [67] && ${mydistutilsargs[@]} ]]; then @@ -483,7 +650,7 @@ esetup.py() { "${@}" || die -n local ret=${?} - if [[ ${BUILD_DIR} ]]; then + if [[ ${BUILD_DIR} && ! ${DISTUTILS_USE_PEP517} ]]; then rm "${HOME}"/.pydistutils.cfg || die -n fi @@ -521,9 +688,17 @@ esetup.py() { # # Please note that in order to test the solution properly you need # to unmerge the package first. +# +# This function is not available in PEP517 mode. The eclass provides +# a venv-style install unconditionally therefore, and therefore it +# should no longer be necessary. distutils_install_for_testing() { debug-print-function ${FUNCNAME} "${@}" + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is not implemented in PEP517 mode" + fi + # A few notes about --via-home mode: # 1) 'install --home' is terribly broken on pypy, so we need # to override --install-lib and --install-scripts, @@ -554,14 +729,26 @@ distutils_install_for_testing() { local add_args=() if [[ ${install_method} == venv ]]; then - "${EPYTHON}" -m venv --system-site-packages --without-pip \ - "${TEST_DIR}" || die + # create a quasi-venv + mkdir -p "${TEST_DIR}"/bin || die + ln -s "${PYTHON}" "${TEST_DIR}/bin/${EPYTHON}" || die + ln -s "${EPYTHON}" "${TEST_DIR}/bin/python3" || die + ln -s "${EPYTHON}" "${TEST_DIR}/bin/python" || die + cat > "${TEST_DIR}"/pyvenv.cfg <<-EOF || die + include-system-site-packages = true + EOF # we only do the minimal necessary subset of activate script PATH=${TEST_DIR}/bin:${PATH} # unset PYTHONPATH in order to prevent BUILD_DIR from overriding # venv packages unset PYTHONPATH + + # force root-style install (note: venv adds TEST_DIR to prefixes, + # so we need to pass --root=/) + add_args=( + --root=/ + ) else local bindir=${TEST_DIR}/scripts local libdir=${TEST_DIR}/lib @@ -590,12 +777,52 @@ distutils_install_for_testing() { esetup.py install "${add_args[@]}" "${@}" } +# @FUNCTION: distutils_write_namespace +# @USAGE: ... +# @DESCRIPTION: +# Write the __init__.py file for the requested namespace into PEP517 +# install tree, in order to fix running tests when legacy namespace +# packages are installed (dev-python/namespace-*). +# +# This function must only be used in python_test(). The created file +# will automatically be removed upon leaving the test phase. +distutils_write_namespace() { + debug-print-function ${FUNCNAME} "${@}" + + if [[ ! ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is available only in PEP517 mode" + fi + if [[ ${EBUILD_PHASE} != test || ! ${BUILD_DIR} ]]; then + die "${FUNCNAME} should only be used in python_test" + fi + + local namespace + for namespace; do + if [[ ${namespace} == *[./]* ]]; then + die "${FUNCNAME} does not support nested namespaces at the moment" + fi + + local path=${BUILD_DIR}/install$(python_get_sitedir)/${namespace}/__init__.py + if [[ -f ${path} ]]; then + die "Requested namespace ${path} exists already!" + fi + cat > "${path}" <<-EOF || die + __path__ = __import__('pkgutil').extend_path(__path__, __name__) + EOF + _DISTUTILS_POST_PHASE_RM+=( "${path}" ) + done +} + # @FUNCTION: _distutils-r1_disable_ez_setup # @INTERNAL # @DESCRIPTION: # Stub out ez_setup.py and distribute_setup.py to prevent packages # from trying to download a local copy of setuptools. _distutils-r1_disable_ez_setup() { + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is not implemented in PEP517 mode" + fi + local stub="def use_setuptools(*args, **kwargs): pass" if [[ -f ez_setup.py ]]; then echo "${stub}" > ez_setup.py || die @@ -610,6 +837,10 @@ _distutils-r1_disable_ez_setup() { # @DESCRIPTION: # Generate setup.py for pyproject.toml if requested. _distutils-r1_handle_pyproject_toml() { + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is not implemented in PEP517 mode" + fi + [[ ${DISTUTILS_USE_SETUPTOOLS} == manual ]] && return if [[ ! -f setup.py && -f pyproject.toml ]]; then @@ -637,6 +868,102 @@ _distutils-r1_check_all_phase_mismatch() { fi } +# @FUNCTION: _distutils-r1_print_package_versions +# @INTERNAL +# @DESCRIPTION: +# Print the version of the relevant build system packages to aid +# debugging. +_distutils-r1_print_package_versions() { + local packages=() + + if [[ ${DISTUTILS_USE_PEP517} ]]; then + packages+=( + dev-python/gpep517 + dev-python/installer + ) + case ${DISTUTILS_USE_PEP517} in + flit) + packages+=( + dev-python/flit_core + ) + ;; + flit_scm) + packages+=( + dev-python/flit_core + dev-python/flit_scm + dev-python/setuptools_scm + ) + ;; + hatchling) + packages+=( + dev-python/hatchling + ) + ;; + jupyter) + packages+=( + dev-python/jupyter_packaging + dev-python/setuptools + dev-python/setuptools_scm + dev-python/wheel + ) + ;; + maturin) + packages+=( + dev-util/maturin + ) + ;; + pbr) + packages+=( + dev-python/pbr + dev-python/setuptools + dev-python/wheel + ) + ;; + pdm) + packages+=( + dev-python/pdm-pep517 + dev-python/setuptools + ) + ;; + poetry) + packages+=( + dev-python/poetry-core + ) + ;; + setuptools) + packages+=( + dev-python/setuptools + dev-python/setuptools_scm + dev-python/wheel + ) + ;; + sip) + packages+=( + dev-python/sip + ) + ;; + esac + else + case ${DISTUTILS_USE_SETUPTOOLS} in + manual|no) + return + ;; + *) + packages+=( + dev-python/setuptools + ) + ;; + esac + fi + + local pkg + einfo "Build system packages:" + for pkg in "${packages[@]}"; do + local installed=$(best_version "${pkg}") + einfo " $(printf '%-30s' "${pkg}"): ${installed#${pkg}-}" + done +} + # @FUNCTION: distutils-r1_python_prepare_all # @DESCRIPTION: # The default python_prepare_all(). It applies the patches from PATCHES @@ -660,8 +987,19 @@ distutils-r1_python_prepare_all() { fi fi - _distutils-r1_disable_ez_setup - _distutils-r1_handle_pyproject_toml + if [[ ! ${DISTUTILS_USE_PEP517} ]]; then + _distutils-r1_disable_ez_setup + _distutils-r1_handle_pyproject_toml + + case ${DISTUTILS_USE_SETUPTOOLS} in + no) + eqawarn "Non-PEP517 builds are deprecated for ebuilds using plain distutils." + eqawarn "Please migrate to DISTUTILS_USE_PEP517=setuptools." + eqawarn "Please see Python Guide for more details:" + eqawarn " https://projects.gentoo.org/python/guide/distutils.html" + ;; + esac + fi if [[ ${DISTUTILS_IN_SOURCE_BUILD} && ! ${DISTUTILS_SINGLE_IMPL} ]] then @@ -669,6 +1007,10 @@ distutils-r1_python_prepare_all() { python_copy_sources fi + python_export_utf8_locale + [[ ${EAPI} == 6 ]] && xdg_environment_reset # Bug 577704 + _distutils-r1_print_package_versions + _DISTUTILS_DEFAULT_CALLED=1 } @@ -678,6 +1020,10 @@ distutils-r1_python_prepare_all() { # Create implementation-specific configuration file for distutils, # setting proper build-dir (and install-dir) paths. _distutils-r1_create_setup_cfg() { + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is not implemented in PEP517 mode" + fi + cat > "${HOME}"/.pydistutils.cfg <<-_EOF_ || die [build] build_base = ${BUILD_DIR} @@ -705,8 +1051,8 @@ _distutils-r1_create_setup_cfg() { zip_safe = False _EOF_ - # we can't refer to ${D} before src_install() if [[ ${EBUILD_PHASE} == install ]]; then + # we can't refer to ${D} before src_install() cat >> "${HOME}"/.pydistutils.cfg <<-_EOF_ || die # installation paths -- allow calling extra install targets @@ -718,6 +1064,7 @@ _distutils-r1_create_setup_cfg() { _EOF_ if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then + # this gets appended to [install] cat >> "${HOME}"/.pydistutils.cfg <<-_EOF_ || die install_scripts = $(python_get_scriptdir) _EOF_ @@ -732,11 +1079,218 @@ _distutils-r1_create_setup_cfg() { # egg-base in esetup.py). This way, we respect whatever's in upstream # egg-info. _distutils-r1_copy_egg_info() { + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is not implemented in PEP517 mode" + fi + mkdir -p "${BUILD_DIR}" || die # stupid freebsd can't do 'cp -t ${BUILD_DIR} {} +' find -name '*.egg-info' -type d -exec cp -R -p {} "${BUILD_DIR}"/ ';' || die } +# @FUNCTION: _distutils-r1_backend_to_key +# @USAGE: +# @INTERNAL +# @DESCRIPTION: +# Print the DISTUTILS_USE_PEP517 value corresponding to the backend +# passed as the only argument. +_distutils-r1_backend_to_key() { + debug-print-function ${FUNCNAME} "${@}" + + local backend=${1} + case ${backend} in + flit_core.buildapi|flit.buildapi) + echo flit + ;; + flit_scm:buildapi) + echo flit_scm + ;; + hatchling.build) + echo hatchling + ;; + jupyter_packaging.build_api) + echo jupyter + ;; + maturin) + echo maturin + ;; + pbr.build) + echo pbr + ;; + pdm.pep517.api) + echo pdm + ;; + poetry.core.masonry.api|poetry.masonry.api) + echo poetry + ;; + setuptools.build_meta|setuptools.build_meta:__legacy__) + echo setuptools + ;; + sipbuild.api) + echo sip + ;; + *) + die "Unknown backend: ${backend}" + ;; + esac +} + +# @FUNCTION: _distutils-r1_get_backend +# @INTERNAL +# @DESCRIPTION: +# Read (or guess, in case of setuptools) the build-backend +# for the package in the current directory. +_distutils-r1_get_backend() { + debug-print-function ${FUNCNAME} "${@}" + + local build_backend + if [[ -f pyproject.toml ]]; then + # if pyproject.toml exists, try getting the backend from it + # NB: this could fail if pyproject.toml doesn't list one + build_backend=$(gpep517 get-backend) + fi + if [[ -z ${build_backend} && ${DISTUTILS_USE_PEP517} == setuptools && + -f setup.py ]] + then + # use the legacy setuptools backend as a fallback + build_backend=setuptools.build_meta:__legacy__ + fi + if [[ -z ${build_backend} ]]; then + die "Unable to obtain build-backend from pyproject.toml" + fi + + if [[ ${DISTUTILS_USE_PEP517} != standalone ]]; then + # verify whether DISTUTILS_USE_PEP517 was set correctly + local expected_value=$(_distutils-r1_backend_to_key "${build_backend}") + if [[ ${DISTUTILS_USE_PEP517} != ${expected_value} ]]; then + eerror "DISTUTILS_USE_PEP517 does not match pyproject.toml!" + eerror " have: DISTUTILS_USE_PEP517=${DISTUTILS_USE_PEP517}" + eerror "expected: DISTUTILS_USE_PEP517=${expected_value}" + eerror "(backend: ${build_backend})" + die "DISTUTILS_USE_PEP517 value incorrect" + fi + + # fix deprecated backends up + local new_backend= + case ${build_backend} in + flit.buildapi) + new_backend=flit_core.buildapi + ;; + poetry.masonry.api) + new_backend=poetry.core.masonry.api + ;; + esac + + if [[ -n ${new_backend} ]]; then + if [[ ! -f ${T}/.distutils_deprecated_backend_warned ]]; then + eqawarn "${build_backend} backend is deprecated. Please see:" + eqawarn "https://projects.gentoo.org/python/guide/distutils.html#deprecated-pep-517-backends" + eqawarn "The eclass will be using ${new_backend} instead." + > "${T}"/.distutils_deprecated_backend_warned || die + fi + build_backend=${new_backend} + fi + fi + + echo "${build_backend}" +} + +# @FUNCTION: distutils_pep517_install +# @USAGE: +# @DESCRIPTION: +# Build the wheel for the package in the current directory using PEP 517 +# backend and install it into . +# +# This function is intended for expert use only. It does not handle +# wrapping executables. +distutils_pep517_install() { + debug-print-function ${FUNCNAME} "${@}" + [[ ${#} -eq 1 ]] || die "${FUNCNAME} takes exactly one argument: root" + + local root=${1} + local -x WHEEL_BUILD_DIR=${BUILD_DIR}/wheel + mkdir -p "${WHEEL_BUILD_DIR}" || die + + if [[ -n ${mydistutilsargs[@]} ]]; then + die "mydistutilsargs are banned in PEP517 mode (use DISTUTILS_ARGS)" + fi + + local config_settings= + if [[ -n ${DISTUTILS_ARGS[@]} ]]; then + case ${DISTUTILS_USE_PEP517} in + setuptools) + config_settings=$( + "${EPYTHON}" - "${DISTUTILS_ARGS[@]}" <<-EOF || die + import json + import sys + print(json.dumps({"--global-option": sys.argv[1:]})) + EOF + ) + ;; + sip) + # NB: for practical reasons, we support only --foo=bar, + # not --foo bar + local arg + for arg in "${DISTUTILS_ARGS[@]}"; do + [[ ${arg} != -* ]] && + die "Bare arguments in DISTUTILS_ARGS unsupported: ${arg}" + done + + config_settings=$( + "${EPYTHON}" - "${DISTUTILS_ARGS[@]}" <<-EOF || die + import collections + import json + import sys + + args = collections.defaultdict(list) + for arg in (x.split("=", 1) for x in sys.argv[1:]): \ + args[arg[0]].extend( + [arg[1]] if len(arg) > 1 else []) + + print(json.dumps(args)) + EOF + ) + ;; + *) + die "DISTUTILS_ARGS are not supported by ${DISTUTILS_USE_PEP517}" + ;; + esac + fi + + local build_backend=$(_distutils-r1_get_backend) + einfo " Building the wheel for ${PWD#${WORKDIR}/} via ${build_backend}" + local config_args=() + [[ -n ${config_settings} ]] && + config_args+=( --config-json "${config_settings}" ) + local wheel=$( + gpep517 build-wheel --backend "${build_backend}" \ + --output-fd 3 \ + --wheel-dir "${WHEEL_BUILD_DIR}" \ + "${config_args[@]}" 3>&1 >&2 || + die "Wheel build failed" + ) + [[ -n ${wheel} ]] || die "No wheel name returned" + + einfo " Installing the wheel to ${root}" + gpep517 install-wheel --destdir="${root}" --interpreter="${PYTHON}" \ + --prefix="${EPREFIX}/usr" "${WHEEL_BUILD_DIR}/${wheel}" || + die "Wheel install failed" + + # remove installed licenses + find "${root}$(python_get_sitedir)" -depth \ + \( -path '*.dist-info/COPYING*' \ + -o -path '*.dist-info/LICENSE*' \ + -o -path '*.dist-info/license_files/*' \ + -o -path '*.dist-info/license_files' \ + \) -delete || die + + # clean the build tree; otherwise we may end up with PyPy3 + # extensions duplicated into CPython dists + if [[ ${DISTUTILS_USE_PEP517:-setuptools} == setuptools ]]; then + rm -rf build || die + fi +} + # @FUNCTION: distutils-r1_python_compile # @USAGE: [additional-args...] # @DESCRIPTION: @@ -751,16 +1305,90 @@ distutils-r1_python_compile() { _python_check_EPYTHON - _distutils-r1_copy_egg_info + case ${DISTUTILS_USE_PEP517:-setuptools} in + setuptools) + # call setup.py build when using setuptools (either via PEP517 + # or in legacy mode) - # distutils is parallel-capable since py3.5 - local jobs=$(makeopts_jobs "${MAKEOPTS}" INF) - if [[ ${jobs} == INF ]]; then - local nproc=$(get_nproc) - jobs=$(( nproc + 1 )) + if [[ ${DISTUTILS_USE_PEP517} ]]; then + if [[ -d build ]]; then + eqawarn "A 'build' directory exists already. Artifacts from this directory may" + eqawarn "be picked up by setuptools when building for another interpreter." + eqawarn "Please remove this directory prior to building." + fi + else + _distutils-r1_copy_egg_info + fi + + # distutils is parallel-capable since py3.5 + local jobs=$(makeopts_jobs "${MAKEOPTS} ${*}") + + if [[ ${DISTUTILS_USE_PEP517} ]]; then + # issue build_ext only if it looks like we have at least + # two source files to build; setuptools is expensive + # to start and parallel builds can only benefit us if we're + # compiling at least two files + # + # see extension.py for list of suffixes + # .pyx is added for Cython + if [[ 1 -ne ${jobs} && 2 -eq $( + find '(' -name '*.c' -o -name '*.cc' -o -name '*.cpp' \ + -o -name '*.cxx' -o -name '*.c++' -o -name '*.m' \ + -o -name '*.mm' -o -name '*.pyx' ')' -printf '\n' | + head -n 2 | wc -l + ) ]]; then + esetup.py build_ext -j "${jobs}" "${@}" + fi + else + esetup.py build -j "${jobs}" "${@}" + fi + ;; + maturin) + # auditwheel may attempt to auto-bundle libraries, bug #831171 + local -x MATURIN_PEP517_ARGS=--skip-auditwheel + + # support cargo.eclass' IUSE=debug if available + in_iuse debug && use debug && + MATURIN_PEP517_ARGS+=" --cargo-extra-args=--profile=dev" + ;; + esac + + if [[ ${DISTUTILS_USE_PEP517} ]]; then + # python likes to compile any module it sees, which triggers sandbox + # failures if some packages haven't compiled their modules yet. + addpredict "${EPREFIX}/usr/lib/${EPYTHON}" + addpredict /usr/lib/pypy3.8 + addpredict /usr/lib/portage/pym + addpredict /usr/local # bug 498232 + + local root=${BUILD_DIR}/install + distutils_pep517_install "${root}" + + # copy executables to python-exec directory + # we do it early so that we can alter bindir recklessly + local bindir=${root}${EPREFIX}/usr/bin + local rscriptdir=${root}$(python_get_scriptdir) + [[ -d ${rscriptdir} ]] && + die "${rscriptdir} should not exist!" + if [[ -d ${bindir} ]]; then + mkdir -p "${rscriptdir}" || die + cp -a --reflink=auto "${bindir}"/. "${rscriptdir}"/ || die + fi + + # enable venv magic inside the install tree + mkdir -p "${bindir}" || die + ln -s "${PYTHON}" "${bindir}/${EPYTHON}" || die + ln -s "${EPYTHON}" "${bindir}/python3" || die + ln -s "${EPYTHON}" "${bindir}/python" || die + cat > "${bindir}"/pyvenv.cfg <<-EOF || die + include-system-site-packages = true + EOF + + # we need to change shebangs to point to the venv-python + find "${bindir}" -type f -exec sed -i \ + -e "1s@^#!\(${EPREFIX}/usr/bin/\(python\|pypy\)\)@#!${root}\1@" \ + {} + || die fi - - esetup.py build -j "${jobs}" "${@}" } # @FUNCTION: _distutils-r1_wrap_scripts @@ -837,7 +1465,7 @@ distutils-r1_python_test() { case ${_DISTUTILS_TEST_RUNNER} in nose) - nosetests -v "${@}" + "${EPYTHON}" -m nose -v "${@}" ;; pytest) epytest @@ -872,59 +1500,72 @@ distutils-r1_python_install() { _python_check_EPYTHON - local root=${D%/}/_${EPYTHON} - [[ ${DISTUTILS_SINGLE_IMPL} ]] && root=${D%/} + local scriptdir=${EPREFIX}/usr/bin + if [[ ${DISTUTILS_USE_PEP517} ]]; then + local root=${BUILD_DIR}/install + # remove the altered bindir, executables from the package + # are already in scriptdir + rm -r "${root}${scriptdir}" || die + if [[ ${DISTUTILS_SINGLE_IMPL} ]]; then + local wrapped_scriptdir=${root}$(python_get_scriptdir) + if [[ -d ${wrapped_scriptdir} ]]; then + mv "${wrapped_scriptdir}" "${root}${scriptdir}" || die + fi + fi + else + local root=${D%/}/_${EPYTHON} + [[ ${DISTUTILS_SINGLE_IMPL} ]] && root=${D%/} - # inline DISTUTILS_ARGS logic from esetup.py in order to make - # argv overwriting easier - local args=( - "${DISTUTILS_ARGS[@]}" - "${mydistutilsargs[@]}" - install --skip-build --root="${root}" "${args[@]}" - "${@}" - ) - local DISTUTILS_ARGS=() - local mydistutilsargs=() + # inline DISTUTILS_ARGS logic from esetup.py in order to make + # argv overwriting easier + local args=( + "${DISTUTILS_ARGS[@]}" + "${mydistutilsargs[@]}" + install --skip-build --root="${root}" "${args[@]}" + "${@}" + ) + local DISTUTILS_ARGS=() + local mydistutilsargs=() - # enable compilation for the install phase. - local -x PYTHONDONTWRITEBYTECODE= + # enable compilation for the install phase. + local -x PYTHONDONTWRITEBYTECODE= - # python likes to compile any module it sees, which triggers sandbox - # failures if some packages haven't compiled their modules yet. - addpredict "${EPREFIX}/usr/lib/${EPYTHON}" - addpredict /usr/lib/pypy3.8 - addpredict /usr/lib/portage/pym - addpredict /usr/local # bug 498232 + # python likes to compile any module it sees, which triggers sandbox + # failures if some packages haven't compiled their modules yet. + addpredict "${EPREFIX}/usr/lib/${EPYTHON}" + addpredict /usr/lib/pypy3.8 + addpredict /usr/lib/portage/pym + addpredict /usr/local # bug 498232 - if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then - # user may override --install-scripts - # note: this is poor but distutils argv parsing is dumb - local scriptdir=${EPREFIX}/usr/bin + if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then + # user may override --install-scripts + # note: this is poor but distutils argv parsing is dumb - # rewrite all the arguments - set -- "${args[@]}" - args=() - while [[ ${@} ]]; do - local a=${1} - shift + # rewrite all the arguments + set -- "${args[@]}" + args=() + while [[ ${@} ]]; do + local a=${1} + shift - case ${a} in - --install-scripts=*) - scriptdir=${a#--install-scripts=} - ;; - --install-scripts) - scriptdir=${1} - shift - ;; - *) - args+=( "${a}" ) - ;; - esac - done + case ${a} in + --install-scripts=*) + scriptdir=${a#--install-scripts=} + ;; + --install-scripts) + scriptdir=${1} + shift + ;; + *) + args+=( "${a}" ) + ;; + esac + done + fi + + esetup.py "${args[@]}" fi - esetup.py "${args[@]}" - local forbidden_package_names=( examples test tests .pytest_cache .hypothesis @@ -939,8 +1580,8 @@ distutils-r1_python_install() { local shopt_save=$(shopt -p nullglob) shopt -s nullglob local pypy_dirs=( - "${root}/usr/$(get_libdir)"/pypy*/share - "${root}/usr/lib"/pypy*/share + "${root}${EPREFIX}/usr/$(get_libdir)"/pypy*/share + "${root}${EPREFIX}/usr/lib"/pypy*/share ) ${shopt_save} @@ -948,8 +1589,15 @@ distutils-r1_python_install() { die "Package installs 'share' in PyPy prefix, see bug #465546." fi - if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then + if [[ ! ${DISTUTILS_SINGLE_IMPL} || ${DISTUTILS_USE_PEP517} ]]; then multibuild_merge_root "${root}" "${D%/}" + if [[ ${DISTUTILS_USE_PEP517} ]]; then + # we need to recompile everything here in order to embed + # the correct paths + python_optimize "${D%/}$(python_get_sitedir)" + fi + fi + if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then _distutils-r1_wrap_scripts "${scriptdir}" fi } @@ -990,20 +1638,29 @@ distutils-r1_run_phase() { fi local BUILD_DIR=${BUILD_DIR}/build fi - local -x PYTHONPATH="${BUILD_DIR}/lib:${PYTHONPATH}" - # make PATH local for distutils_install_for_testing calls - # it makes little sense to let user modify PATH in per-impl phases - # and _all() already localizes it - local -x PATH=${PATH} + if [[ ${DISTUTILS_USE_PEP517} ]]; then + local -x PATH=${BUILD_DIR}/install${EPREFIX}/usr/bin:${PATH} + else + local -x PYTHONPATH="${BUILD_DIR}/lib:${PYTHONPATH}" - # Bug 559644 - # using PYTHONPATH when the ${BUILD_DIR}/lib is not created yet might lead to - # problems in setup.py scripts that try to import modules/packages from that path - # during the build process (Python at startup evaluates PYTHONPATH, if the dir is - # not valid then associates a NullImporter object to ${BUILD_DIR}/lib storing it - # in the sys.path_importer_cache) - mkdir -p "${BUILD_DIR}/lib" || die + # make PATH local for distutils_install_for_testing calls + # it makes little sense to let user modify PATH in per-impl phases + # and _all() already localizes it + local -x PATH=${PATH} + + # Undo the default switch in setuptools-60+ for the time being, + # to avoid replacing .egg-info file with directory in-place. + local -x SETUPTOOLS_USE_DISTUTILS="${SETUPTOOLS_USE_DISTUTILS:-stdlib}" + + # Bug 559644 + # using PYTHONPATH when the ${BUILD_DIR}/lib is not created yet might lead to + # problems in setup.py scripts that try to import modules/packages from that path + # during the build process (Python at startup evaluates PYTHONPATH, if the dir is + # not valid then associates a NullImporter object to ${BUILD_DIR}/lib storing it + # in the sys.path_importer_cache) + mkdir -p "${BUILD_DIR}/lib" || die + fi # Set up build environment, bug #513664. local -x AR=${AR} CC=${CC} CPP=${CPP} CXX=${CXX} @@ -1020,10 +1677,17 @@ distutils-r1_run_phase() { esac local -x LDSHARED="${CC} ${ldopts}" LDCXXSHARED="${CXX} ${ldopts}" + local _DISTUTILS_POST_PHASE_RM=() "${@}" + local ret=${?} + + if [[ -n ${_DISTUTILS_POST_PHASE_RM} ]]; then + rm "${_DISTUTILS_POST_PHASE_RM[@]}" || die + fi cd "${_DISTUTILS_INITIAL_CWD}" || die + return "${ret}" } # @FUNCTION: _distutils-r1_run_common_phase @@ -1081,14 +1745,14 @@ _distutils-r1_run_foreach_impl() { distutils-r1_src_prepare() { debug-print-function ${FUNCNAME} "${@}" - + local ret=0 local _DISTUTILS_DEFAULT_CALLED # common preparations if declare -f python_prepare_all >/dev/null; then - python_prepare_all + python_prepare_all || ret=${?} else - distutils-r1_python_prepare_all + distutils-r1_python_prepare_all || ret=${?} fi if [[ ! ${_DISTUTILS_DEFAULT_CALLED} ]]; then @@ -1096,35 +1760,42 @@ distutils-r1_src_prepare() { fi if declare -f python_prepare >/dev/null; then - _distutils-r1_run_foreach_impl python_prepare + _distutils-r1_run_foreach_impl python_prepare || ret=${?} fi + + return ${ret} } distutils-r1_src_configure() { - python_export_utf8_locale - [[ ${EAPI} == 6 ]] && xdg_environment_reset # Bug 577704 + debug-print-function ${FUNCNAME} "${@}" + local ret=0 if declare -f python_configure >/dev/null; then - _distutils-r1_run_foreach_impl python_configure + _distutils-r1_run_foreach_impl python_configure || ret=${?} fi if declare -f python_configure_all >/dev/null; then - _distutils-r1_run_common_phase python_configure_all + _distutils-r1_run_common_phase python_configure_all || ret=${?} fi + + return ${ret} } distutils-r1_src_compile() { debug-print-function ${FUNCNAME} "${@}" + local ret=0 if declare -f python_compile >/dev/null; then - _distutils-r1_run_foreach_impl python_compile + _distutils-r1_run_foreach_impl python_compile || ret=${?} else - _distutils-r1_run_foreach_impl distutils-r1_python_compile + _distutils-r1_run_foreach_impl distutils-r1_python_compile || ret=${?} fi if declare -f python_compile_all >/dev/null; then - _distutils-r1_run_common_phase python_compile_all + _distutils-r1_run_common_phase python_compile_all || ret=${?} fi + + return ${ret} } # @FUNCTION: _distutils-r1_clean_egg_info @@ -1134,20 +1805,29 @@ distutils-r1_src_compile() { # Those files ended up being unversioned, and caused issues: # https://bugs.gentoo.org/534058 _distutils-r1_clean_egg_info() { + if [[ ${DISTUTILS_USE_PEP517} ]]; then + die "${FUNCNAME} is not implemented in PEP517 mode" + fi + rm -rf "${BUILD_DIR}"/lib/*.egg-info || die } distutils-r1_src_test() { debug-print-function ${FUNCNAME} "${@}" + local ret=0 if declare -f python_test >/dev/null; then - _distutils-r1_run_foreach_impl python_test - _distutils-r1_run_foreach_impl _distutils-r1_clean_egg_info + _distutils-r1_run_foreach_impl python_test || ret=${?} + if [[ ! ${DISTUTILS_USE_PEP517} ]]; then + _distutils-r1_run_foreach_impl _distutils-r1_clean_egg_info + fi fi if declare -f python_test_all >/dev/null; then - _distutils-r1_run_common_phase python_test_all + _distutils-r1_run_common_phase python_test_all || ret=${?} fi + + return ${ret} } # @FUNCTION: _distutils-r1_check_namespace_pth @@ -1173,26 +1853,29 @@ _distutils-r1_check_namespace_pth() { ewarn "read our documentation on reliable handling of namespaces and update" ewarn "the ebuild accordingly:" ewarn - ewarn " https://wiki.gentoo.org/wiki/Project:Python/Namespace_packages" + ewarn " https://projects.gentoo.org/python/guide/concept.html#namespace-packages" fi } distutils-r1_src_install() { debug-print-function ${FUNCNAME} "${@}" + local ret=0 if declare -f python_install >/dev/null; then - _distutils-r1_run_foreach_impl python_install + _distutils-r1_run_foreach_impl python_install || ret=${?} else - _distutils-r1_run_foreach_impl distutils-r1_python_install + _distutils-r1_run_foreach_impl distutils-r1_python_install || ret=${?} fi if declare -f python_install_all >/dev/null; then - _distutils-r1_run_common_phase python_install_all + _distutils-r1_run_common_phase python_install_all || ret=${?} else - _distutils-r1_run_common_phase distutils-r1_python_install_all + _distutils-r1_run_common_phase distutils-r1_python_install_all || ret=${?} fi _distutils-r1_check_namespace_pth + + return ${ret} } _DISTUTILS_R1=1