Skip to content

Commit

Permalink
chore(build): update build scripts to use pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
aaraney authored and robertbartel committed Jul 31, 2024
1 parent b67d05b commit 5f863d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion scripts/dist_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 16 additions & 8 deletions scripts/shared/py_dev_func.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions scripts/update_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -245,15 +248,15 @@ 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

# Then add the generated dist directory pip arg value to that array
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}"
Expand Down

0 comments on commit 5f863d1

Please sign in to comment.