-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpython.sh
More file actions
140 lines (119 loc) · 3.88 KB
/
python.sh
File metadata and controls
140 lines (119 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- mode: bash; tab-width: 2; -*-
# vim: ts=2 sw=2 ft=bash noet
# source the deps functions
. ${engine_lib_dir}/deps.sh
# Copy the code into the live directory which will be used to run the app
publish_release() {
nos_print_bullet "Moving build into live app directory..."
rsync -a $(nos_code_dir)/ $(nos_app_dir)
}
# Determine the python runtime to install. This will first check
# within the boxfile.yml, then will rely on default_runtime to
# provide a sensible default
runtime() {
echo $(nos_validate \
"$(nos_payload "config_runtime")" \
"string" "$(default_runtime)")
}
# Provide a default python version.
default_runtime() {
echo "python-2.7"
}
# The pip package will look something like py27-pip so
# we need to fetch the condensed runtime to use for the package
condensed_runtime() {
version=$(runtime)
echo "${version//[.thon-]/}"
}
# In the data directory, the dir will look like python2.7 so
# we need to fetch to lib runtime whenever we're messing with libs
lib_runtime() {
version=$(runtime)
echo "${version//[-]/}"
}
# Install the python runtime along with any dependencies.
install_runtime_packages() {
pkgs=("$(runtime)" \
"$(condensed_runtime)-setuptools" \
"$(condensed_runtime)-pip")
# add packages that are usually part of the stdlib
pkgs+=(\
"$(condensed_runtime)-cElementTree" \
"$(condensed_runtime)-curses" \
"$(condensed_runtime)-expat" \
"$(condensed_runtime)-readline" \
"$(condensed_runtime)-sqlite3")
# add any client dependencies
pkgs+=("$(query_dependencies)")
nos_install ${pkgs[@]}
}
# Uninstall build dependencies
uninstall_build_packages() {
pkgs=()
# if pkgs isn't empty, let's uninstall what we don't need
if [[ ${#pkgs[@]} -gt 0 ]]; then
nos_uninstall ${pkgs[@]}
fi
}
# Here we get to be a bit clever. Python will store it's cache (deps, etc)
# in the site-packages dir. Changing this is not easily configurable, so we'll
# essentially setup a cache dir in ~/.nanobox/pip_cache, and symlink
# the site-packages to the cached location. Also, we'll copy anything
# into the cache on the first run.
#
# Additionally, we'll set some environment variables specifically for python
setup_python_env() {
# ensure python doesn't buffer even when not attached to a pty
nos_template_file \
"env.d/PYTHONUNBUFFERED" \
"$(nos_etc_dir)/env.d/PYTHONUNBUFFERED"
# Ensure the cache destination exists for site-packages
if [[ ! -d "$(nos_code_dir)/.nanobox/pip_cache/site-packages" ]]; then
mkdir -p "$(nos_code_dir)/.nanobox/pip_cache/site-packages"
fi
# If anything exists before we symlink, copy it into the cache
if [[ -d "$(nos_data_dir)/lib/$(lib_runtime)/site-packages" ]]; then
cp -a \
"$(nos_data_dir)/lib/$(lib_runtime)/site-packages" \
"$(nos_code_dir)/.nanobox/pip_cache/"
fi
# set the profile script that correctly sets up the links
set_python_profile_script
}
# Generate the payload to render the python profile template
python_profile_payload() {
cat <<-END
{
"code_dir": "$(nos_code_dir)",
"data_dir": "$(nos_data_dir)",
"runtime": "$(lib_runtime)"
}
END
}
# Profile script to ensure symlinks for pip
set_python_profile_script() {
mkdir -p "$(nos_etc_dir)/profile.d"
nos_template \
"profile.d/pip.sh" \
"$(nos_etc_dir)/profile.d/pip.sh" \
"$(python_profile_payload)"
}
# fetch the user-specified pip install command or use a default
pip_install_cmd() {
echo $(nos_validate \
"$(nos_payload "config_pip_install")" \
"string" "$(default_pip_install)")
}
# the default pip install cmd when a user-specified cmd is not present
default_pip_install() {
echo "pip install -I -r requirements.txt"
}
# Install dependencies via pip from requirements.txt
pip_install() {
if [[ -f $(nos_code_dir)/requirements.txt ]]; then
cd $(nos_code_dir)
nos_run_process "Running pip install" \
"$(pip_install_cmd)"
cd - >/dev/null
fi
}