Skip to content

Commit d8e2ed9

Browse files
authored
Misc package manager handling refactor (#1680)
Split out of the Poetry PR to keep that one smaller.
1 parent f00f258 commit d8e2ed9

File tree

6 files changed

+30
-25
lines changed

6 files changed

+30
-25
lines changed

bin/compile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ENV_DIR="${3}"
1919
BUILDPACK_DIR=$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" && pwd)
2020

2121
source "${BUILDPACK_DIR}/bin/utils"
22+
source "${BUILDPACK_DIR}/lib/utils.sh"
2223
source "${BUILDPACK_DIR}/lib/cache.sh"
2324
source "${BUILDPACK_DIR}/lib/hooks.sh"
2425
source "${BUILDPACK_DIR}/lib/metadata.sh"
@@ -27,7 +28,6 @@ source "${BUILDPACK_DIR}/lib/package_manager.sh"
2728
source "${BUILDPACK_DIR}/lib/pip.sh"
2829
source "${BUILDPACK_DIR}/lib/pipenv.sh"
2930
source "${BUILDPACK_DIR}/lib/python_version.sh"
30-
source "${BUILDPACK_DIR}/lib/utils.sh"
3131

3232
compile_start_time=$(nowms)
3333

@@ -75,6 +75,7 @@ PROFILE_PATH="$BUILD_DIR/.profile.d/python.sh"
7575
EXPORT_PATH="${BUILDPACK_DIR}/export"
7676
GUNICORN_PROFILE_PATH="$BUILD_DIR/.profile.d/python.gunicorn.sh"
7777
WEB_CONCURRENCY_PROFILE_PATH="$BUILD_DIR/.profile.d/WEB_CONCURRENCY.sh"
78+
python_home='/app/.heroku/python'
7879

7980
export PATH="/app/.heroku/python/bin:${PATH}"
8081
# Tell Python to not buffer it's stdin/stdout.
@@ -156,14 +157,13 @@ meta_time "python_install_duration" "${install_python_start_time}"
156157

157158
# Install the package manager and related tools.
158159
package_manager_install_start_time=$(nowms)
159-
bundled_pip_module_path="$(utils::bundled_pip_module_path "${BUILD_DIR}")"
160160
case "${package_manager}" in
161161
pip)
162-
pip::install_pip_setuptools_wheel "${bundled_pip_module_path}" "${python_major_version}"
162+
pip::install_pip_setuptools_wheel "${python_home}" "${python_major_version}"
163163
;;
164164
pipenv)
165165
# TODO: Stop installing pip when using Pipenv.
166-
pip::install_pip_setuptools_wheel "${bundled_pip_module_path}" "${python_major_version}"
166+
pip::install_pip_setuptools_wheel "${python_home}" "${python_major_version}"
167167
pipenv::install_pipenv
168168
;;
169169
*)

bin/utils

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,3 @@ is_module_available() {
4141
local module_name="${1}"
4242
python -c "import sys, importlib.util; sys.exit(0 if importlib.util.find_spec('${module_name}') else 1)"
4343
}
44-
45-
# The requirement versions are effectively buildpack constants, however, we want
46-
# Dependabot to be able to update them, which requires that they be in requirements
47-
# files. The requirements files contain contents like `package==1.2.3` (and not just
48-
# the package version) so we have to extract the version substring from it.
49-
get_requirement_version() {
50-
local package_name="${1}"
51-
local requirement
52-
requirement=$(cat "${BUILDPACK_DIR}/requirements/${package_name}.txt")
53-
local requirement_version="${requirement#"${package_name}=="}"
54-
echo "${requirement_version}"
55-
}

lib/package_manager.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ function package_manager::determine_package_manager() {
3636
meta_set "setup_py_only" "false"
3737
fi
3838

39-
case "${#package_managers_found[@]}" in
39+
local num_package_managers_found=${#package_managers_found[@]}
40+
41+
case "${num_package_managers_found}" in
4042
1)
4143
echo "${package_managers_found[0]}"
4244
return 0

lib/pip.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
# however, it helps Shellcheck realise the options under which these functions will run.
55
set -euo pipefail
66

7-
PIP_VERSION=$(get_requirement_version 'pip')
8-
SETUPTOOLS_VERSION=$(get_requirement_version 'setuptools')
9-
WHEEL_VERSION=$(get_requirement_version 'wheel')
7+
PIP_VERSION=$(utils::get_requirement_version 'pip')
8+
SETUPTOOLS_VERSION=$(utils::get_requirement_version 'setuptools')
9+
WHEEL_VERSION=$(utils::get_requirement_version 'wheel')
1010

1111
function pip::install_pip_setuptools_wheel() {
12+
local python_home="${1}"
13+
local python_major_version="${2}"
14+
1215
# We use the pip wheel bundled within Python's standard library to install our chosen
1316
# pip version, since it's faster than `ensurepip` followed by an upgrade in place.
14-
local bundled_pip_module_path="${1}"
15-
local python_major_version="${2}"
17+
local bundled_pip_module_path
18+
bundled_pip_module_path="$(utils::bundled_pip_module_path "${python_home}")"
1619

1720
meta_set "pip_version" "${PIP_VERSION}"
1821

lib/pipenv.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# however, it helps Shellcheck realise the options under which these functions will run.
55
set -euo pipefail
66

7-
PIPENV_VERSION=$(get_requirement_version 'pipenv')
7+
PIPENV_VERSION=$(utils::get_requirement_version 'pipenv')
88

99
# TODO: Either enable or remove these.
1010
# export CLINT_FORCE_COLOR=1

lib/utils.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44
# however, it helps Shellcheck realise the options under which these functions will run.
55
set -euo pipefail
66

7+
# The requirement versions are effectively buildpack constants, however, we want
8+
# Dependabot to be able to update them, which requires that they be in requirements
9+
# files. The requirements files contain contents like `package==1.2.3` (and not just
10+
# the package version) so we have to extract the version substring from it.
11+
function utils::get_requirement_version() {
12+
local package_name="${1}"
13+
local requirement
14+
requirement=$(cat "${BUILDPACK_DIR:?}/requirements/${package_name}.txt")
15+
local requirement_version="${requirement#"${package_name}=="}"
16+
echo "${requirement_version}"
17+
}
18+
719
# Python bundles pip within its standard library, which we can use to install our chosen
820
# pip version from PyPI, saving us from having to download the usual pip bootstrap script.
921
function utils::bundled_pip_module_path() {
10-
local build_dir="${1}"
22+
local python_home="${1}"
1123

1224
# We have to use a glob since the bundled wheel filename contains the pip version, which
1325
# differs between Python versions. We also have to handle the case where there are multiple
1426
# matching pip wheels, since in some versions of Python (eg 3.9.0) multiple versions of pip
1527
# were accidentally bundled upstream. Note: This implementation relies upon `nullglob` being
1628
# set, which is the case thanks to the `bin/utils` that was run earlier.
17-
local bundled_pip_wheel_list=("${build_dir}"/.heroku/python/lib/python*/ensurepip/_bundled/pip-*.whl)
29+
local bundled_pip_wheel_list=("${python_home}"/lib/python*/ensurepip/_bundled/pip-*.whl)
1830
local bundled_pip_wheel="${bundled_pip_wheel_list[0]}"
1931

2032
if [[ -z "${bundled_pip_wheel}" ]]; then

0 commit comments

Comments
 (0)