Skip to content

Commit 2627dcc

Browse files
07.Version Cache - Support for PIP (sonic-net#14613)
During build, lots of pip packages are getting installed through pip install command. This feature adds support for caching all the pip packages into local cache path, so that subsequent build always loads from the cache.
1 parent d4a81ea commit 2627dcc

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/sonic-build-hooks/scripts/buildinfo_base.sh

+40-11
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,37 @@ download_packages()
280280

281281
run_pip_command()
282282
{
283-
parameters=("$@")
283+
declare -a parameters=("$@")
284+
PIP_CACHE_PATH=${PKG_CACHE_PATH}/pip
285+
PKG_CACHE_OPTION="--cache-dir=${PIP_CACHE_PATH}"
284286

285-
if [ ! -x "$REAL_COMMAND" ] && [ " $1" == "freeze" ]; then
287+
if [[ ! -e ${PIP_CACHE_PATH} ]]; then
288+
${SUDO} mkdir -p ${PIP_CACHE_PATH}
289+
${SUDO} chmod 777 ${PIP_CACHE_PATH}
290+
fi
291+
292+
if [ ! -x "$REAL_COMMAND" ] && [ "$1" == "freeze" ]; then
286293
return 1
287294
fi
288295

289-
if [ "$ENABLE_VERSION_CONTROL_PY" != "y" ]; then
296+
if [[ "$SKIP_BUILD_HOOK" == y || "$ENABLE_VERSION_CONTROL_PY" != "y" ]]; then
297+
if [ ! -z "$(get_version_cache_option)" ]; then
298+
FLOCK ${PIP_CACHE_PATH}
299+
$REAL_COMMAND ${PKG_CACHE_OPTION} "$@"
300+
local result=$?
301+
chmod -f -R 777 ${PIP_CACHE_PATH}
302+
touch ${PIP_CACHE_PATH}
303+
FUNLOCK ${PIP_CACHE_PATH}
304+
return ${result}
305+
fi
290306
$REAL_COMMAND "$@"
291307
return $?
292308
fi
293309

310+
294311
local found=n
295312
local install=n
313+
local count=0
296314
local pip_version_file=$PIP_VERSION_FILE
297315
local tmp_version_file=$(mktemp)
298316
[ -f "$pip_version_file" ] && cp -f $pip_version_file $tmp_version_file
@@ -301,6 +319,7 @@ run_pip_command()
301319
([ "$para" == "-c" ] || [ "$para" == "--constraint" ]) && found=y
302320
if [ "$para" == "install" ]; then
303321
install=y
322+
parameters[${count}]=install
304323
elif [[ "$para" == *.whl ]]; then
305324
package_name=$(echo $para | cut -d- -f1 | tr _ .)
306325
$SUDO sed "/^${package_name}==/d" -i $tmp_version_file
@@ -309,20 +328,30 @@ run_pip_command()
309328
package_name=$(echo $para | cut -d= -f1)
310329
$SUDO sed "/^${package_name}==/d" -i $tmp_version_file
311330
fi
331+
(( count++ ))
312332
done
313333

314334
if [ "$found" == "n" ] && [ "$install" == "y" ]; then
315335
parameters+=("-c")
316336
parameters+=("${tmp_version_file}")
317337
fi
318338

319-
$REAL_COMMAND "${parameters[@]}"
320-
local result=$?
321-
if [ "$result" != 0 ]; then
322-
echo "Failed to run the command with constraint, try to install with the original command" 1>&2
323-
$REAL_COMMAND "$@"
324-
result=$?
325-
fi
339+
if [ ! -z "$(get_version_cache_option)" ]; then
340+
FLOCK ${PIP_CACHE_PATH}
341+
$REAL_COMMAND ${PKG_CACHE_OPTION} "${parameters[@]}"
342+
local result=$?
343+
chmod -f -R 777 ${PIP_CACHE_PATH}
344+
touch ${PIP_CACHE_PATH}
345+
FUNLOCK ${PIP_CACHE_PATH}
346+
else
347+
$REAL_COMMAND "${parameters[@]}"
348+
local result=$?
349+
if [ "$result" != 0 ]; then
350+
echo "Failed to run the command with constraint, try to install with the original command" 1>&2
351+
$REAL_COMMAND "$@"
352+
result=$?
353+
fi
354+
fi
326355
rm $tmp_version_file
327356
return $result
328357
}
@@ -348,7 +377,7 @@ check_apt_install()
348377
# Print warning message if a debian package version not specified when debian version control enabled.
349378
check_apt_version()
350379
{
351-
VERSION_FILE="/usr/local/share/buildinfo/versions/versions-deb"
380+
VERSION_FILE="${VERSION_PATH}/versions-deb"
352381
local install=$(check_apt_install "$@")
353382
if [ "$ENABLE_VERSION_CONTROL_DEB" == "y" ] && [ "$install" == "y" ]; then
354383
for para in "$@"

0 commit comments

Comments
 (0)