@@ -14,19 +14,21 @@ cols=2
1414
1515# Define the 2D array
1616declare -A packages_array
17+ declare -A required_versions
1718
1819# Fill the 2D array
1920for (( i= 0 ; i< rows; i++ )) ; do
2021 # Split each element of vulnerable_packages by the '=' sign
2122 IFS=' =' read -ra parts <<< " ${vulnerable_packages[$i]}"
2223 # Assign the parts to the 2D array
23- packages_array[$i ,0]=${parts[0]}
24- packages_array[$i ,1]=${parts[1]}
24+ packages_array[" $i ,0" ]=" ${parts[0]} "
25+ packages_array[" $i ,1" ]=" ${parts[1]} "
26+ required_versions[" ${parts[0]} " ]=" ${parts[1]} "
2527done
2628
2729# Add an array for packages that should always pin to the provided version,
2830# even if higher version is available in conda channel
29- pin_to_required_version=(" transformers" " imagecodecs" " brotli" )
31+ pin_to_required_version=(" transformers" " imagecodecs" " brotli" " protobuf " )
3032
3133# Function to check if a package is in the pin_to_required_version array
3234function is_pin_to_required_version() {
@@ -40,38 +42,51 @@ function is_pin_to_required_version() {
4042}
4143
4244for (( i= 0 ; i< rows; i++ )) ; do
43- CURRENT_VERSION=$( pip show " ${packages_array[$i,0]} " --disable-pip-version-check | grep ' ^Version:' | awk ' {print $2}' )
44- REQUIRED_VERSION=" ${packages_array[$i,1]} "
45- GREATER_VERSION_A=$(( echo ${REQUIRED_VERSION} ; echo ${CURRENT_VERSION} ) | sort - V | tail - 1 )
45+ CURRENT_VERSION=$( pip show " ${packages_array["$i,0"]} " --disable-pip-version-check | grep ' ^Version:' | awk ' {print $2}' )
46+ REQUIRED_VERSION=" ${packages_array["$i,1"]} "
47+ if is_pin_to_required_version " ${packages_array["$i,0"]} " ; then
48+ continue
49+ fi
50+ GREATER_VERSION_A=$( (echo ${REQUIRED_VERSION} ; echo ${CURRENT_VERSION} ) | sort -V | tail -1)
4651 # Check if the required_version is greater than current_version
4752 if [[ $CURRENT_VERSION != $GREATER_VERSION_A ]]; then
48- echo "${packages_array[$i,0]} version v${CURRENT_VERSION} installed by the base image is not greater or equal to the required: v${REQUIRED_VERSION} "
53+ echo " ${packages_array[" $i,0" ]} version v${CURRENT_VERSION} installed by the base image is not greater or equal to the required: v${REQUIRED_VERSION} "
4954 # Check whether conda channel has a greater or equal version available, so install from conda, otherwise use pip package manager
5055 channel_name=" anaconda"
51- CONDA_VERSION=$(conda search "${packages_array[$i,0]} " - c "$channel_name " | \
56+ CONDA_VERSION=$( conda search " ${packages_array[" $i,0" ]} " -c " $channel_name " | \
5257 grep -E ' ^[[:alnum:]]' | \
5358 awk ' {print $2}' | \
5459 sort -V | \
5560 uniq | \
5661 tail -n 2 | \
5762 head -n 1)
5863 if [[ -z " $CONDA_VERSION " ]]; then
59- echo "No version for ${packages_array[$i,0]} found in conda channel."
64+ echo " No version for ${packages_array[" $i,0" ]} found in conda channel."
6065 CONDA_VERSION=" 0"
6166 fi
62- GREATER_VERSION_B=$((echo ${REQUIRED_VERSION} ; echo ${CONDA_VERSION} ) | sort - V | tail - 1 )
63- if is_pin_to_required_version "${packages_array[$i,0]} "; then
64- echo -e "Package ${packages_array[$i,0]} is set to always use the required version: v${REQUIRED_VERSION} .\n";
65- echo "Installing ${packages_array[$i,0]} from pip for v${REQUIRED_VERSION} ..."
66- python3 -m pip install --upgrade --no-cache-dir "${packages_array[$i,0]} == ${REQUIRED_VERSION} "
67- elif [[ $CONDA_VERSION == $GREATER_VERSION_B ]]; then
67+ GREATER_VERSION_B=$( (echo ${REQUIRED_VERSION} ; echo ${CONDA_VERSION} ) | sort -V | tail -1)
68+ if [[ $CONDA_VERSION == $GREATER_VERSION_B ]]; then
6869 echo -e " Found Version v${CONDA_VERSION} in the Conda channel which is greater than or equal to the required version: v${REQUIRED_VERSION} . \n" ;
69- echo "Installing ${packages_array[$i,0]} from source from conda channel for v${REQUIRED_VERSION} ..."
70- conda install "${packages_array[$i,0]} == ${CONDA_VERSION} "
70+ echo " Installing ${packages_array[" $i,0" ]} from source from conda channel for v${REQUIRED_VERSION} ..."
71+ conda install " ${packages_array[" $i,0" ]} ==${CONDA_VERSION} "
7172 elif [[ $REQUIRED_VERSION == $GREATER_VERSION_B ]]; then
7273 echo -e " Required version: v${REQUIRED_VERSION} is greater than the version found in the Conda channel v${CONDA_VERSION} . \n" ;
73- echo "Installing ${packages_array[$i,0]} from source from pip package manager for v${REQUIRED_VERSION} ..."
74- python3 -m pip install --upgrade --no-cache-dir "${packages_array[$i,0]} == ${REQUIRED_VERSION} "
74+ echo " Installing ${packages_array[" $i,0" ]} from source from pip package manager for v${REQUIRED_VERSION} ..."
75+ python3 -m pip install --upgrade --no-cache-dir " ${packages_array[" $i,0" ]} ==${REQUIRED_VERSION} "
7576 fi
7677 fi
7778done
79+
80+ # After the main upgrade loop, install packages from the pin_to_required_version list at their required versions to keep exact versions even if conda upgrades them as dependencies.
81+ for pkg in " ${pin_to_required_version[@]} " ; do
82+ REQUIRED_VERSION=" ${required_versions["$pkg"]} "
83+ if [[ -z " ${REQUIRED_VERSION} " ]]; then
84+ echo " WARNING: Missing required version for ${pkg} . Skipping installation."
85+ continue
86+ fi
87+ CURRENT_VERSION=$( pip show " ${pkg} " --disable-pip-version-check | grep ' ^Version:' | awk ' {print $2}' )
88+ if [[ " ${CURRENT_VERSION} " != " ${REQUIRED_VERSION} " ]]; then
89+ echo " Installing ${pkg} from pip for v${REQUIRED_VERSION} ..."
90+ python3 -m pip install --upgrade --no-cache-dir " ${pkg} ==${REQUIRED_VERSION} "
91+ fi
92+ done
0 commit comments