-
Notifications
You must be signed in to change notification settings - Fork 176
/
PythonBashBuildSnippet.sh.tpl
288 lines (256 loc) · 11.5 KB
/
PythonBashBuildSnippet.sh.tpl
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
set -e
# TODO: refactor redundant code. Work-item: 1476457
declare -r TS_FMT='[%T%z] '
declare -r REQS_NOT_FOUND_MSG='Could not find setup.py or requirements.txt; Not running pip install. More information: https://aka.ms/requirements-not-found'
echo "Python Version: $python"
PIP_CACHE_DIR=/usr/local/share/pip-cache
{{ if PythonBuildCommandsFileName | IsNotBlank }}
COMMAND_MANIFEST_FILE="{{ PythonBuildCommandsFileName }}"
{{ end }}
echo "Creating directory for command manifest file if it does not exist"
mkdir -p "$(dirname "$COMMAND_MANIFEST_FILE")"
echo "Removing existing manifest file"
rm -f "$COMMAND_MANIFEST_FILE"
echo "PlatformWithVersion=Python {{ PythonVersion }}" > "$COMMAND_MANIFEST_FILE"
InstallCommand=""
if [ ! -d "$PIP_CACHE_DIR" ];then
mkdir -p $PIP_CACHE_DIR
fi
{{ if CustomRequirementsTxtPath | IsNotBlank }}
REQUIREMENTS_TXT_FILE="{{ CustomRequirementsTxtPath }}"
{{ else }}
REQUIREMENTS_TXT_FILE="requirements.txt"
{{ end }}
{{ if VirtualEnvironmentName | IsNotBlank }}
{{ if PackagesDirectory | IsNotBlank }}
if [ -d "{{ PackagesDirectory }}" ]
then
rm -fr "{{ PackagesDirectory }}"
fi
{{ end }}
VIRTUALENVIRONMENTNAME={{ VirtualEnvironmentName }}
VIRTUALENVIRONMENTMODULE={{ VirtualEnvironmentModule }}
VIRTUALENVIRONMENTOPTIONS="{{ VirtualEnvironmentParameters }}"
zippedVirtualEnvFileName={{ CompressedVirtualEnvFileName }}
echo "Python Virtual Environment: $VIRTUALENVIRONMENTNAME"
if [ -e "$REQUIREMENTS_TXT_FILE" ]; then
VIRTUALENVIRONMENTOPTIONS="$VIRTUALENVIRONMENTOPTIONS --system-site-packages"
fi
echo Creating virtual environment...
CreateVenvCommand="$python -m $VIRTUALENVIRONMENTMODULE $VIRTUALENVIRONMENTNAME $VIRTUALENVIRONMENTOPTIONS"
echo "BuildCommands=$CreateVenvCommand" >> "$COMMAND_MANIFEST_FILE"
$python -m $VIRTUALENVIRONMENTMODULE $VIRTUALENVIRONMENTNAME $VIRTUALENVIRONMENTOPTIONS
echo Activating virtual environment...
printf %s " , $ActivateVenvCommand" >> "$COMMAND_MANIFEST_FILE"
ActivateVenvCommand="source $VIRTUALENVIRONMENTNAME/bin/activate"
source $VIRTUALENVIRONMENTNAME/bin/activate
moreInformation="More information: https://aka.ms/troubleshoot-python"
if [ -e "$REQUIREMENTS_TXT_FILE" ]
then
set +e
echo "Running pip install..."
InstallCommand="python -m pip install --cache-dir $PIP_CACHE_DIR --prefer-binary -r $REQUIREMENTS_TXT_FILE | ts $TS_FMT"
printf %s " , $InstallCommand" >> "$COMMAND_MANIFEST_FILE"
StdError=$( ( python -m pip install --cache-dir $PIP_CACHE_DIR --prefer-binary -r $REQUIREMENTS_TXT_FILE | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pipInstallExitCode=${PIPESTATUS[0]}
set -e
if [[ $pipInstallExitCode != 0 ]]
then
StdError=$(echo "$StdError" | sed '/^error/I!d')
LogError "${StdError} | Exit code: ${pipInstallExitCode} | Please review your requirements.txt | ${moreInformation}"
exit $pipInstallExitCode
fi
elif [ -e "setup.py" ]
then
set +e
echo "Running python setup.py install..."
InstallCommand="$python setup.py install --user| ts $TS_FMT"
printf %s " , $InstallCommand" >> "$COMMAND_MANIFEST_FILE"
StdError=$( ( $python setup.py install --user| ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pythonBuildExitCode=${PIPESTATUS[0]}
set -e
if [[ $pythonBuildExitCode != 0 ]]
then
LogError "${StdError} | Exit code: ${pipInstallExitCode} | Please review your setup.py | ${moreInformation}"
exit $pythonBuildExitCode
fi
elif [ -e "pyproject.toml" ]
then
set +e
echo "Running pip install poetry..."
InstallPipCommand="pip install poetry"
printf %s " , $InstallPipCommand" >> "$COMMAND_MANIFEST_FILE"
pip install poetry
echo "Running poetry install..."
InstallPoetryCommand="poetry install"
printf %s " , $InstallPoetryCommand" >> "$COMMAND_MANIFEST_FILE"
StdWarning=$( ( poetry install; exit ${PIPESTATUS[0]} ) 2>&1)
pythonBuildExitCode=${PIPESTATUS[0]}
set -e
if [[ $pythonBuildExitCode != 0 ]]
then
LogWarning "${StdWarning} | Exit code: {pythonBuildExitCode} | Please review message | ${moreInformation}"
exit $pythonBuildExitCode
fi
else
echo $REQS_NOT_FOUND_MSG
fi
# For virtual environment, we use the actual 'python' alias that as setup by the venv,
python_bin=python
{{ else }}
moreInformation="More information: https://aka.ms/troubleshoot-python"
if [ -e "$REQUIREMENTS_TXT_FILE" ]
then
set +e
echo
echo Running pip install...
START_TIME=$SECONDS
InstallCommand="$python -m pip install --cache-dir $PIP_CACHE_DIR --prefer-binary -r $REQUIREMENTS_TXT_FILE --target="{{ PackagesDirectory }}" --upgrade | ts $TS_FMT"
printf %s " , $InstallCommand" >> "$COMMAND_MANIFEST_FILE"
StdError=$( ( $python -m pip install --cache-dir $PIP_CACHE_DIR --prefer-binary -r $REQUIREMENTS_TXT_FILE --target="{{ PackagesDirectory }}" --upgrade | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pipInstallExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Done in $ELAPSED_TIME sec(s)."
set -e
if [[ $pipInstallExitCode != 0 ]]
then
StdError=$(echo "$StdError" | sed '/^error/I!d')
LogError "${StdError} | Exit code: ${pipInstallExitCode} | Please review your requirements.txt | ${moreInformation}"
exit $pipInstallExitCode
fi
elif [ -e "setup.py" ]
then
echo
START_TIME=$SECONDS
UpgradeCommand="pip install --upgrade pip"
printf %s " , $UpgradeCommand" >> "$COMMAND_MANIFEST_FILE"
pip install --upgrade pip
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Done in $ELAPSED_TIME sec(s)."
set +e
echo "Running python setup.py install..."
InstallCommand="$python setup.py install --user| ts $TS_FMT"
printf %s " , $InstallCommand" >> "$COMMAND_MANIFEST_FILE"
StdError=$( ( $python setup.py install --user| ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pythonBuildExitCode=${PIPESTATUS[0]}
set -e
if [[ $pythonBuildExitCode != 0 ]]
then
LogError "${StdError} | Exit code: ${pipInstallExitCode} | Please review your setup.py | ${moreInformation}"
exit $pythonBuildExitCode
fi
elif [ -e "pyproject.toml" ]
then
set +e
echo "Running pip install poetry..."
InstallPipCommand="pip install poetry"
printf %s " , $InstallPipCommand" >> "$COMMAND_MANIFEST_FILE"
pip install poetry
START_TIME=$SECONDS
echo "Running poetry install..."
InstallPoetryCommand="poetry install"
printf %s " , $InstallPoetryCommand" >> "$COMMAND_MANIFEST_FILE"
StdWarning=$( ( poetry install; exit ${PIPESTATUS[0]} ) 2>&1 )
pythonBuildExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Done in $ELAPSED_TIME sec(s)."
set -e
if [[ $pythonBuildExitCode != 0 ]]
then
LogWarning "${StdWarning} | Exit code: {pythonBuildExitCode} | Please review message | ${moreInformation}"
exit $pythonBuildExitCode
fi
else
echo $REQS_NOT_FOUND_MSG
fi
# We need to use the python binary selected by benv
python_bin=$python
# Detect the location of the site-packages to add the .pth file
# For the local site package, only major and minor versions are provided, so we fetch it again
SITE_PACKAGE_PYTHON_VERSION=$($python -c "import sys; print(str(sys.version_info.major) + '.' + str(sys.version_info.minor))")
SITE_PACKAGES_PATH=$PIP_CACHE_DIR"/lib/python"$SITE_PACKAGE_PYTHON_VERSION"/site-packages"
mkdir -p $SITE_PACKAGES_PATH
# To make sure the packages are available later, e.g. for collect static or post-build hooks, we add a .pth pointing to them
APP_PACKAGES_PATH=$(pwd)"/{{ PackagesDirectory }}"
echo $APP_PACKAGES_PATH > $SITE_PACKAGES_PATH"/oryx.pth"
{{ end }}
{{ if RunPythonPackageCommand }}
echo
echo "Running python packaging commands ...."
echo
echo "Determining python package wheel ...."
{{ if PythonPackageWheelProperty }}
echo "Creating universal package wheel ...."
{{ end }}
PackageWheelCommand=""
if [ -z "{{ PythonPackageWheelProperty }}" ]
then
echo "Creating non universal package wheel ...."
PackageWheelCommand="$python setup.py sdist --formats=gztar,zip,tar bdist_wheel"
$python setup.py sdist --formats=gztar,zip,tar bdist_wheel
else
PackageWheelCommand="$python setup.py sdist --formats=gztar,zip,tar bdist_wheel --universal"
$python setup.py sdist --formats=gztar,zip,tar bdist_wheel --universal
fi
PackageEggCommand="$python setup.py bdist_egg"
echo "Now creating python package egg ...."
printf %s " , $PackageWheelCommand, $PackageEggCommand" >> "$COMMAND_MANIFEST_FILE"
$python setup.py bdist_egg
echo
{{ end }}
{{ if EnableCollectStatic }}
set +e
if [ -e "$SOURCE_DIR/manage.py" ]
then
if grep -iq "Django" "$SOURCE_DIR/$REQUIREMENTS_TXT_FILE"
then
echo
echo Content in source directory is a Django app
echo Running 'collectstatic'...
START_TIME=$SECONDS
CollectStaticCommand="$python_bin manage.py collectstatic --noinput"
printf %s " , $CollectStaticCommand" >> "$COMMAND_MANIFEST_FILE"
StdWarning=$(($python_bin manage.py collectstatic --noinput; exit ${PIPESTATUS[0]}) 2>&1)
EXIT_CODE=${PIPESTATUS[0]}
if [[ $EXIT_CODE != 0 ]]
then
recommendation="Please review message"
LogWarning "${StdWarning} | Exit code: ${EXIT_CODE} | ${recommendation} | ${moreInformation}"
fi
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Done in $ELAPSED_TIME sec(s)."
else
StdWarning="Missing Django modile in Missing Django module in $SOURCE_DIR/$REQUIREMENTS_TXT_FILE"
recommendation="Add Django to your requirements.txt file."
LogWarning "${StdWarning} | Exit code: 0 | ${recommendation} | ${moreInformation}"
fi
fi
set -e
{{ end }}
ReadImageType=$(cat /opt/oryx/.imagetype)
if [ "$ReadImageType" = "vso-focal" ]
then
echo $ReadImageType
cat "$COMMAND_MANIFEST_FILE"
else
echo "Not a vso image, so not writing build commands"
rm "$COMMAND_MANIFEST_FILE"
fi
{{ if VirtualEnvironmentName | IsNotBlank }}
{{ if CompressVirtualEnvCommand | IsNotBlank }}
if [ "$SOURCE_DIR" != "$DESTINATION_DIR" ]
then
if [ -d "$VIRTUALENVIRONMENTNAME" ]
then
echo
echo "Compressing existing '$VIRTUALENVIRONMENTNAME' folder..."
START_TIME=$SECONDS
# Make the contents of the virtual env folder appear in the zip file, not the folder itself
cd "$VIRTUALENVIRONMENTNAME"
{{ CompressVirtualEnvCommand }} ../$zippedVirtualEnvFileName .
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Done in $ELAPSED_TIME sec(s)."
fi
fi
{{ end }}
{{ end }}