diff --git a/bin/boxfile b/bin/boxfile index 2d5427c..0f5c68b 100755 --- a/bin/boxfile +++ b/bin/boxfile @@ -5,7 +5,7 @@ cat <<-END run.config: cache_dirs: - - src + - .nanobox/pip END if [[ $(grep -c build_triggers /opt/nanobox/hooks/lib/boxfile.rb) -gt 1 ]]; then cat <<-END diff --git a/bin/build b/bin/build index 383012a..47a6fef 100755 --- a/bin/build +++ b/bin/build @@ -14,6 +14,9 @@ nos_init "$@" # install python interpreter and pip install_runtime_packages +# set pip env +set_pip_env + # setup python environment setup_python_env diff --git a/lib/python.sh b/lib/python.sh index 469ce0a..34599b7 100644 --- a/lib/python.sh +++ b/lib/python.sh @@ -124,7 +124,15 @@ pip_install_cmd() { # the default pip install cmd when a user-specified cmd is not present default_pip_install() { - echo "pip install -I -r requirements.txt" + echo "\ + pip install \ + -I \ + -r requirements.txt \ + --disable-pip-version-check \ + --no-cache-dir \ + --target=$(nos_code_dir)/.nanobox/pip/src \ + --upgrade \ + --install-option='--install-scripts=$(nos_code_dir)/.nanobox/pip/bin'" } # Install dependencies via pip from requirements.txt @@ -136,3 +144,21 @@ pip_install() { cd - >/dev/null fi } + +# Generate the payload to render the pip env profile.d template +pip_env_payload() { + cat <<-END +{ + "code_dir": "$(nos_code_dir)" +} +END +} + +# Since we install pip modules into a custom cache_dir, we need +# to setup the environment (with env vars) for the python app to work +set_pip_env() { + nos_template \ + "profile.d/pip.sh" \ + "$(nos_etc_dir)/profile.d/pip.sh" \ + "$(pip_env_payload)" +} diff --git a/templates/profile.d/pip.sh b/templates/profile.d/pip.sh new file mode 100755 index 0000000..38abde5 --- /dev/null +++ b/templates/profile.d/pip.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Nanobox stashes the pip module src and bin dirs inside of a cache_dir +# at {{code_dir}}/.nanobox/pip. We need to set PATH to include the bin dir +# and PYTHONPATH to include the src dir + +export PATH={{code_dir}}/.nanobox/pip/bin:$PATH +export PYTHONPATH={{code_dir}}/.nanobox/pip/src:$PYTHONPATH diff --git a/test/apps/simple-python/requirements.txt b/test/apps/simple-python/requirements.txt index bd4036f..bb7162c 100644 --- a/test/apps/simple-python/requirements.txt +++ b/test/apps/simple-python/requirements.txt @@ -1,3 +1,3 @@ --e git://github.com/webpy/webpy.git#egg=web.py +web.py gunicorn -Pillow \ No newline at end of file +Pillow diff --git a/test/tests/integration/simple-python.bats b/test/tests/integration/simple-python.bats index f9b1f14..14383b4 100644 --- a/test/tests/integration/simple-python.bats +++ b/test/tests/integration/simple-python.bats @@ -82,8 +82,11 @@ setup() { # cd into the app code_dir cd /tmp/code + # source the profile.d/pip.sh env + source /data/etc/profile.d/pip.sh + # start the server in the background - /data/bin/gunicorn -b 0.0.0.0:8080 app:wsgiapp & + /tmp/code/.nanobox/pip/bin/gunicorn -b 0.0.0.0:8080 app:wsgiapp & # grab the pid pid=$! diff --git a/test/util/env.sh b/test/util/env.sh index a599d3f..3f0e847 100644 --- a/test/util/env.sh +++ b/test/util/env.sh @@ -1,11 +1,14 @@ directories=( "/tmp/code" + "/tmp/code/.nanobox" + "/tmp/code/.nanobox/pip" "/tmp/app" "/tmp/cache" "/data" "/data/etc" "/data/etc/env.d" + "/data/etc/profile.d" ) prepare_environment() {