-
Notifications
You must be signed in to change notification settings - Fork 50
feat: convert python to a v2 tool #404
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1184fe0
feat: convert python to a v2 tool
Chumper 22b8678
Apply suggestions from code review
viceice 556b049
Merge branch 'main' into feat/v2Python
Chumper 78d804c
chore: remove semver checking from v2 python tool
Chumper fdb4385
Merge branch 'main' into feat/v2Python
Chumper 110f726
fix: use correct test order for bats test
Chumper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #!/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 | ||
|
|
||
| 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) | ||
|
|
||
| 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" | ||
|
|
||
| # 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| 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 | ||
|
|
||
| # needed to export versions | ||
| check_tool_requirements | ||
|
|
||
|
|
||
| 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 | ||
|
|
||
| # needed to export versions | ||
| check_tool_requirements | ||
|
|
||
| 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 | ||
|
|
||
| # needed to export versions | ||
| check_tool_requirements | ||
|
|
||
| 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 | ||
|
|
||
| # needed to export versions | ||
| check_tool_requirements | ||
|
|
||
| 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 | ||
|
|
||
| # needed to export versions | ||
| check_tool_requirements | ||
|
|
||
| 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}" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.