From 5f863d1fb2fe0bd90e9db3d34423f1197a0b9a10 Mon Sep 17 00:00:00 2001 From: Austin Raney Date: Mon, 29 Jul 2024 16:02:14 -0400 Subject: [PATCH] chore(build): update build scripts to use pyproject.toml --- scripts/dist_package.sh | 2 +- scripts/shared/py_dev_func.sh | 24 ++++++++++++++++-------- scripts/update_package.sh | 15 +++++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/scripts/dist_package.sh b/scripts/dist_package.sh index 26b7ed66c..944e21f5e 100755 --- a/scripts/dist_package.sh +++ b/scripts/dist_package.sh @@ -108,5 +108,5 @@ elif [ -n "${CLEAN_ONLY}" ]; then >&2 echo "Error: unexpected value set for variable CLEAN_ONLY (${CLEAN_ONLY}); exiting without building" exit 1 else - py_dev_clean_dist && python setup.py sdist bdist_wheel + py_dev_clean_dist && python -m build fi diff --git a/scripts/shared/py_dev_func.sh b/scripts/shared/py_dev_func.sh index d267e7612..ac351ccc7 100644 --- a/scripts/shared/py_dev_func.sh +++ b/scripts/shared/py_dev_func.sh @@ -6,6 +6,18 @@ VENV_WAS_ACTIVATED=1 PACKAGE_NAMESPACE_ROOT=dmod +# Check and install 'build' package if it's not present. +# Returns 0 on success and > 0 on failure. +py_dev_maybe_install_build_deps() +{ + if ! python -m pip show build > /dev/null 2>&1; then + echo 'Installing missing python build dependency: "build"' + python -m pip install build + return $? + fi + return 0 +} + # Validate that an arg is a path to a valid venv directory, echoing the path to stdout if it is, and also returning 0 or # 1 consistent with standard shell T/F. # Also, print out messages to stderr when not valid, unless suppressed with '-q' arg. @@ -71,7 +83,6 @@ py_dev_get_egg_info_dir() py_dev_clean_dist() { _PY_DEV_CLEAN_DIST_EGG_DIR="$(ls | grep -e '.*\.egg-info$')" - python setup.py clean --all 2>/dev/null [ -d ./build ] && echo "Removing $(pwd)/build" && rm -r ./build [ -d ./dist ] && echo "Removing $(pwd)/dist" && rm -r ./dist @@ -81,18 +92,15 @@ py_dev_clean_dist() fi } -# Determine the distribution name of the Python package based in the current working directory from its setup.py +# Determine the distribution name of the Python package based in the current working directory from its pyproject.toml py_dev_extract_package_dist_name_from_setup() { - if [ ! -e setup.py ]; then - >&2 echo "Error: expected $(pwd)/setup.py not found; cannot determine package dist name" + if [ ! -e pyproject.toml ]; then + >&2 echo "Error: expected $(pwd)/pyproject.toml not found; cannot determine package dist name" return 1 fi - cat setup.py \ - | grep -v import \ - | grep -e '^\(.*,\)\?[ ]*name[ ]*=[ ]*\(.\)\([^,]*\)[^, ][ ]*,' \ - | sed -E 's/^(.*,)*[ ]*name[ ]*=[ ]*.([^,]*)[^,],.*/\2/g' + sed -n 's/^name[ ]*=[ ]*"\(.*\)"/\1/p' pyproject.toml } # If appropriate, activate the virtual env set within VENV_DIR, and note whether this is done in VENV_WAS_ACTIVATED. diff --git a/scripts/update_package.sh b/scripts/update_package.sh index 0e8bd8b62..320680552 100755 --- a/scripts/update_package.sh +++ b/scripts/update_package.sh @@ -82,8 +82,8 @@ init_package_dirs_array_when_empty() spi=0 for i in ${LIB_PACKAGE_DIRS[@]}; do - # Include package directory, as long as there is a setup.py for the package - if [ -e "${i}/setup.py" ]; then + # Include package directory, as long as there is a pyproject.toml for the package + if [ -e "${i}/pyproject.toml" ]; then PACKAGE_DIRS[${spi}]="${i}" spi=$((spi+1)) fi @@ -92,8 +92,8 @@ init_package_dirs_array_when_empty() # Though check for option indicating only library packages should be installed. if [ -z "${NO_SERVICE_PACKAGES:-}" ]; then for i in ${SERVICE_PACKAGE_DIRS[@]}; do - # Include package directory, as long as there is a setup.py for the package - if [ -e "${i}/setup.py" ]; then + # Include package directory, as long as there is a pyproject.toml for the package + if [ -e "${i}/pyproject.toml" ]; then PACKAGE_DIRS[${spi}]="${i}" spi=$((spi+1)) fi @@ -209,6 +209,9 @@ py_dev_activate_venv # Trap to make sure we "clean up" script activity before exiting trap cleanup_before_exit 0 1 2 3 6 15 +# Install build dependencies if not present +py_dev_maybe_install_build_deps + # After setting VENV, if set to get dependencies, do that, optionally exiting after if that's all we are set to do if [ -n "${DO_DEPS:-}" ]; then pip install --upgrade -r "${_REQ_FILE}" @@ -245,7 +248,7 @@ build_package_and_collect_dist_details() # Bail if we can't detect the appropriate package dist name if [ -z "${PACKAGE_DIST_NAMES[${_N}]}" ]; then - >&2 echo "Error: unable to determine package dist name from ${1}/setup.py" + >&2 echo "Error: unable to determine package dist name from ${1}/pyproject.toml" exit 1 fi @@ -253,7 +256,7 @@ build_package_and_collect_dist_details() PACKAGE_DIST_DIR_LINK_ARGS[${_N}]="--find-links=${1}/dist" # Clean any previous artifacts and build - py_dev_clean_dist && python setup.py sdist bdist_wheel + py_dev_clean_dist && python -m build # Return to starting directory if one was given cd "${2}"