1+ #! /bin/bash
2+
3+ set -e
4+
5+ # Optional: Import test library
6+ source dev-container-features-test-lib
7+
8+ check " python version 3.11 installed as default" bash -c " python --version | grep 3.11"
9+ check " python3 version 3.11 installed as default" bash -c " python3 --version | grep 3.11"
10+ check " python version 3.10.5 installed" bash -c " ls -l /usr/local/python | grep 3.10.5"
11+ check " python version 3.8 installed" bash -c " ls -l /usr/local/python | grep 3.8"
12+ check " python version 3.9.13 installed" bash -c " ls -l /usr/local/python | grep 3.9.13"
13+
14+ # Check paths in settings
15+ check " current symlink is correct" bash -c " which python | grep /usr/local/python/current/bin/python"
16+ check " current symlink works" /usr/local/python/current/bin/python --version
17+
18+ # check alternatives command
19+ check_version_switch () {
20+ if type apt-get > /dev/null 2>&1 ; then
21+ PYTHON_ALTERNATIVES=$( update-alternatives --query python3 | grep -E ' Alternative:|Priority:' )
22+ STYLE=" debian"
23+ elif type dnf > /dev/null 2>&1 || type yum > /dev/null 2>&1 || type microdnf > /dev/null 2>&1 ; then
24+ PYTHON_ALTERNATIVES=$( alternatives --display python3 | grep " - priority" )
25+ STYLE=" fedora"
26+ else
27+ echo " No supported package manager found."
28+ exit 1
29+ fi
30+ AVAILABLE_VERSIONS=()
31+ INDEX=1
32+ echo " Available Python versions:"
33+ if [ " ${STYLE} " = " debian" ]; then
34+ while read -r alt && read -r pri; do
35+ PATH=${alt# Alternative: } # Extract only the path
36+ PRIORITY=${pri# Priority: } # Extract only the priority number
37+ TEMP_VERSIONS+=(" ${PRIORITY} ${PATH} " )
38+ echo " $INDEX ) $PATH (Priority: $PRIORITY )"
39+ (( INDEX++ ))
40+ done <<< " ${PYTHON_ALTERNATIVES}"
41+ elif [ " ${STYLE} " = " fedora" ]; then
42+ export PATH=" /usr/bin:$PATH "
43+ # Fedora/RHEL output: one line per alternative in the format:
44+ while IFS= read -r line; do
45+ # Split using " - priority " as a delimiter.
46+ PATH=$( /usr/bin/awk -F' - priority ' ' {print $1}' <<< " $line" | /usr/bin/xargs /bin/echo)
47+ PRIORITY=$( /usr/bin/awk -F' - priority ' ' {print $2}' <<< " $line" | /usr/bin/xargs /bin/echo)
48+ TEMP_VERSIONS+=(" ${PRIORITY} ${PATH} " )
49+ echo " $INDEX ) $PATH (Priority: $PRIORITY_VALUE )"
50+ (( INDEX++ ))
51+ done <<< " ${PYTHON_ALTERNATIVES}"
52+ fi
53+
54+ export PATH=" /usr/bin:$PATH "
55+ # Sort by priority (numerically ascending)
56+ IFS=$' \n ' TEMP_VERSIONS=($( sort -n <<< " ${TEMP_VERSIONS[*]}" ) )
57+ unset IFS
58+
59+ # Populate AVAILABLE_VERSIONS from sorted data
60+ AVAILABLE_VERSIONS=()
61+ INDEX=1
62+ echo -e " \nAvailable Python versions (Sorted in asc order of priority):"
63+ for ENTRY in " ${TEMP_VERSIONS[@]} " ; do
64+ PRIORITY=${ENTRY%% * } # Extract priority (first part before space)
65+ PATH=${ENTRY#* } # Extract path (everything after first space)
66+ AVAILABLE_VERSIONS+=(" ${PATH} " )
67+ echo " $INDEX ) $PATH (Priority: $PRIORITY )"
68+ (( INDEX++ ))
69+ done
70+
71+ echo -e " \nAvailable Versions Count: ${# AVAILABLE_VERSIONS[@]} \n"
72+ # Ensure at least 4 alternatives exist
73+ if [ " ${# AVAILABLE_VERSIONS[@]} " -lt 4 ]; then
74+ echo " Error: Less than 4 Python versions registered in update-alternatives."
75+ exit 1
76+ fi
77+
78+ export PATH=" /usr/bin:$PATH "
79+ echo -e " \nSwitching to different versions using update-alternatives --set command...\n"
80+ for CHOICE in {1..4}; do
81+ SELECTED_VERSION=" ${AVAILABLE_VERSIONS[$((CHOICE - 1))]} "
82+ echo " Switching to: ${SELECTED_VERSION} "
83+ if command -v apt-get > /dev/null 2>&1 ; then
84+ /usr/bin/update-alternatives --set python3 ${SELECTED_VERSION}
85+ elif command -v dnf > /dev/null 2>&1 || command -v yum > /dev/null 2>&1 || command -v microdnf > /dev/null 2>&1 ; then
86+ /usr/sbin/alternatives --set python3 ${SELECTED_VERSION}
87+ fi
88+ # Verify the switch
89+ echo " Python version after switch:"
90+ /usr/local/python/current/bin/python3 --version
91+ /bin/sleep 1
92+ echo -e " \n"
93+ done
94+ echo -e " Update-Alternatives --display: \n"
95+ if type apt-get > /dev/null 2>&1 ; then
96+ /usr/bin/update-alternatives --display python3
97+ elif type dnf > /dev/null 2>&1 || type yum > /dev/null 2>&1 || type microdnf > /dev/null 2>&1 ; then
98+ /usr/sbin/alternatives --display python3
99+ fi
100+ }
101+
102+ check " Version Switch With Update_Alternatives" check_version_switch
0 commit comments