Skip to content

Instantly share code, notes, and snippets.

@mechanicker
Created July 8, 2013 07:10
Show Gist options
  • Save mechanicker/5946783 to your computer and use it in GitHub Desktop.
Save mechanicker/5946783 to your computer and use it in GitHub Desktop.
Bash shell helper functions to make working in development environment a little easier
#!/bin/env bash
# Important variables
USER=`who am i | cut -d" " -f1`
if [ -z "$USER" ] ; then
USER=`whoami`
fi
WSROOT=${WSROOT:-~/devel}
if [ -f /x/eng/site/site.def ] ; then
SITE=`grep BUILDSITE= /x/eng/site/site.def|cut -d= -f2|tr -d '"'|tr A-Z a-z`
else
SITE='nb'
fi
BBBASE=/x/eng/bbnb/daemon/DOT
# Some useful aliases
SELF=/u/dhruva,btc/.bash_funcs
if [ ! -f $SELF ] ; then
SELF=${BASH_SOURCE[0]}
fi
alias bashfuncs='source $SELF'
# Disable git by default
NOGIT=1
export NOGIT
#------------------------------------------------------------------------------
# cycl
# Log into site specific cycl machines
#------------------------------------------------------------------------------
function cycl
{
if [ "$1" = "rtp" ] ; then
host=cycl${1}.${1}.netapp.com
elif [ "$1" = "svl" ] ; then
host=cycl${1}.eng.netapp.com
else
host=cyclnb.eng.btc.netapp.in
fi
shift
ssh $host $*
}
#------------------------------------------------------------------------------
# cifs
# Launch smbclient to the specified vsim
#------------------------------------------------------------------------------
function cifs
{
vnum=`echo $1|tr -d '[a-zA-Z]'`
if [ -z "$2" ] ; then
share="NS"
else
share="$2"
fi
if [ "$USER" == "venkatc" ] ; then
d=1
else
d=2
fi
smbclient -U Administrator -W CR //${USER}-vsim${vnum}-d${d}.sim.eng.btc.netapp.in/${share} netapp*123
}
#------------------------------------------------------------------------------
# error_handler
#------------------------------------------------------------------------------
function error_handler
{
# Save the last error, we may clobber it
err=$?
# Get the command and args seperated out
read cmd args <<< $BASH_COMMAND
smart_cd $WSROOT $err $cmd $args
}
#------------------------------------------------------------------------------
# smart_cd
#------------------------------------------------------------------------------
function smart_cd
{
root=$1
shift
err=$1
shift
cmd=$1
shift
args=$*
if [ $err -eq 127 ] ; then
dir=(${root}/${cmd}*)
dir=`echo $dir|sed s/\\."[0-9]\+\$"//g`
dir=`\ls -d ${dir}.{0..9}* 2>/dev/null|awk -F"." '{print $NF,$0}'|\
sort -rn|cut -d' ' -f2|head -1`
if [ -d "$dir" ] ; then
pushd $dir 1>/dev/null
else
dir=(${root}/${cmd}*)
if [ -d "$dir" ] ; then
pushd $dir 1>/dev/null
else
if [ "$root" = "." ] ; then
return
fi
smart_cd "." $err $cmd $args
return
fi
fi
fi
}
#------------------------------------------------------------------------------
# em
# Launch emacs daemon if not running
# Launch emacs GUI
#------------------------------------------------------------------------------
function em
{
vopt='-n'
declare -i notfound=0
if [ -f ~/.emacs.d/server/server ]; then
ip=`egrep -e "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\:" ~/.emacs.d/server/server|cut -d':' -f1`
/sbin/ifconfig|grep -q $ip
notfound=$?
else
emacs --daemon
notfound=0
fi
if [ $# -eq 0 ]; then
vopt='-t'
elif [ -z $DISPLAY ]; then
vopt='-t'
elif [ $1 == "-t" ]; then
vopt='-t'
shift
elif [ $1 == "-w" ]; then
echo $host
return
fi
# Launch emacs if not running
if [ $notfound -eq 1 ]; then
echo "Running emacs in standalone mode"
emacs -nw $*
else
echo "Connecting to local emacs daemon"
emacsclient --alternate-editor=vim ${vopt} $*
fi
}
#------------------------------------------------------------------------------
# bsdgcc
#------------------------------------------------------------------------------
function bsdgcc
{
if [ "help" == "$1" ] ; then
echo "
bsdgcc compiler_options
"
return;
fi
compiler=0
GCCROOT=/usr/software/build/compilers/x86_64-unknown-freebsd7-gcc-4.2.1_p15
if [ $# -gt 0 ] ; then
if [ "gcc" = "$1" -o "g++" = "$1" -o "ld" = "$1" ] ; then
compiler=1
fi
tool=$1
shift
fi
if [ -z $tool ] ; then
tool=gcc
fi
tl=`\ls -1 $GCCROOT/bin/*${tool} 2>/dev/null | xargs basename 2>/dev/null`
if [ ! -f $GCCROOT/bin/$tl ] ; then
echo Error: No such file $GCCROOT/bin/\*$tool found
return
fi
tool=$tl
if [ $compiler -eq 1 ] ; then
$ACTION $GCCROOT/bin/$tool --sysroot=$GCCROOT/sysroot.hidden $*
else
$ACTION $GCCROOT/bin/$tool $*
fi
}
#------------------------------------------------------------------------------
# smoke
#------------------------------------------------------------------------------
function smoke
{
SCRIPT=${SCRIPT:-/x/eng/localtest/noarch/main/CR/scripts/cr_presubmit_fullsteam}
if [ -x $SCRIPT ] ; then
echo Using script: $SCRIPT
else
echo Error: Could not find $SCRIPT
return -1
fi
if [ "help" == "$1" ] ; then
echo "
smoke options
extra tests...
"
return;
fi
TESTS=
P4DIR=`pwd -P`
if [ $# -gt 0 ] ; then
TESTS="--add-tests=$*"
fi
$ACTION ${SCRIPT} --buildroot=$P4DIR --site=NB --background $TESTS
}
#------------------------------------------------------------------------------
# 4p
#------------------------------------------------------------------------------
function 4p
{
if [ "help" == "$1" ] ; then
echo "
4p command options...
head|tip => Gets the top change in the current client
hist => Lists the file propogation history across branches
shelves => Shows the shelves in the current client
changes => Verbose display of pending changes in current client
change => Terse change number display of pending changes in current client
patch => Attempt to generate diff in GNU patch format
status => Shows the build target of last failed build
remake => Rebuilds the last failed component
diff => p4 diff using vimdiff
merge => wsmerge that figues out the codeline based on source workspace
open|op => Terse display of edited files in the current client
"
return;
fi
if [ $# -eq 0 ] ; then
return
fi
op=$1
shift
if [ "merge" != $op -a "get" != $op ] ; then
if [ -z $P4CLIENT ] ; then
return
fi
fi
if [ "head" == $op -o "tip" == $op ] ; then
p4 changes -m1 \#have
elif [ "root" == $op ] ; then
p4 client -o|egrep -A1 "^AltRoots:"|tr -d '[ \t\n]'|cut -d':' -f2
elif [ "backup" == $op ] ; then
bdir=~/.backup/p4
if [ ! -d ${bdir} ] ; then
mkdir -p ${bdir}
fi
out=${bdir}/backup.`pwd|tr \/ \_`.`date +%s`.tar.gz
echo Backup file location: $out | tee -a $bdir/index.txt
if [ $# != 0 ] ; then
echo Comment: $* | tee -a ${bdir}/index.txt
fi
tar -zcvf $out `4p open` | tee -a ${bdir}/index.txt
elif [ "branch" == $op ] ; then
p4 -c $P4CLIENT client -o|egrep -A1 "^View:"|tr -d '\n'\
|egrep -v "^[ \t]*-"\
|awk -F"//depot/prod/DOT/" '{print $2}'|cut -d'/' -f1
elif [ "hist" == $op ] ; then
echo $1
br=`p4 filelog $1 | cut -s -d ' ' -f3,4,5 | egrep -e "^branch from" \
| cut -d ' ' -f3 | cut -d'#' -f1`
if [ -z $br ] ; then
return
else
4p hist $br
fi
elif [ "shelves" == $op ] ; then
4p changes shelved $*
elif [ "shelf" == $op ] ; then
4p shelves $* | cut -d' ' -f2
elif [ "changes" == $op -o "change" == $op ] ; then
if [ "$1" == "" ] ; then
args="pending"
else
args="$*"
fi
if [ "changes" == $op ] ; then
p4 changes -c $P4CLIENT -u $USER -s $args
else
p4 changes -c $P4CLIENT -u $USER -s $args | cut -d' ' -f2
fi
elif [ "patch" == $op ] ; then
filt="`4p root | cut -d' ' -f3 | xargs dirname`/"
p4 diff -du | sed s#${filt}##g
elif [ "status" == $op ] ; then
br status | grep -E "\<failed\>[ \t]+" | awk '{print $1"."$2}'
elif [ "remake" == $op ] ; then
targ=`4p status`
if [ "$targ" != "" ] ; then
br make $* $targ
fi
elif [ "diff" == $op ] ; then
P4DIFF=vimdiff review $*
elif [ "merge" == $op -a -f $1/P4ENV ] ; then
source $1/P4ENV
br=`4p branch`
if [ $# -ge 1 ] ; then
old=$1
shift
fi
if [ $# -ge 1 ] ; then
new=$1
shift
else
declare -i idx=0
base=`echo $old|sed s/\\."[0-9]\+\$"//g`
idx=`\ls -d ${base}.{0..9}* 2>/dev/null|awk -F"." '{print $NF,$0}'|\
sort -rn|cut -d' ' -f2|head -1|rev|cut -d'.' -f1|rev`
new="${base}.${idx}"
while [ -d $new ] ; do
idx+=1
new="${base}.${idx}"
done
fi
$ACTION wsmerge DOT:${br} $old $new $USER:$SITE:$new
elif [ "get" == $op ] ; then
if [ $# -ge 2 ] ; then
P4MASTER=$1
ws=$2
else
ws=$1
fi
declare -i idx=0
base=`echo $1|sed s/\\\."[0-9]\+\$"//g`
idx=`\ls -d ${base}.{0..9}* 2>/dev/null|awk -F"." '{print $NF,$0}'|\
sort -rn|cut -d' ' -f2|head -1|rev|cut -d'.' -f1|rev`
new="${base}.${idx}"
while [ -d $new ] ; do
idx+=1
new="${base}.${idx}"
done
ws=$new
if [ "" == "$P4MASTER" ] ; then
echo Error: Provide bammbamm depot
return
fi
args=
if [ -z "$NOGIT" -a -f $BBBASE/$P4MASTER/daemon_base/.git/config ] ; then
args="-git"
echo "Enabling git backed p4 bammbammm client"
fi
$ACTION wsget $args DOT:$P4MASTER $ws $USER:$SITE:$ws
elif [ "open" == $op -o "op" == $op ] ; then
$ACTION p4 opened 2>/dev/null|cut -d' ' -f1|cut -d'#' -f1\
|awk -F"/DOT/`4p branch`/" '{print $2}'
elif [ "cov" == $op -o "coverage" == $op ] ; then
/x/eng/csitools/cr/policy/cr-test-linux-ut-cov $HOME/web/linuxcov
elif [ "tags" == $op -o "gtags" == $op ] ; then
rt=`4p root`
files=`mktemp -t $USER-gtags.XXXXXX`
4p srcfiles|sort|uniq > $files
gtags --gtagsconf=/u/dhruva/.globalrc -f $files $rt
unlink $files
elif [ "srcfiles" == $op ] ; then
RX="(\.h|\.c|\.cc|\.cpp|\.C|\.y|\.l|\.php|\.java|\.smf|\.tcc|\.x|\.X)$"
EXTDIRS+=spinnp/bedrock/export/common/headers:freebsd_vfs/bedrock/internal/common/spin
# Get all the cscope input files from nightly
br=`4p branch`
egrep -E $RX /x/eng/rlse/DOT/${br}N/tools/bedrock/export/common/cscope-files/cscope.files | sed s#^\.\/##g
# Add extra user specified folders to extract tags
for dir in `echo $EXTDIRS | tr ':' ' '` ; do
egrep -E "^\./${dir}.*${RX}" .filelist | sed s#^\.\/##g
done
# Get from WIP
px lswip 2>/dev/null | rev | cut -d' ' -f1 | rev
# Get files added to P4 and not part of WIP
px opened 2>/dev/null | cut -d' ' -f1 | cut -d'#' -f1 | sed s#"`px where | sed s#"[\. ]\+"#"\\\\\|"#g`"##g
else
return -1
fi
}
function vs
{
vsid=$1
shift
if [ "$1" = "extend" ] ; then
for ii in `egrep -E "^(server|instance):" ~/vsims/${USER}_vsim_${vsid}/SimBindings` ; do
key=`echo $ii|cut -d: -f1`
val=`echo $ii|cut -d: -f2`
if [ "$key" = "instance" ] ; then
inst=$val
else
serv=$val
fi
done
serv=`echo $serv|sed s/\\\\./-$inst\./`
echo "Extending ${USER}_vsim${vsid} on $serv for 2 weeks"
elms extend $serv -l 2w
fi
}
#------------------------------------------------------------------------------
# genxref
#------------------------------------------------------------------------------
function genxref
{
if [ "help" == "$1" ] ; then
echo "
genxref [-l] [-f|-s]
l => Local tag store instead of sharing across clients
f => Cleanup tags and build afresh instead of incremental
s => Create symlinks from available tags for the branch
"
return;
fi
rebuild=1
br=`p4 where | cut -d ' ' -f1 | egrep -v "^[ \t]+-" | rev | cut -d '/' -f2 | rev`
croot="tools/bedrock/export/common"
nightly="/x/eng/rlse/DOT/${br}N"
PWD=`pwd`
# Determine the tag location
if [ "$1" == "-l" ] ; then
TAGROOT=.
shift
else
TAGROOT=~/.tags/$br
\mkdir -p $TAGROOT
fi
# Cleanup for a fresh start
if [ "$1" == "-f" ] ; then
\rm -f $TAGROOT/cscope.*
\rm -f $TAGROOT/tags
\rm -f $TAGROOT/GPATH
\rm -f $TAGROOT/GTAGS
\rm -f $TAGROOT/GRTAGS
elif [ "$1" == "-s" ] ; then
rebuild=0
fi
# If we do not have cscope files, pull it from nightly
if [ ! -f $TAGROOT/cscope.out ] ; then
rebuild=1
# Do not copy if force rebuild is requested
if [ "$1" != "-f" ] ; then
cp ${nightly}/${croot}/cscope/cscope.out* $TAGROOT/.
fi
fi
# Do we have the source list file to build tags
if [ $rebuild -gt 0 ] ; then
if [ ! -f ${croot}/cscope-files/cscope.files ] ; then
br make -f cscope-files@tools.common
while true ; do
if [ -f ${croot}/cscope-files/cscope.files ] ; then
break
fi
sync
sleep 1
done
fi
fi
if [ $rebuild -gt 0 ] ; then
# Update the source file list
/u/dhruva,btc/bin/srcfiles | sort | uniq > cscope.files
# Get build HOSTS to distribute tag generation
localhost=`hostname`
declare -i i=0
for h in `sqlite3 .brinfo.sqlite3 "SELECT DISTINCT job_host FROM jobs WHERE job_host LIKE \"bld%\""` ; do
if [ "$localhost" = "$h" ] ; then
cmd="ssh cyclnb"
else
cmd="ssh $h"
fi
HOSTS[$i]=$cmd
i+=1
HOSTS[$i]=$cmd
done
i=0
# Build incremental cscope
echo Dispatching cscope: ${HOSTS[$i]}
${HOSTS[$i]} "cd $PWD && cscope -b -U -k -q -i cscope.files -f $TAGROOT/cscope.out"&
if [ $USER == "dhruva" ] ; then
# Build incremental gtags
i+=1
echo Dispatching gtags: ${HOSTS[$i]}
${HOSTS[$i]} "cd $PWD && gtags -i --gtagsconf=/u/dhruva/.globalrc -f cscope.files $TAGROOT"
else
# Build incremental ctags
i+=1
echo Dispatching ctags: ${HOSTS[$i]}
${HOSTS[$i]} "cd $PWD && ctags -a --extra=+f -L cscope.files -f $TAGROOT/tags"
fi
# Join the background process
fg 2>/dev/null
fi
# Link all tags to local client
if [ $TAGROOT != "." ] ; then
\ln -f -s $TAGROOT/* .
fi
}
function g4
{
if [ $# -eq 0 ] ; then
return
fi
op=$1
shift
br=`git branch | grep \* | cut -d' ' -f2`
rembr=`git config branch.${br}.merge | sed s#refs/heads/##`
rem=`git config branch.${br}.remote`/${rembr}
if [ "op" == ${op} -o "ed" == ${op} ] ; then
git diff --diff-filter=ACMRT $* --name-only ${rem}..${br}
elif [ "diff" == ${op} -o "df" == ${op} ] ; then
if [ -z $1 ] ; then
git ${op} $* ${rem}..${br}
elif [ -f $1 ] ; then
git ${op} ${rem} -- $*
else
git ${op} $* ${rem}..$1
fi
elif [ "bup" == ${op} ] ; then
out=~/.backup/backup-${br}.`pwd|tr \/ \_`.`date +%s`.tar.gz
tar -zcvf ${out} `g4 ed`
elif [ "review" == ${op} ] ; then
git --no-pager diff ${rem} -- $*
elif [ "summary" == ${op} ] ; then
git log --oneline ${rem}..${br}
elif [ "info" == ${op} ] ; then
echo "Branch: $br"
echo "Remote Branch: $rem"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment