11#! /usr/bin/env bash
22
3- # Thanks CorpNewt for this source code
43# Get the curent directory, the script name
54# and the script name with "py" substituted for the extension.
6- printf ' \e[8;35;100t'
75args=( " $@ " )
86dir=" $( cd -- " $( dirname " $0 " ) " > /dev/null 2>&1 ; pwd -P) "
97script=" ${0##*/ } "
@@ -17,6 +15,8 @@ use_py3="FORCE"
1715
1816# We'll parse if the first argument passed is
1917# --install-python and if so, we'll just install
18+ # Can optionally take a version number as the
19+ # second arg - i.e. --install-python 3.13.1
2020just_installing=" FALSE"
2121
2222tempdir=" "
@@ -31,7 +31,7 @@ compare_to_version () {
3131 return
3232 fi
3333 local current_os= comp=
34- current_os=" $( sw_vers -productVersion) "
34+ current_os=" $( sw_vers -productVersion 2> /dev/null ) "
3535 comp=" $( vercomp " $current_os " " $2 " ) "
3636 # Check gequal and lequal first
3737 if [[ " $1 " == " 3" && (" $comp " == " 1" || " $comp " == " 0" ) ]] || [[ " $1 " == " 4" && (" $comp " == " 2" || " $comp " == " 0" ) ]] || [[ " $comp " == " $1 " ]]; then
@@ -82,68 +82,79 @@ download_py () {
8282 if [ -z " $vers " ]; then
8383 echo " Gathering latest version..."
8484 vers=" $( get_remote_py_version) "
85+ if [ -z " $vers " ]; then
86+ if [ " $just_installing " == " TRUE" ]; then
87+ echo " - Failed to get info!"
88+ exit 1
89+ else
90+ # Didn't get it still - bail
91+ print_error
92+ fi
93+ fi
94+ echo " Located Version: $vers "
95+ else
96+ # Got a version passed
97+ echo " User-Provided Version: $vers "
8598 fi
86- if [ -z " $vers " ]; then
87- # Didn't get it still - bail
88- print_error
89- fi
90- echo " Located Version: $vers "
91- echo
9299 echo " Building download url..."
93- url=" $( curl -L https://www.python.org/downloads/release/python-${vers// ./ } / --compressed 2>&1 | grep -iE " python-$vers -macos.*.pkg\" " | awk -F' "' ' { print $2 }' ) "
100+ url=" $( \
101+ curl -L https://www.python.org/downloads/release/python-${vers// ./ } / --compressed 2>&1 | \
102+ grep -iE " python-$vers -macos.*.pkg\" " | \
103+ grep -iE " a href=" | \
104+ awk -F' "' ' { print $2 }' | \
105+ head -n 1\
106+ ) "
94107 if [ -z " $url " ]; then
95- # Couldn't get the URL - bail
96- print_error
108+ if [ " $just_installing " == " TRUE" ]; then
109+ echo " - Failed to build download url!"
110+ exit 1
111+ else
112+ # Couldn't get the URL - bail
113+ print_error
114+ fi
97115 fi
98116 echo " - $url "
99- echo
100117 echo " Downloading..."
101- echo
102118 # Create a temp dir and download to it
103119 tempdir=" $( mktemp -d 2> /dev/null || mktemp -d -t ' tempdir' ) "
104120 curl " $url " -o " $tempdir /python.pkg"
105121 if [ " $? " != " 0" ]; then
106- echo
107122 echo " - Failed to download python installer!"
108- echo
109123 exit $?
110124 fi
111125 echo
112126 echo " Running python install package..."
113127 echo
114128 sudo installer -pkg " $tempdir /python.pkg" -target /
115129 if [ " $? " != " 0" ]; then
116- echo
117130 echo " - Failed to install python!"
118- echo
119131 exit $?
120132 fi
133+ echo
121134 # Now we expand the package and look for a shell update script
122135 pkgutil --expand " $tempdir /python.pkg" " $tempdir /python"
123136 if [ -e " $tempdir /python/Python_Shell_Profile_Updater.pkg/Scripts/postinstall" ]; then
124137 # Run the script
125- echo
126138 echo " Updating PATH..."
127139 echo
128140 " $tempdir /python/Python_Shell_Profile_Updater.pkg/Scripts/postinstall"
141+ echo
129142 fi
130143 vers_folder=" Python $( echo " $vers " | cut -d' .' -f1 -f2) "
131144 if [ -f " /Applications/$vers_folder /Install Certificates.command" ]; then
132145 # Certs script exists - let's execute that to make sure our certificates are updated
133- echo
134146 echo " Updating Certificates..."
135147 echo
136148 " /Applications/$vers_folder /Install Certificates.command"
149+ echo
137150 fi
138- echo
139151 echo " Cleaning up..."
140152 cleanup
141- echo
142153 if [ " $just_installing " == " TRUE" ]; then
154+ echo
143155 echo " Done."
144156 else
145157 # Now we check for py again
146- echo " Rechecking py..."
147158 downloaded=" TRUE"
148159 clear
149160 main
@@ -328,7 +339,7 @@ check_py3_stub="$(compare_to_version "3" "10.15")"
328339trap cleanup EXIT
329340if [ " $1 " == " --install-python" ] && [ " $kernel " == " Darwin" ]; then
330341 just_installing=" TRUE"
331- download_py
342+ download_py " $2 "
332343else
333344 main
334- fi
345+ fi
0 commit comments