-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathPythonBashBuildSnippet.sh.tpl
More file actions
602 lines (530 loc) · 23.8 KB
/
PythonBashBuildSnippet.sh.tpl
File metadata and controls
602 lines (530 loc) · 23.8 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
set -e
# TODO: refactor redundant code. Work-item: 1476457
declare -r TS_FMT='[%T%z] '
declare -r REQS_NOT_FOUND_MSG='Could not find requirements.txt, pyproject.toml, or setup.py; Not installing dependencies. More information: https://aka.ms/requirements-not-found'
echo "Python Version: $python"
# Cache directories for package installation
PIP_CACHE_DIR=/usr/local/share/pip-cache
UV_PIP_CACHE_DIR=/usr/local/share/uv-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=""
# Create PIP cache directory if it doesn't exist
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 }}
# Function to install packages via uv
install_via_uv() {
# Create UV cache directory if it doesn't exist
if [ ! -d "$UV_PIP_CACHE_DIR" ];then
mkdir -p $UV_PIP_CACHE_DIR
fi
START_TIME=$SECONDS
local python_cmd=$1
local requirements_file=$2
local target_dir=$3
local upgrade_flag=$4
# Install uv if not already available
if ! command -v uv &> /dev/null; then
echo "Installing uv..."
local install_uv_cmd="$python_cmd -m pip install uv"
printf %s " , $install_uv_cmd" >> "$COMMAND_MANIFEST_FILE"
$python_cmd -m pip install uv
else
echo "uv is already installed, skipping installation..."
fi
set +e
echo "Running uv pip install..."
# Build the command with --no-build to only use pre-built wheels
local base_cmd="uv pip install --cache-dir $UV_PIP_CACHE_DIR --compile-bytecode"
# Add find-links if PYTHON_PRELOADED_WHEELS_DIR is set
if [ -n "$PYTHON_PRELOADED_WHEELS_DIR" ]; then
echo "Using preloaded wheels from: $PYTHON_PRELOADED_WHEELS_DIR"
base_cmd="$base_cmd --find-links=$PYTHON_PRELOADED_WHEELS_DIR"
fi
base_cmd="$base_cmd -r $requirements_file"
if [ -n "$target_dir" ]; then
base_cmd="$base_cmd --target=\"$target_dir\""
fi
if [ -n "$upgrade_flag" ]; then
base_cmd="$base_cmd $upgrade_flag"
fi
# Log the command
local uv_cmd="$base_cmd | ts $TS_FMT"
printf %s " , $uv_cmd" >> "$COMMAND_MANIFEST_FILE"
# Execute uv pip install (uv manages its own cache)
output=$( ( $base_cmd | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
local exit_code=${PIPESTATUS[0]}
echo "${output}"
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "uv pip install done in $ELAPSED_TIME sec(s)."
return $exit_code
}
# Function to install packages via pip
install_via_pip() {
START_TIME=$SECONDS
local python_cmd=$1
local requirements_file=$2
local target_dir=$3
local upgrade_flag=$4
set +e
echo "Running pip install..."
# Build the command
local base_cmd="$python_cmd -m pip install --cache-dir $PIP_CACHE_DIR --prefer-binary -r $requirements_file"
if [ -n "$target_dir" ]; then
base_cmd="$base_cmd --target=\"$target_dir\""
fi
if [ -n "$upgrade_flag" ]; then
base_cmd="$base_cmd $upgrade_flag"
fi
# Add find-links if PYTHON_PRELOADED_WHEELS_DIR is set
if [ -n "$PYTHON_PRELOADED_WHEELS_DIR" ]; then
echo "Using preloaded wheels from: $PYTHON_PRELOADED_WHEELS_DIR"
base_cmd="$base_cmd --find-links=$PYTHON_PRELOADED_WHEELS_DIR"
fi
# Log the command
local pip_cmd="$base_cmd | ts $TS_FMT"
printf %s " , $pip_cmd" >> "$COMMAND_MANIFEST_FILE"
# Execute pip install
output=$( ( $base_cmd | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
local exit_code=${PIPESTATUS[0]}
echo "${output}"
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "pip install done in $ELAPSED_TIME sec(s)."
return $exit_code
}
# Internal function to install packages with uv and fallback to pip
install_python_packages_impl() {
local python_cmd=$1
local requirements_file=$2
local target_dir=$3
local upgrade_flag=$4
set +e
# Try uv first
install_via_uv "$python_cmd" "$requirements_file" "$target_dir" "$upgrade_flag"
local exit_code=$?
# Fallback to pip if uv fails
if [[ $exit_code != 0 ]]; then
echo "uv pip install failed with exit code ${exit_code}, falling back to pip install..."
install_via_pip "$python_cmd" "$requirements_file" "$target_dir" "$upgrade_flag"
exit_code=$?
fi
set -e
return $exit_code
}
{{ 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 "pyproject.toml" ] && [ -e "uv.lock" ] && [ ! -e "$REQUIREMENTS_TXT_FILE" ]; then
echo "Detected uv.lock (and no $REQUIREMENTS_TXT_FILE)"
echo "Installing uv..."
START_TIME=$SECONDS
InstallUv="python -m pip install uv"
printf %s " , $InstallUv" >> "$COMMAND_MANIFEST_FILE"
$python -m pip install uv
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Installing uv done in $ELAPSED_TIME sec(s)."
CreateVenvCommand="uv venv --link-mode=copy --system-site-packages $VIRTUALENVIRONMENTNAME"
VIRTUALENVIRONMENTOPTIONS="$VIRTUALENVIRONMENTOPTIONS --system-site-packages"
else
if [ -e "$REQUIREMENTS_TXT_FILE" ]; then
VIRTUALENVIRONMENTOPTIONS="$VIRTUALENVIRONMENTOPTIONS --system-site-packages"
fi
CreateVenvCommand="$python -m $VIRTUALENVIRONMENTMODULE $VIRTUALENVIRONMENTNAME $VIRTUALENVIRONMENTOPTIONS"
fi
if [ "$CreateVenvWithPythonVenv" = "true" ]; then
CreateVenvCommand="$python -m $VIRTUALENVIRONMENTMODULE $VIRTUALENVIRONMENTNAME $VIRTUALENVIRONMENTOPTIONS"
fi
echo Creating virtual environment...
echo "BuildCommands=$CreateVenvCommand" >> "$COMMAND_MANIFEST_FILE"
# Execute the resolved CreateVenvCommand
echo "Executing: $CreateVenvCommand"
$CreateVenvCommand
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
if [ "$PYTHON_FAST_BUILD_ENABLED" = "true" ]; then
set +e
echo "Fast build is enabled"
install_python_packages_impl "python" "$REQUIREMENTS_TXT_FILE" "" ""
pipInstallExitCode=$?
set -e
if [[ $pipInstallExitCode != 0 ]]
then
LogError "Package installation failed | Exit code: ${pipInstallExitCode} | Please review your requirements.txt | ${moreInformation}"
exit $pipInstallExitCode
fi
else
set +e
echo "Running pip install..."
START_TIME=$SECONDS
InstallCommand="python -m pip install --cache-dir $PIP_CACHE_DIR --prefer-binary -r $REQUIREMENTS_TXT_FILE"
# Add find-links if PYTHON_PRELOADED_WHEELS_DIR is set
if [ -n "$PYTHON_PRELOADED_WHEELS_DIR" ]; then
echo "Using preloaded wheels from: $PYTHON_PRELOADED_WHEELS_DIR"
InstallCommand="$InstallCommand --find-links=$PYTHON_PRELOADED_WHEELS_DIR"
fi
printf %s " , $InstallCommand | ts $TS_FMT" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallCommand | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pipInstallExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
set -e
echo "${output}"
echo "pip install done in $ELAPSED_TIME sec(s)."
if [[ $pipInstallExitCode != 0 ]]
then
LogError "${output} | Exit code: ${pipInstallExitCode} | Please review your requirements.txt | ${moreInformation}"
exit $pipInstallExitCode
fi
fi
elif [ -e "setup.py" ]
then
set +e
echo "Running pip install setuptools..."
START_TIME=$SECONDS
InstallSetuptoolsPipCommand="pip install setuptools"
printf %s " , $InstallSetuptoolsPipCommand" >> "$COMMAND_MANIFEST_FILE"
pip install setuptools
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "pip install setuptools done in $ELAPSED_TIME sec(s)."
echo "Running python setup.py install..."
START_TIME=$SECONDS
InstallCommand="pip install . --cache-dir $PIP_CACHE_DIR --prefer-binary | ts $TS_FMT"
printf %s " , $InstallCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( pip install . --cache-dir $PIP_CACHE_DIR --prefer-binary | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pythonBuildExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
set -e
echo "${output}"
echo "pip install done in $ELAPSED_TIME sec(s)."
if [[ $pythonBuildExitCode != 0 ]]
then
LogError "${output} | Exit code: ${pipInstallExitCode} | Please review your setup.py | ${moreInformation}"
exit $pythonBuildExitCode
fi
elif [ -e "pyproject.toml" ]
then
if [ -e "uv.lock" ];
then
# Install using uv
set +e
echo "Detected uv.lock. Installing dependencies with uv..."
START_TIME=$SECONDS
InstallUvCommand="uv sync --active --link-mode copy"
printf %s " , $InstallUvCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallUvCommand; exit ${PIPESTATUS[0]} ) 2>&1 )
uvExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "uv sync done in $ELAPSED_TIME sec(s)."
set -e
echo "${output}"
if [[ $uvExitCode != 0 ]]; then
LogError "${output} | Exit code: ${uvExitCode} | Please review your uv.lock | ${moreInformation}"
exit $uvExitCode
fi
else
# Fallback to poetry
set +e
echo "Running pip install poetry..."
START_TIME=$SECONDS
InstallPipCommand="pip install poetry"
printf %s " , $InstallPipCommand" >> "$COMMAND_MANIFEST_FILE"
pip install poetry
echo "Running poetry install..."
# Try with --only main flag as --no-dev option is depreciated in latest poetry versions
InstallPoetryCommand="poetry install --only main"
printf %s " , $InstallPoetryCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallPoetryCommand; exit ${PIPESTATUS[0]} ) 2>&1)
pythonBuildExitCode=${PIPESTATUS[0]}
# Fallback to --no-dev flag
if [[ $pythonBuildExitCode != 0 ]]; then
echo "poetry install failed with --only main flag, falling back to --no-dev"
pip install poetry==1.8.5
InstallPoetryCommand="poetry install --no-dev"
printf %s " , $InstallPoetryCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallPoetryCommand; exit ${PIPESTATUS[0]} ) 2>&1)
pythonBuildExitCode=${PIPESTATUS[0]}
# Final check after fallback
if [[ $pythonBuildExitCode != 0 ]]; then
set -e
echo "${output}"
LogWarning "${output} | Exit code: ${pythonBuildExitCode} | Please review message | ${moreInformation}"
exit $pythonBuildExitCode
fi
fi
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "poetry install done in $ELAPSED_TIME sec(s)."
set -e
echo "${output}"
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
if [ "$PYTHON_FAST_BUILD_ENABLED" = "true" ]; then
set +e
echo "Fast build is enabled"
install_python_packages_impl "python" "$REQUIREMENTS_TXT_FILE" "" ""
pipInstallExitCode=$?
set -e
if [[ $pipInstallExitCode != 0 ]]
then
LogError "Package installation failed | Exit code: ${pipInstallExitCode} | Please review your requirements.txt | ${moreInformation}"
exit $pipInstallExitCode
fi
else
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 }}" {{ PipUpgradeFlag }}"
# Add find-links if PYTHON_PRELOADED_WHEELS_DIR is set
if [ -n "$PYTHON_PRELOADED_WHEELS_DIR" ]; then
echo "Using preloaded wheels from: $PYTHON_PRELOADED_WHEELS_DIR"
InstallCommand="$InstallCommand --find-links=$PYTHON_PRELOADED_WHEELS_DIR"
fi
printf %s " , $InstallCommand | ts $TS_FMT" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallCommand | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pipInstallExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
set -e
echo "${output}"
echo "pip install done in $ELAPSED_TIME sec(s)."
if [[ $pipInstallExitCode != 0 ]]
then
LogError "${output} | Exit code: ${pipInstallExitCode} | Please review your requirements.txt | ${moreInformation}"
exit $pipInstallExitCode
fi
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 "pip upgrade done in $ELAPSED_TIME sec(s)."
set +e
echo "Running pip install setuptools..."
START_TIME=$SECONDS
InstallSetuptoolsPipCommand="pip install setuptools"
printf %s " , $InstallSetuptoolsPipCommand" >> "$COMMAND_MANIFEST_FILE"
pip install setuptools
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "pip install setuptools done in $ELAPSED_TIME sec(s)."
echo "Running pip install..."
START_TIME=$SECONDS
InstallCommand="$python -m pip install . --cache-dir $PIP_CACHE_DIR --prefer-binary --target=\"{{ PackagesDirectory }}\" {{ PipUpgradeFlag }} | ts $TS_FMT"
printf %s " , $InstallCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $python -m pip install . --cache-dir $PIP_CACHE_DIR --prefer-binary --target="{{ PackagesDirectory }}" {{ PipUpgradeFlag }} | ts $TS_FMT; exit ${PIPESTATUS[0]} ) 2>&1; exit ${PIPESTATUS[0]} )
pythonBuildExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
set -e
echo "${output}"
echo "pip install done in $ELAPSED_TIME sec(s)."
if [[ $pythonBuildExitCode != 0 ]]
then
LogError "${output} | Exit code: ${pipInstallExitCode} | Please review your setup.py | ${moreInformation}"
exit $pythonBuildExitCode
fi
elif [ -e "pyproject.toml" ]
then
if [ -e "uv.lock" ];
then
# Install using uv
echo "Detected uv.lock. Installing dependencies with uv..."
START_TIME=$SECONDS
echo "Installing uv..."
InstallUv="python -m pip install uv"
printf %s " , $InstallUv" >> "$COMMAND_MANIFEST_FILE"
$python -m pip install uv
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "Installing uv done in $ELAPSED_TIME sec(s)."
START_TIME=$SECONDS
set +e
SITE_PACKAGES_PATH="{{ PackagesDirectory }}"
echo "Installing dependencies..."
# Stream the export directly into uv pip install using process substitution
InstallUvCommand="uv export --locked | uv pip install --link-mode copy --target $SITE_PACKAGES_PATH -r -"
printf %s " , $InstallUvCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( eval $InstallUvCommand; exit ${PIPESTATUS[0]} ) 2>&1 )
uvExitCode=${PIPESTATUS[0]}
ELAPSED_TIME=$(($SECONDS - $START_TIME))
set -e
echo "${output}"
echo "uv pip install done in $ELAPSED_TIME sec(s)."
if [[ $uvExitCode != 0 ]]; then
LogError "${output} | Exit code: ${uvExitCode} | Please review your uv.lock | ${moreInformation}"
exit $uvExitCode
fi
else
# Fallback to poetry
set +e
echo "Running pip install poetry..."
START_TIME=$SECONDS
InstallPipCommand="pip install poetry"
printf %s " , $InstallPipCommand" >> "$COMMAND_MANIFEST_FILE"
pip install poetry
echo "Running poetry install..."
# Try with --only main flag as --no-dev option is depreciated in latest poetry versions
InstallPoetryCommand="poetry install --only main"
printf %s " , $InstallPoetryCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallPoetryCommand; exit ${PIPESTATUS[0]} ) 2>&1)
pythonBuildExitCode=${PIPESTATUS[0]}
# Fallback to --no-dev flag
if [[ $pythonBuildExitCode != 0 ]]; then
echo "poetry install failed with --only main flag, falling back to --no-dev"
pip install poetry==1.8.5
InstallPoetryCommand="poetry install --no-dev"
printf %s " , $InstallPoetryCommand" >> "$COMMAND_MANIFEST_FILE"
output=$( ( $InstallPoetryCommand; exit ${PIPESTATUS[0]} ) 2>&1)
pythonBuildExitCode=${PIPESTATUS[0]}
# Final check after fallback
if [[ $pythonBuildExitCode != 0 ]]; then
set -e
echo "${output}"
LogWarning "${output} | Exit code: ${pythonBuildExitCode} | Please review message | ${moreInformation}"
exit $pythonBuildExitCode
fi
fi
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "poetry install done in $ELAPSED_TIME sec(s)."
set -e
echo "${output}"
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"
output=$(($python_bin manage.py collectstatic --noinput; exit ${PIPESTATUS[0]}) 2>&1)
EXIT_CODE=${PIPESTATUS[0]}
echo "${output}"
if [[ $EXIT_CODE != 0 ]]
then
recommendation="Please review message"
LogWarning "${output} | Exit code: ${EXIT_CODE} | ${recommendation} | ${moreInformation}"
fi
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "collectstatic done in $ELAPSED_TIME sec(s)."
else
output="Missing Django module in $SOURCE_DIR/$REQUIREMENTS_TXT_FILE"
recommendation="Add Django to your requirements.txt file."
LogWarning "${output} | Exit code: 0 | ${recommendation} | ${moreInformation}"
fi
fi
set -e
{{ end }}
ReadImageType=$(cat /opt/oryx/.imagetype)
if [ "$ReadImageType" = "vso-focal" ] || [ "$ReadImageType" = "vso-debian-bullseye" ]
then
echo $ReadImageType
cat "$COMMAND_MANIFEST_FILE"
else
echo "Not a vso image, so not writing build commands"
rm "$COMMAND_MANIFEST_FILE"
fi
# Copy requirements.txt to the destination directory if it exists
if [ "$SOURCE_DIR" != "$DESTINATION_DIR" ]
then
if [ -e "$SOURCE_DIR/$REQUIREMENTS_TXT_FILE" ]
then
echo
echo "Copying '$REQUIREMENTS_TXT_FILE' to destination directory..."
cp "$SOURCE_DIR/$REQUIREMENTS_TXT_FILE" "$DESTINATION_DIR/requirements.txt" || true
echo "Done copying requirements.txt to destination directory."
fi
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 "Compressing virtual environment done in $ELAPSED_TIME sec(s)."
fi
fi
{{ end }}
{{ end }}