Skip to content

Instantly share code, notes, and snippets.

@fperez
Created May 17, 2012 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fperez/2716714 to your computer and use it in GitHub Desktop.
Save fperez/2716714 to your computer and use it in GitHub Desktop.
Bash configuration utilities for easy layered PYTHONPATH management
# -*- sh -*-
# Utilities for bash config files
# Contact: Fernando Perez <fernando.perez@colorado.edu>
#-----------------------------------------------------------------------------
# Useful constants
# For os, user or processor dependent config
UNAME=$(uname)
MACHINE=$(uname -m)
PROCESSOR=$(uname -p)
HOST=$(hostname | sed 's/\..*//')
WHOAMI=$(whoami)
# Python version information
PYVER=$(python -ESV 2>&1)
PYVER_MINOR=${PYVER#Python }
PYVER_MAJOR=${PYVER_MINOR:0:3}
# Names for common ANSI color escapes. Note: the initial and final escaped [/]
# are needed for line lengths to be correctly computed. Otherwise
# tab-completion produces incorrect wrapping problems.
# dark colors
BLACK="\[\033[0;30m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
BROWN="\[\033[0;33m\]"
BLUE="\[\033[0;34m\]"
PURPLE="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
L_GRAY="\[\033[0;37m\]"
# light colors
GRAY="\[\033[1;30m\]"
L_RED="\[\033[1;31m\]"
L_GREEN="\[\033[1;32m\]"
YELLOW="\[\033[1;33m\]"
L_BLUE="\[\033[1;34m\]"
L_PURPLE="\[\033[1;35m\]"
L_CYAN="\[\033[1;36m\]"
WHITE="\[\033[1;37m\]"
# to revert to the default font color
NO_COLOR="\[\033[0m\]"
#-----------------------------------------------------------------------------
# Functions begin
# The common paths that the first group of functions manage is:
# PATH: binary execution
# LD_LIBRARY_PATH: dynamic linker search path
# LIBRARY_PATH: static linking by gcc (like -L)
# CPATH: generic include path for gcc (like -I), used for all languages
# C_INCLUDE_PATH: C-specific include path, after CPATH
# CPLUS_INCLUDE_PATH: C++-specific include path, after CPATH
# PYTHONPATH: search path for python packages
function mk_pathspec {
# Basic path spec generator, used by the mk_genpath* functions
# Inputs:
local prefixes=$1 # list of prefixes for path construction
local ptypes=$2 # type of the path ('bin','lib', 'include', etc)
# Code begins
local ppath=''
local prefix
local pt
for prefix in $prefixes
do
for pt in $ptypes
do
ppath="$ppath:$prefix/$pt"
done
done
echo $ppath
}
function mk_genpath {
# Generic path spec generator (for unqualified, 32-bit only path specs)
# Inputs:
local prefixes=$1 # list of prefixes for path construction
local ptype=$2 # type of the path ('bin','lib', 'include', etc)
local postfix=${3:-" "} # possible postfix to be appended after $ptype
if [[ "$postfix" ]]; then
ptype="$ptype/$postfix"
fi
echo $(mk_pathspec "$prefixes" "$ptype")
}
function mk_genpath64 {
# Path spec generator, that produces 64-bit specific paths. On a 32-bit
# system this works like mk_genpath, but on x86_64 architectures each path
# component is produced both in $ptype and ${ptype}64 versions. This is
# useful for things like 'lib64:lib' combinations.
# Inputs:
local prefixes=$1 # list of prefixes for path construction
local ptype=$2 # type of the path ('bin','lib', 'include', etc)
local postfix=${3:-" "} # possible postfix to be appended after $ptype
local ptypeout
if [[ $MACHINE == "x86_64" ]]; then # 64-bit OS
if [[ "$postfix" ]]; then
ptypeout="${ptype}64/$postfix $ptype/$postfix"
fi
else # 32-bit OS
if [[ "$postfix" ]]; then
ptypeout="$ptype/$postfix"
fi
fi
echo $(mk_pathspec "$prefixes" "$ptypeout")
}
# Specific functions to create certain common types of paths
# PATH: binary execution
function mk_path {
echo $(mk_genpath "$1" bin)
}
# CPATH: generic include path for gcc (like -I), used for all languages
# C_INCLUDE_PATH: C-specific include path, after CPATH
# CPLUS_INCLUDE_PATH: C++-specific include path, after CPATH
function mk_cpath {
echo $(mk_genpath "$1" include)
}
# LIBRARY_PATH: static linking by gcc (like -L)
function mk_library_path {
echo $(mk_genpath64 "$1" lib)
}
# LD_LIBRARY_PATH: dynamic linker search path
function mk_ld_library_path {
echo $(mk_genpath64 "$1" lib)
}
# PYTHONPATH: search path for python packages
function mk_pythonpath {
local pypath=python${2-$PYVER_MAJOR}/site-packages
echo $(mk_genpath64 "$1" lib "$pypath")
}
# MANPATH: search path for man pages.
# See the manpath man page for details on what starting/end colons mean.
function mk_manpath {
echo $(mk_genpath "$1" share/man)
}
function export_paths {
# Export all common paths based on a list of prefixes
#
# Inputs:
# prefixes -- a list of prefixes to export all common paths for.
local prefixes=$1
# PATH: binary execution
export PATH=$(mk_path "$prefixes"):$PATH
# CPATH: generic include path for gcc (like -I), used for all languages
# C_INCLUDE_PATH: C-specific include path, after CPATH
# CPLUS_INCLUDE_PATH: C++-specific include path, after CPATH
export CPATH=$(mk_cpath "$prefixes"):$CPATH
# LIBRARY_PATH: static linking by gcc (like -L)
export LIBRARY_PATH=$(mk_library_path "$prefixes"):$LIBRARY_PATH
# LD_LIBRARY_PATH: dynamic linker search path
export LD_LIBRARY_PATH=$(mk_ld_library_path "$prefixes"):$LD_LIBRARY_PATH
# PYTHONPATH: search path for python packages
export PYTHONPATH=$(mk_pythonpath "$prefixes"):$PYTHONPATH
# MANPATH: search path for man pages.
local manpath=$(mk_manpath "$prefixes"):$MANPATH
# Note, this may be a bit buggy regarding colon positioning, not all edge
# cases covered. I assume here that mk_manpath returns colon-prefixed
# strings and strip the first character.
export MANPATH=${manpath:1}
}
function cdiff {
# Call colordiff, pipe to less
colordiff -u $@ | less
}
function add_ppa_key {
# Add a key from the Ubuntu PPA to the system's keyring
# Usage
# add_ppa_key key_signature
# See https://help.launchpad.net/Packaging/PPA for details
local keysig=$1
local tmpkey=$(mktemp -p /tmp ppa-key.XXXXXXXXXX)
gpg --no-default-keyring --keyring $tmpkey \
--keyserver keyserver.ubuntu.com --recv $keysig
gpg --no-default-keyring --keyring $tmpkey --export --armor $keysig | \
sudo apt-key add -
rm -f $tmpkey
}
function pybuild {
# Clean a python tree and build all extension code in place
echo "Cleaning up directory with grot"
grot
echo "Running a python clean/build pass"
python setup.py clean
python setup.py build_ext --inplace
}
function pytest {
# Run the test suite for a python package by name.
# This assumes the package has a top-level .test() routine to run its
# test suite.
local pname=$1
python -c "import $pname;${pname}.test()"
}
function mvl {
# Move a symlink - rename the target a symlink points to
# strip possible trailing '/'s from the source and target
local linkname=${1/%\//""}
local target=${2/%\//""}
if [[ ! -L $linkname ]]; then
echo "ERROR: Filename $linkname must be a symlink"
return
fi
rm -f $linkname
ln -s $target $linkname
}
function bzd {
# colored bzr diff with pager
bzr cdiff "$@" | less
}
function codeprint {
enscript -G2rjE --color $1
}
function codeps {
enscript -G2rjE --color -o $1.ps $1
gv $1.ps
}
# shows the git history as ASCII graph
# from: http://www.ralfebert.de/blog/tools/git_log_helper/
function glog() {
git log --oneline --topo-order --graph $@
}
# Show git authors for the specified commit range
function gauthor0() {
git log "$@" | grep '^Author' | cut -d' ' -f 2- | sort | uniq
}
function gauthor() {
git log "$@" | grep '^Author' | cut -d' ' -f 2- | sort | uniq
}
# http://www.ralfebert.de/blog/tools/www_here/
function www_here {
python -c 'import SimpleHTTPServer,SocketServer;PORT=1234;httpd = SocketServer.TCPServer(("", PORT),SimpleHTTPServer.SimpleHTTPRequestHandler); print "serving at port", PORT; httpd.serve_forever()'
}
function airplane-mode {
local mode=${1:-"on"}
if [[ "$mode" == "on" ]]; then
action="stop"
sudo rfkill block bluetooth
sudo rfkill block wlan
else
action="start"
sudo rfkill unblock bluetooth
sudo rfkill unblock wlan
fi
services="network-manager networking ntp avahi-daemon portmap cups ssh"
for service in $services
do
sudo service $service $action
done
echo
echo "Airplane mode now: $mode"
}
# Use this version at boot time to turn the card on
function wiboot {
sudo rfkill unblock wifi
sleep 1
nmcli nm wifi on
}
# Use this one on resuming from wakeup, since the resume acpi script will wake
function wi {
nmcli nm wifi on
}
# From the which manpage
#function which ()
#{
# (alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
#}
#export -f which
function mendeleysync_up ()
{
unison mendeley -ui text -batch -force /home/fperez/
}
function mendeleysync_down ()
{
unison mendeley -ui text -batch -force ssh://bic/
}
function ssh-on ()
{
if [[ "$1" ]]; then
id=_$1
fi
ssh-add $HOME/.ssh/id_rsa$id
}
function kbfix ()
{
rm -f $HOME/.config/xfce4/panel/xkb-plugin-12.rc
rm -f $HOME/.config/xfce4/panel/xkb-plugin-35.rc
cp $HOME/.config/xfce4/panel/xkb-plugin-12-good.rc $HOME/.config/xfce4/panel/xkb-plugin-12.rc
cp $HOME/.config/xfce4/panel/xkb-plugin-12-good.rc $HOME/.config/xfce4/panel/xkb-plugin-35.rc
pkill xkb
}
#*********************** End of file <.bash_utils> *************************
############################################################################
#
# Load basic bash utilities (handy functions and constants)
#
. $HOME/.bash_utils
#
# Initialize $PATH with sbin locations so I can find system tools
#
export PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH
#
# Configure paths, using the path generation functions in .bash_utils
#
# These are the prefixes I typically use as --prefix options for installation
# of packages. There's a method to the madness of having several of them, and
# in this order. The ones at the top end up first in the generated path specs,
# so they take precedence.
pfx="$pfx $HOME/tmp/junk" # quick and dirty testing
pfx="$pfx $HOME/tmp/local" # temporary, stable testing
pfx="$pfx $HOME/usr" # codes *I* have written
pfx="$pfx $HOME/usr/opt" # I don't sync this across computers
pfx="$pfx $HOME/usr/local" # default prefix for third-party installs
pfx="$pfx $HOME/.local" # used by python in --user installs
pfx="$pfx /local" # used in some machines I work on
pfx="$pfx /opt" # vendor directory for commercial stuff
# Now, set all common paths based on the prefix list just built. The
# export_paths function ensures that all commonly needed paths get correctly
# set and exported to the environment.
export_paths "$pfx"
# Default prefix for personal installs I use.
#export PREFIX=$HOME/usr/local
export PREFIX=$HOME/usr/opt
# This is the name CMAKE uses for the same thing
export CMAKE_INSTALL_PREFIX=$PREFIX
# Make version-specific ones, so it's easy to switch with a simple alias
PYTHONPATH26=`mk_pythonpath "$pfx" 2.6`
PYTHONPATH27=`mk_pythonpath "$pfx" 2.7`
PYTHONPATH32=`mk_pythonpath "$pfx" 3.2`
alias pypath26='export PYTHONPATH=$PYTHONPATH26'
alias pypath27='export PYTHONPATH=$PYTHONPATH27'
alias pypath32='export PYTHONPATH=$PYTHONPATH32'
alias py2="PYTHONPATH=$PYTHONPATH27 python2.7"
alias py3="PYTHONPATH=$PYTHONPATH32 python3.2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment