Skip to content

Commit 1416dbb

Browse files
committed
postflight.patch-profile refactor
1 parent 980963d commit 1416dbb

1 file changed

Lines changed: 75 additions & 83 deletions

File tree

Lines changed: 75 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,108 @@
11
#!/bin/sh
22

3-
echo "This script will update your shell profile when the 'bin' directory"
4-
echo "of python is not early enough of the PATH of your shell."
5-
echo "These changes will be effective only in shell windows that you open"
6-
echo "after running this script."
7-
83
PYVER=@PYVER@
94
PYTHON_ROOT="/Library/Frameworks/Python.framework/Versions/@PYVER@"
105

11-
if [ `id -ur` = 0 ]; then
12-
# Run from the installer, do some trickery to fetch the information
13-
# we need.
14-
theShell="`finger $USER | grep Shell: | head -1 | awk '{ print $NF }'`"
6+
set -e
157

16-
else
17-
theShell="${SHELL}"
18-
fi
8+
# Run from the installer, do some trickery to fetch the information
9+
# we need.
10+
theShell="`finger $USER | grep Shell: | head -1 | awk '{ print $NF }'`"
1911

2012
# Make sure the directory ${PYTHON_ROOT}/bin is on the users PATH.
2113
BSH="`basename "${theShell}"`"
2214
case "${BSH}" in
2315
bash|ksh|sh|*csh|zsh|fish)
24-
if [ `id -ur` = 0 ]; then
25-
P=`su - ${USER} -c 'echo A-X-4-X@@$PATH@@X-4-X-A' | grep 'A-X-4-X@@.*@@X-4-X-A' | sed -e 's/^A-X-4-X@@//g' -e 's/@@X-4-X-A$//g'`
26-
else
27-
P="`(exec -l ${theShell} -c 'echo $PATH')`"
28-
fi
29-
;;
16+
true
17+
;;
3018
*)
31-
echo "Sorry, I don't know how to patch $BSH shells"
32-
exit 0
33-
;;
19+
exit 0
20+
;;
3421
esac
3522

