forked from filcab/SublimeLLDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch-python-version
More file actions
executable file
·137 lines (120 loc) · 3.34 KB
/
switch-python-version
File metadata and controls
executable file
·137 lines (120 loc) · 3.34 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
#!/usr/bin/env zsh
Verb=
local default_sysroots='/'
local default_sdks_prefix="/Applications/Xcode45-DP2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs"
for i in {9..6}; do
if [[ -d "$default_sdks_prefix/MacOSX10.$i.sdk" ]]; then
default_sysroots+=" $default_sdks_prefix/MacOSX10.$i.sdk"
break
fi
done
local framework_dir=/System/Library/Frameworks/Python.framework/Versions
local usrlib_dir=/usr/lib
# default: latest version in /
local version=$(ls $framework_dir | sort | grep -v Current | tail -n 1 | tr -d '\n')
function py_version_print() {
v=$(cd $framework_dir && \
ls -ld Current | sed -e 's/^.*Current/Current/')
sdk_v=$(cd $usrlib_dir && ls -l libpython.dylib)
echo "Python.framework symlink: $v"
echo -n "dylib symlink: "
ls -l /usr/lib/libpython.dylib | sed -e 's/^[^/]*//'
echo -n "python --version: "
python --version
}
function list_versions {
echo "Available python versions:"
for v in $(cd $framework_dir && ls | grep -v Current); do
if [[ -d "$framework_dir/$v" && -h "/usr/lib/libpython$v.dylib" ]] \
&& type "python$v" >&-; then
echo $v
fi
done
}
while [[ "${1:0:1}" == "-" ]]; do
case "$1" in
-y|--dry-run)
Verb="echo"
shift
;;
-c|--check|-p|--print)
py_version_print
echo
list_versions
exit 0
;;
-l|--list)
list_versions
exit 0
;;
-h|--help)
py_version_print
echo
list_versions
echo "\nUse '$0 <version> [sysroot ...]' to change python version in the \
mentioned system roots."
echo Other options:
echo "-y|--dry-run\tPerform a dry run (only output commands, don't \
execute them"
echo
echo "-l|--list\tList available Python versions"
echo "-c|--check\tCheck the current Python version and list available \
versions."
echo "-p|--print\tSame as --check"
echo "-h|--help\tThis message"
exit 0
;;
esac
done
user_version=$version
if [[ ${#*} > 0 ]]; then
case "$1" in
2.[0-9])
echo Setting version $1
user_version=$1
shift
;;
esac
for sysroot in "$*"; do
sysroots="$sysroots $sysroot"
done
else
sysroots=$default_sysroots
fi
if [[ ! -z "$user_version" && -d "$framework_dir/$user_version"
&& -h "/usr/lib/libpython$user_version.dylib" ]] \
&& type "python$user_version" >&-; then
if [[ "$user_version" == "$version" ]]; then
# default: use the latest version
echo "Deleting versioner key"
$Verb defaults delete com.apple.versioner.python Version 2>&-
else
version=$user_version
echo "Writing versioner key 'Version = $version'"
$Verb defaults write com.apple.versioner.python Version $version
fi
else
echo "$1 is not a valid python version for this system."
list_versions
echo
py_version_print
exit 1
fi
echo "Changing symlink for Python.framework/Versions/Current"
echo "and symlink for /usr/lib/libpython.dylib for the following system roots:"
for s in ${=sysroots}; do
echo "\t" $s
done
echo to Python $version
for sysroot in ${=sysroots}; do
$Verb ${Verb/*}
($Verb cd $sysroot$framework_dir
$Verb sudo rm Current
$Verb sudo ln -s $version Current)
($Verb cd $sysroot$usrlib_dir
$Verb sudo rm libpython.dylib
$Verb sudo ln -s libpython$version.dylib libpython.dylib)
$Verb ${Verb/*}
done
echo
py_version_print "Updated"