Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert python to a v2 tool #404

Merged
merged 6 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 0 additions & 117 deletions src/usr/local/buildpack/tools/python.sh

This file was deleted.

82 changes: 82 additions & 0 deletions src/usr/local/buildpack/tools/v2/python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# sets the correct shebang for python
fix_python_shebangs() {
# https://github.com/koalaman/shellcheck/wiki/SC2044
while IFS= read -r -d '' file
do
case "$(head -1 "${file}")" in
"#!"*"/bin/python" )
sed -i "1 s:.*:#\!${versioned_tool_path}\/bin\/python:" "${file}"
;;
"#!"*"/bin/python${MAJOR}" )
sed -i "1 s:.*:#\!${versioned_tool_path}\/bin\/python${MAJOR}:" "${file}"
;;
"#!"*"/bin/python${MAJOR}.${MINOR}" )
sed -i "1 s:.*:#\!${versioned_tool_path}\/bin\/python${MAJOR}.${MINOR}:" "${file}"
;;
esac
done < <(find "${versioned_tool_path}/bin" -type f -exec grep -Iq . {} \; -print0)
}

function install_tool () {
local versioned_tool_path
local file
local url
local arch
local version_codename

# get semver -> major, minor, patch
check_semver "${TOOL_VERSION}"
Chumper marked this conversation as resolved.
Show resolved Hide resolved

versioned_tool_path=$(create_versioned_tool_path)
arch=$(uname -p)
url="https://github.com/containerbase/python-prebuild/releases/download"
version_codename=$(get_distro)

create_folder "${versioned_tool_path}/bin"

file=$(get_from_url "${url}/${TOOL_VERSION}/python-${TOOL_VERSION}-${version_codename}-${arch}.tar.xz")

if [[ -f ${file} ]]; then
echo 'Using prebuild python'
tar -C "${versioned_tool_path}" --strip 1 -xf "${file}"
else
echo 'No prebuild python found' >&2
exit 1
fi

fix_python_shebangs

# install latest pip
PYTHONHOME=${versioned_tool_path} "${versioned_tool_path}/bin/pip" install --upgrade pip

# clean cache https://pip.pypa.io/en/stable/reference/pip_cache/#pip-cache
PYTHONHOME=${versioned_tool_path} "${versioned_tool_path}/bin/pip" cache purge
}

function link_tool () {
local versioned_tool_path
versioned_tool_path=$(find_versioned_tool_path)

# get semver -> major, minor, patch
check_semver "${TOOL_VERSION}"

reset_tool_env

# export python vars
export_tool_env PYTHONHOME "${versioned_tool_path}"
export_tool_path "${versioned_tool_path}/bin"
export_tool_path "${USER_HOME}/.local/bin"
viceice marked this conversation as resolved.
Show resolved Hide resolved

# TODO: fix me, currently required for global pip
shell_wrapper "${TOOL_NAME}" "${versioned_tool_path}/bin"
shell_wrapper "${TOOL_NAME}${MAJOR}" "${versioned_tool_path}/bin"
shell_wrapper "${TOOL_NAME}${MAJOR}.${MINOR}" "${versioned_tool_path}/bin"
shell_wrapper pip "${versioned_tool_path}/bin"
shell_wrapper "pip${MAJOR}" "${versioned_tool_path}/bin"
shell_wrapper "pip${MAJOR}.${MINOR}" "${versioned_tool_path}/bin"

python --version
pip --version
}
155 changes: 155 additions & 0 deletions test/bash/tools/python.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
setup_file () {
export TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)"

# set up the cache
load "$TEST_DIR/../cache.sh"
export BUILDPACK_CACHE_DIR="$(create_temp_dir TEST_CACHE_DIR)"
}

setup() {
load '../../../node_modules/bats-support/load'
load '../../../node_modules/bats-assert/load'

TEST_ROOT_DIR=$(mktemp -u)

load "$TEST_DIR/../../../src/usr/local/buildpack/util.sh"

# load v2 overwrites
load "$TEST_DIR/../../../src/usr/local/buildpack/utils/v2/overrides.sh"

# load test overwrites
load "$TEST_DIR/../util.sh"

# set directories for test
ROOT_DIR="${TEST_ROOT_DIR}/root"
BIN_DIR="${TEST_ROOT_DIR}/bin"

setup_directories

# set default test user
TEST_ROOT_USER=1000

# load python
load "$TEST_DIR/../../../src/usr/local/buildpack/tools/v2/python.sh"

}

teardown() {
rm -rf "${TEST_ROOT_DIR}"
}

teardown_file () {
clean_temp_dir $BUILDPACK_CACHE_DIR TEST_CACHE_DIR
}

@test "python: check_tool_requirements" {
TOOL_NAME=python

TOOL_VERSION=foobar \
run check_tool_requirements
assert_failure

TOOL_VERSION=1.2.3 \
run check_tool_requirements
assert_success
}

@test "python: check_tool_installed" {
local TOOL_NAME=python
local TOOL_VERSION

# renovate: datasource=github-releases lookupName=containerbase/python-prebuild
TOOL_VERSION=3.10.5

run check_tool_installed
assert_failure

run install_tool
assert_success

run check_tool_installed
assert_success
}

@test "python: install_tool" {
local TOOL_NAME=python
local TOOL_VERSION

# renovate: datasource=github-releases lookupName=containerbase/python-prebuild
TOOL_VERSION=3.10.5

run install_tool
assert_success

local versioned_tool_path=$(find_versioned_tool_path)

PATH="${versioned_tool_path}/bin" \
run python --version
assert_success
assert_output --partial "${TOOL_VERSION}"

# don't update
TOOL_VERSION=3.10.4

run install_tool
assert_success

local versioned_tool_path=$(find_versioned_tool_path)

PATH="${versioned_tool_path}/bin" \
run python --version
assert_success
assert_output --partial "${TOOL_VERSION}"
}

@test "python: link_tool" {
local TOOL_NAME=python
local TOOL_VERSION
local bin_path=$(get_bin_path)

# renovate: datasource=github-releases lookupName=containerbase/python-prebuild
TOOL_VERSION=3.10.5

run install_tool
assert_success

PATH="${bin_path}:$PATH" \
run link_tool
assert_success
assert_output --partial "${TOOL_VERSION}"

PATH="${bin_path}" \
run python --version
assert_success
assert_output --partial "${TOOL_VERSION}"

local versioned_tool_path=$(find_versioned_tool_path)

PATH="${versioned_tool_path}/bin" \
run python --version
assert_success
assert_output --partial "${TOOL_VERSION}"

# don't update
TOOL_VERSION=3.10.4

run install_tool
assert_success

PATH="${bin_path}:$PATH" \
run link_tool
assert_success
assert_output --partial "${TOOL_VERSION}"

local versioned_tool_path=$(find_versioned_tool_path)

PATH="${versioned_tool_path}/bin" \
run python --version
assert_success
assert_output --partial "${TOOL_VERSION}"

PATH="${bin_path}" \
run python --version
assert_success
assert_output --partial "${TOOL_VERSION}"
}