36-
# Now ensure that our bin directory is on $P and before /usr/bin at that
37-
for elem in `echo $P | tr ':' ' '`
38-
do
39-
if [ "${elem}" = "${PYTHON_ROOT}/bin" ]; then
40-
echo "All right, you're a python lover already"
41-
exit 0
42-
elif [ "${elem}" = "/usr/bin" ]; then
43-
break
44-
fi
45-
done
46-
47-
echo "${PYTHON_ROOT}/bin is not on your PATH or at least not early enough"
4823
case "${BSH}" in
4924
*csh)
50-
if [ -f "${HOME}/.tcshrc" ]; then
51-
RC="${HOME}/.tcshrc"
52-
else
53-
RC="${HOME}/.cshrc"
54-
fi
55-
# Create backup copy before patching
56-
if [ -f "${RC}" ]; then
57-
cp -fp "${RC}" "${RC}.pysave"
58-
fi
59-
echo "" >> "${RC}"
60-
echo "# Setting PATH for Python ${PYVER}" >> "${RC}"
61-
echo "# The original version is saved in .cshrc.pysave" >> "${RC}"
62-
echo "set path=(${PYTHON_ROOT}/bin "'$path'")" >> "${RC}"
63-
if [ `id -ur` = 0 ]; then
64-
chown -h "${USER}" "${RC}"
65-
fi
66-
exit 0
67-
;;
25+
if [ -f "${HOME}/.tcshrc" ]; then
26+
RC="${HOME}/.tcshrc"
27+
else
28+
RC="${HOME}/.cshrc"
29+
fi
30+
31+
# Drop privileges while writing files.
32+
su -m ${USER} <<EOFC
33+
set -e
34+
# Create backup copy before patching
35+
if [ -f "${RC}" ]; then
36+
cp -fp "${RC}" "${RC}.pysave"
37+
fi
38+
echo "" >> "${RC}"
39+
echo "# Setting PATH for Python ${PYVER}" >> "${RC}"
40+
echo "# The original version is saved in .cshrc.pysave" >> "${RC}"
41+
echo "set path=(${PYTHON_ROOT}/bin "'$path'")" >> "${RC}"
42+
EOFC
43+
44+
if [ `id -ur` = 0 ]; then
45+
chown -h "${USER}" "${RC}"
46+
fi
47+
exit 0
48+
;;
6849
bash)
69-
if [ -e "${HOME}/.bash_profile" ]; then
70-
PR="${HOME}/.bash_profile"
71-
elif [ -e "${HOME}/.bash_login" ]; then
72-
PR="${HOME}/.bash_login"
73-
elif [ -e "${HOME}/.profile" ]; then
74-
PR="${HOME}/.profile"
75-
else
76-
PR="${HOME}/.bash_profile"
77-
fi
78-
;;
50+
if [ -e "${HOME}/.bash_profile" ]; then
51+
PR="${HOME}/.bash_profile"
52+
elif [ -e "${HOME}/.bash_login" ]; then
53+
PR="${HOME}/.bash_login"
54+
elif [ -e "${HOME}/.profile" ]; then
55+
PR="${HOME}/.profile"
56+
else
57+
PR="${HOME}/.bash_profile"
58+
fi
59+
;;
7960
fish)
80-
CONFIG_DIR="${HOME}/.config/fish/conf.d/"
81-
RC="${CONFIG_DIR}/python-${PYVER}.fish"
82-
mkdir -p "$CONFIG_DIR"
83-
if [ -f "${RC}" ]; then
84-
cp -fp "${RC}" "${RC}.pysave"
85-
fi
86-
echo "# Setting PATH for Python ${PYVER}" > "${RC}"
87-
if [ -f "${RC}.pysave" ]; then
88-
echo "# The original version is saved in ${RC}.pysave" >> "${RC}"
89-
fi
90-
echo "fish_add_path -g \"${PYTHON_ROOT}/bin\"" >> "${RC}"
91-
if [ `id -ur` = 0 ]; then
92-
chown -h "${USER}" "${RC}"
93-
fi
94-
exit 0
95-
;;
61+
CONFIG_DIR="${HOME}/.config/fish/conf.d/"
62+
RC="${CONFIG_DIR}/python-${PYVER}.fish"
63+
64+
# Drop privileges while writing files.
65+
su -m ${USER} <<EOFF
66+
set -e
67+
mkdir -p "$CONFIG_DIR"
68+
if [ -f "${RC}" ]; then
69+
cp -fp "${RC}" "${RC}.pysave"
70+
fi
71+
echo "# Setting PATH for Python ${PYVER}" > "${RC}"
72+
if [ -f "${RC}.pysave" ]; then
73+
echo "# The original version is saved in ${RC}.pysave" >> "${RC}"
74+
fi
75+
echo "fish_add_path -g \"${PYTHON_ROOT}/bin\"" >> "${RC}"
76+
EOFF
77+
78+
if [ `id -ur` = 0 ]; then
79+
chown -h "${USER}" "${RC}"
80+
fi
81+
exit 0
82+
;;
9683
zsh)
97-
PR="${HOME}/.zprofile"
98-
;;
84+
PR="${HOME}/.zprofile"
85+
;;
9986
*sh)
100-
PR="${HOME}/.profile"
101-
;;
87+
PR="${HOME}/.profile"
88+
;;
10289
esac
10390

91+
# Drop privileges while writing files.
92+
su -m ${USER} <<EOFS
93+
set -e
10494
# Create backup copy before patching
10595
if [ -f "${PR}" ]; then
106-
cp -fp "${PR}" "${PR}.pysave"
96+
cp -fp "${PR}" "${PR}.pysave"
10797
fi
10898
echo "" >> "${PR}"
10999
echo "# Setting PATH for Python ${PYVER}" >> "${PR}"
110100
echo "# The original version is saved in `basename ${PR}`.pysave" >> "${PR}"
111101
echo 'PATH="'"${PYTHON_ROOT}/bin"':${PATH}"' >> "${PR}"
112102
echo 'export PATH' >> "${PR}"
103+
EOFS
104+
113105
if [ `id -ur` = 0 ]; then
114-
chown -h "${USER}" "${PR}"
106+
chown -h "${USER}" "${PR}"
115107
fi
116108
exit 0

0 commit comments

Comments
 (0)