From 2bc3d0456e95499e211caf28aeae27cd04ef994e Mon Sep 17 00:00:00 2001 From: Craig Silverstein Date: Tue, 16 Sep 2014 15:43:40 -0700 Subject: [PATCH] Totally revamp the way we handle .bashrc. Summary: I have spent some time groveling through the bash manpages, and am clearer now on the difference between .bashrc and .bash_profile -- basically, one is used for login shells and one for non-login shells. We want our code to run in both contexts, so I have moved everything to .bash_profile, which I now source from .bashrc. (I actually only source .bash_profile.khan, to keep any existing .bash_profile stuff out of it, since maybe the user *does* want that stuff to be login-only.) I also separate out .bash_profile -- which is bash-specific -- from .profile -- which runs on any sh-compatible shell, including zsh. This should make it easier for zsh users to do their setup. Test Plan: I created a new user on my machine, and ran make in the khan-dotfiles directory. It created all the files the way they should be. I then ran bash git rb and saw that the git alias was picked up; that is, the git command didn't say 'rb not found'. I then ran bash --login git rb and saw that it was picked up as well (it wasn't, before this change). Reviewers: marcos, alpert Reviewed By: alpert Subscribers: ayman Differential Revision: http://phabricator.khanacademy.org/D13071 --- .bash_profile.khan | 39 +++++++++++++++++++++++++++++++++ .bashrc.khan | 46 +++++++++++---------------------------- .profile.khan | 51 ++++++++++++++++++++++++++++++++++++++++++++ bash_profile.default | 12 +++++++++++ mac-setup.sh | 51 ++++++-------------------------------------- profile.default | 7 ++++++ setup.sh | 2 +- 7 files changed, 129 insertions(+), 79 deletions(-) create mode 100644 .bash_profile.khan create mode 100755 .profile.khan create mode 100644 bash_profile.default create mode 100644 profile.default diff --git a/.bash_profile.khan b/.bash_profile.khan new file mode 100644 index 0000000..295ee1a --- /dev/null +++ b/.bash_profile.khan @@ -0,0 +1,39 @@ +################################################################################ +# Khan Academy specific .bash_profile +# +# The difference between .bash_profile and .profile is that the latter +# is called for all sh-compatible shells. So we put bashisms here +# and non-bashisms in .profile. +# +# According to the bash manpage, if both .bash_profile and .profile +# exist, bash only reads the first one. So we have to source .profile +# manually. + +if [ -s ~/.profile ]; then + source ~/.profile +fi + +# Activate Python2.7 virtualenv +source ~/.virtualenv/khan27/bin/activate + +# Figure out what directory we're *really* in (following symlinks). +# We need this because *-completion.bash are siblings to this script. +# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # follow symlinks + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # resolve relative symlink +done +DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +# Enable autocompletion for git +source "$DIR/git-completion.bash" + +# Make sure git (and other apps) prefer 'vim' to 'vi'. +: ${EDITOR:=vim} + +# Set a high limit for open file descriptors per shell. +# The default on OS X is 256; we increase it to 1024--the default value of +# `sysctl kern.maxfilesperproc`, which `ulimit -n` must not exceed. +ulimit -S -n 1024 diff --git a/.bashrc.khan b/.bashrc.khan index 6224dad..657777d 100644 --- a/.bashrc.khan +++ b/.bashrc.khan @@ -1,36 +1,14 @@ ################################################################################ # Khan Academy specific bashrc - -# Add Phabricator bins to PATH -export PATH="$HOME/khan/devtools/arcanist/khan-bin:$PATH" - -# Add frankenserver bins to PATH -export PATH="$HOME/khan/webapp/third_party/frankenserver:$PATH" - -# Add dotfiles scripts to PATH -export PATH="$HOME/khan/devtools/khan-dotfiles/bin:$PATH" - -# Activate Python2.7 virtualenv -source ~/.virtualenv/khan27/bin/activate - -# Figure out what directory we're *really* in (following symlinks). -# We need this because *-completion.bash are siblings to this script. -# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in -SOURCE="${BASH_SOURCE[0]}" -while [ -h "$SOURCE" ]; do # follow symlinks - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - SOURCE="$(readlink "$SOURCE")" - [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # resolve relative symlink -done -DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - -# Enable autocompletion for git -source "$DIR/git-completion.bash" - -# Make sure git (and other apps) prefer 'vim' to 'vi'. -: ${EDITOR:=vim} - -# Set a high limit for open file descriptors per shell. -# The default on OS X is 256; we increase it to 1024--the default value of -# `sysctl kern.maxfilesperproc`, which `ulimit -n` must not exceed. -ulimit -S -n 1024 +# +# We actually store all our rc-code in .profile and .bash_profile. +# The difference between .*profile and .bashrc is that the former +# are loaded for login shells and the latter for non-login shells. +# (For more info, see http://hacktux.com/bash/bashrc/bash_profile.) +# +# We want our code to run for both types of shells, so we just include +# .bash_profile from here. We source only .bash_profile.khan in case +# you have code in your .bashrc_profile that you *don't* want executed +# for non-login shells. + +source ~/.bash_profile.khan diff --git a/.profile.khan b/.profile.khan new file mode 100755 index 0000000..7c81572 --- /dev/null +++ b/.profile.khan @@ -0,0 +1,51 @@ +################################################################################ +# Khan Academy specific .profile +# +# The difference between .bash_profile and .profile is that the latter +# is called for all sh-compatible shells. So we put non-bashisms here +# and bashisms in .bash_profile. + +# Add Phabricator bins to PATH +export PATH="$HOME/khan/devtools/arcanist/khan-bin:$PATH" + +# Add frankenserver bins to PATH +export PATH="$HOME/khan/webapp/third_party/frankenserver:$PATH" + +# Add dotfiles scripts to PATH +export PATH="$HOME/khan/devtools/khan-dotfiles/bin:$PATH" + +# Make sure git (and other apps) prefer 'vim' to 'vi'. +: ${EDITOR:=vim} + +# Set a high limit for open file descriptors per shell. +# The default on OS X is 256; we increase it to 1024--the default value of +# `sysctl kern.maxfilesperproc`, which `ulimit -n` must not exceed. +ulimit -S -n 1024 + + +# Mac-specific stuff. +if [ `uname -s` = Darwin ]; then + # We need /usr/local/bin to come before /usr/bin on the path, + # to pick up brew files we install. + if ! echo "$PATH" | egrep -q '(:|^)/usr/local/bin/?:(.*:)?/usr/bin/?(:|$)' + then + # This replaces /usr/bin with /usr/local/bin:/usr/bin + PATH=`echo $PATH | sed -E 's,(^|:)(/usr/bin/?(:|$)),\1/usr/local/bin:\2,'` + fi + + # Ideally we'd put /usr/local/sbin right before /usr/sbin, but + # there's so little in it we figure it's ok to put it first. + PATH=/usr/local/sbin:$PATH + export PATH + + # Numpy/etc use flags clang doesn't know about. This is only + # needed for mavericks. + if expr "`sw_vers -productVersion`" : 10.9 >/dev/null; then + CPPFLAGS="-Qunused-arguments $CPPFLAGS" + CFLAGS="-Qunused-arguments $CFLAGS" + # This ARCHFLAGS is needed until we have pyobjc 3.0, according to + # https://bitbucket.org/ronaldoussoren/pyobjc/issue/66/cannot-locate-a-working-compiler-error + ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future $ARCHFLAGS" + export CPPFLAGS CFLAGS ARCHFLAGS + fi +fi diff --git a/bash_profile.default b/bash_profile.default new file mode 100644 index 0000000..4293fc4 --- /dev/null +++ b/bash_profile.default @@ -0,0 +1,12 @@ +################################################################################ +# Local .bash_profile. +# Feel free to add whatever you like after the 'bash_profile.khan' line. +# +# Note that .bash_profile.khan automatically sources ~/.profile if it +# exists, so you don't have to worry that the addition of this file +# "hides" .profile (bash won't load a .profile file if it sees a +# .bash_profile file instead). + +if [ -s ~/.bash_profile.khan ]; then + source ~/.bash_profile.khan +fi diff --git a/mac-setup.sh b/mac-setup.sh index f2f6988..385c01a 100755 --- a/mac-setup.sh +++ b/mac-setup.sh @@ -38,51 +38,14 @@ error () { printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n" } -update_path() { - # We need /usr/local/bin to come before /usr/bin on the path, - # to pick up brew files we install. - if ! echo "$PATH" | egrep -q '(:|^)/usr/local/bin/?:(.*:)?/usr/bin/?(:|$)' - then - # This replaces /usr/bin with /usr/local/bin:/usr/bin - PATH=`echo $PATH | sed -E 's,(^|:)(/usr/bin/?(:|$)),\1/usr/local/bin:\2,'` - # Make this path update work in the future too. - path_update=`cat<<'EOF' -echo $PATH | sed -E 's,(^|:)(/usr/bin/?(:|$)),\1/usr/local/bin:\2,' -EOF` - else - path_update='' - fi - # Ideally we'd put /usr/local/sbin right before /usr/sbin, but - # there's so little in it we figure it's ok to put it first. - export PATH=/usr/local/sbin:$PATH - - # Put these in shell config file too. - # Test whether it's already in a config file (sorry zsh users who - # aren't using .profile). We follow the same order bash does: - if [ -f ~/.bash_profile ]; then - PROFILE_FILE="$HOME/.bash_profile" - elif [ -f ~/.bash_login ]; then - PROFILE_FILE="$HOME/.bash_login" - else - PROFILE_FILE="$HOME/.profile" - fi - if ! grep -q "export PATH=.*/usr/local/sbin" \ - ~/.bash_profile ~/.bash_login ~/.profile; then - echo 'export PATH=/usr/local/sbin:$PATH' >> "$PROFILE_FILE" - fi - # Numpy/etc use flags clang doesn't know about. This is only - # needed for mavericks. - if expr "`sw_vers -productVersion`" : 10.9 >/dev/null && \ - ! grep -q -e "-Qunused-arguments" \ - ~/.bash_profile ~/.bash_login ~/.profile; then - echo 'export CPPFLAGS="-Qunused-arguments $CPPFLAGS"' >> "$PROFILE_FILE" - echo 'export CFLAGS="-Qunused-arguments $CFLAGS"' >> "$PROFILE_FILE" - fi - if [ -n "$path_update" ]; then - echo "# Put /usr/local/bin right before /usr/bin" >> "$PROFILE_FILE" - echo 'PATH=`'"$path_update"'`' >> "$PROFILE_FILE" - fi +update_path() { + # We need /usr/local/bin to come before /usr/bin on the path, to + # pick up brew files we install. To do this, we just source + # .profile.khan, which does this for us (and the new user). + # (This assumes you're running mac-setup.sh from the khan-dotfiles + # directory.) + . .profile.khan } maybe_generate_ssh_keys () { diff --git a/profile.default b/profile.default new file mode 100644 index 0000000..b551569 --- /dev/null +++ b/profile.default @@ -0,0 +1,7 @@ +################################################################################ +# Local .profile. +# Feel free to add whatever you like after the profile.khan' line. + +if [ -s ~/.profile.khan ]; then + . ~/.profile.khan +fi diff --git a/setup.sh b/setup.sh index bfdc4c7..1050e3e 100755 --- a/setup.sh +++ b/setup.sh @@ -68,7 +68,7 @@ install_dotfiles() { # They all have the property they 'include' khan-specific code. for file in *.default; do dest="$ROOT/.`echo "$file" | sed s/.default$//`" # foo.default -> .foo - ka_version=.`echo "$file" | sed s/default/khan/` # .bashrc.khan + ka_version=.`echo "$file" | sed s/default/khan/` # .bashrc.khan, etc. if [ ! -e "$dest" ]; then cp -f "$file" "$dest" elif ! fgrep -q "$ka_version" "$dest"; then