Permalink
Fetching contributors…
Cannot retrieve contributors at this time
298 lines (264 sloc) 7.97 KB
### bashrc.dotfiles.sh
dotfiles_add_path() {
# dotfiles_add_path() -- add ${__DOTFILES}/scripts to $PATH
if [ -d "${__DOTFILES}" ]; then
#PATH_prepend "${__DOTFILES}/bin" # [01-bashrc.lib.sh]
PATH_prepend "${__DOTFILES}/scripts"
fi
}
shell_escape_single() {
# shell_escape_single()
strtoescape=${1}
echo "'"$(echo ${strtoescape} | sed "s,','\"'\"',g")"'"
}
dotfiles_status() {
# dotfiles_status() -- print dotfiles_status
echo "# dotfiles_status()"
echo HOSTNAME=$(shell_escape_single "${HOSTNAME}")
echo USER=$(shell_escape_single "${USER}")
echo __WRK=$(shell_escape_single "${__WRK}")
echo PROJECT_HOME=$(shell_escape_single "${PROJECT_HOME}")
echo WORKON_HOME=$(shell_escape_single "${WORKON_HOME}")
echo VIRTUAL_ENV_NAME=$(shell_escape_single "${VIRTUAL_ENV_NAME}")
echo VIRTUAL_ENV=$(shell_escape_single "${VIRTUAL_ENV}")
echo _SRC=$(shell_escape_single "${_SRC}")
echo _APP=$(shell_escape_single "${_APP}")
echo _WRD=$(shell_escape_single "${_WRD}")
#echo "__DOCSWWW=$(shell_escape_single "${_DOCS}")
#echo "__SRC=$(shell_escape_single "${__SRC}")
#echo "__PROJECTSRC=$(shell_escape_single "${__PROJECTSRC}")
echo _USRLOG=$(shell_escape_single "${_USRLOG}")
echo _TERM_ID=$(shell_escape_single "${_TERM_ID}")
echo PATH=$(shell_escape_single "${PATH}")
echo __DOTFILES=$(shell_escape_single "${__DOTFILES}")
#echo $PATH | tr ':' '\n' | sed 's/\(.*\)/# \1/g'
echo "#"
}
ds() {
# ds() -- print dotfiles_status
dotfiles_status $@
}
clr() {
# clr() -- clear scrollback
if [ -d '/Library' ]; then # see __IS_MAC
# osascript -e 'if application "Terminal" is frontmost then tell application "System Events" to keystroke "k" using command down'
clear && printf '\e[3J'
else
reset
fi
}
cls() {
# cls() -- clear scrollback and print dotfiles_status()
clr ; dotfiles_status
}
#dotfiles_term_uri() {
##dotfiles_term_uri() -- print a URI for the current _TERM_ID
#term_path="${HOSTNAME}/usrlog/${USER}"
#term_key=${_APP}/${_TERM_ID}
#TERM_URI="${term_path}/${term_key}"
#echo "TERM_URI='${TERM_URL}'"
#}
debug-env() {
_log=${_LOG:-"."}
OUTPUT=${1:-"${_log}/$(date +"%FT%T%z").debug-env.env.log"}
dotfiles_status
echo "## export"
export | tee $OUTPUT
echo "## alias"
alias | tee $OUTPUT
# echo "## lspath"
# lspath | tee $OUTPUT
}
# https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html#The-Shopt-Builtin
debug-on() {
# debug-on() -- set -x -v
set -x -v
shopt -s extdebug
}
debug-off() {
# debug-off() -- set +x +v
set +x +v
shopt -s extdebug
}
_virtualenvwrapper_get_step_num() {
# Virtualenvwrapper numeric sequence
# * to make logs in /var/log/venv.nnn-stepname.log naturally ordered
#
# * 0xx : 'initialization' actions : [initialize]
# * 1xx : 'creation' actions : [pre|post]mk[virtualenv|project]
# * 2xx : 'vation' actions : [pre|post][activate|deactivate]
# * 8xx : 'managment' actions : [pre|post][cpvirtualenv|rmvirtualenv]
# * 868 : unknown
# * xx0 : 'pre' actions
# * xx9 : 'post' actions
# Source-ordered according to the virtualenvwrapper docs
# * https://virtualenvwrapper.readthedocs.org/en/latest/scripts.html#scripts
step=${1}
n="868" # unknown
case ${step} in
"get_env_details")
n="800" ;;
"initialize")
n="010" ;;
"premkvirtualenv")
n="120" ;;
"postmkvirtualenv")
n="129" ;;
"precpvirtualenv")
n="820" ;;
"postcpvirtualenv")
n="829" ;;
"preactivate")
n="230" ;;
"postactivate")
n="239" ;;
"predeactivate")
n="290" ;;
"postdeactivate")
n="299" ;;
"prermvirtualenv")
n="810" ;;
"postrmvirtualenv")
n="819" ;;
"premkproject")
n="140" ;;
"postmkproject")
n="149" ;;
esac
echo "${n}"
}
log_dotfiles_state() {
# log_dotfiles_state() -- save current environment to logfiles
# $1 -- logkey (virtualenvwrapper step name)
test -n "${DOTFILES_SKIP_LOG}" && echo '#DOTFILES_SKIP_LOG' && return
_log=${_LOG:-"${HOME}/var/log"}
if [ "${_log}" == "/var/log" ]; then
_log="${HOME}/var/log"
fi
logkey=${1:-'log_dotfiles_state'}
stepnum="$(_virtualenvwrapper_get_step_num "${logkey}")"
logdir=${_log:-"var/log"}/venv..${VIRTUAL_ENV_NAME}..${stepnum}..${logkey}
exportslogfile=${logdir}/exports.log
envlogfile=${logdir}/exports_env.log
test -n ${logdir} && test -d ${logdir} || mkdir -p ${logdir}
# XXX:
export > ${exportslogfile}
set > ${envlogfile}
}
dotfiles_initialize() {
# dotfiles_initialize() -- virtualenvwrapper initialize
log_dotfiles_state 'initialize'
}
dotfiles_premkvirtualenv() {
# dotfiles_premkvirtualenv -- virtualenvwrapper premkvirtualenv
if [ -n "${1}" ]; then
export VIRTUAL_ENV="${WORKON_HOME}/${1}"
fi
}
dotfiles_postmkvirtualenv() {
# dotfiles_postmkvirtualenv -- virtualenvwrapper postmkvirtualenv
log_dotfiles_state 'postmkvirtualenv'
if [ -n "${VIRTUAL_ENV}" ]; then
echo "VIRTUAL_ENV is not set? (err: -1)"
return -1
fi
declare -f 'venv_mkdirs' 2>&1 >/dev/null && venv_mkdirs
test -d ${VIRTUAL_ENV}/var/log || mkdir -p ${VIRTUAL_ENV}/var/log
echo ""
echo $(which pip)
pip_freeze="${VIRTUAL_ENV}/var/log/pip.freeze.postmkvirtualenv.txt"
echo "pip_freeze='${pip_freeze}'"
pip freeze | tee ${pip_freeze}
echo ""
pip_list="${VIRTUAL_ENV}/var/log/pip.freeze.postmkvirtualenv.txt"
echo "pip_list='${pip_list}'"
pip list | tee ${pip_list}
}
dotfiles_preactivate() {
# dotfiles_preactivate() -- virtualenvwrapper preactivate
log_dotfiles_state 'preactivate'
}
dotfiles_postactivate() {
# dotfiles_postactivate() -- virtualenvwrapper postactivate
log_dotfiles_state 'postactivate'
local bash_debug_output=$(
$__VENV -e --verbose --diff --print-bash 2>&1 /dev/null)
local venv_return_code=$?
if [ ${venv_return_code} -eq 0 ]; then
if [ -n "${__VENV}" ]; then
source <($__VENV -e --print-bash)
fi
else
echo "${bash_debug_output}" # >2
fi
declare -f '_setup_usrlog' 2>&1 > /dev/null \
&& _setup_usrlog
declare -f '_setup_venv_prompt' 2>&1 > /dev/null \
&& _setup_venv_prompt
}
dotfiles_predeactivate() {
# dotfiles_predeactivate() -- virtualenvwrapper predeactivate
log_dotfiles_state 'predeactivate'
}
dotfiles_postdeactivate() {
# dotfiles_postdeactivate() -- virtualenvwrapper postdeactivate
log_dotfiles_state 'postdeactivate'
unset VIRTUAL_ENV_NAME
unset VENVSTR
unset VENVSTRAPP
unset VENVPREFIX
unset _APP
unset _BIN
unset _CFG
unset _EDITCFG_
export EDITOR_=${EDITOR}
unset _EDIT_
unset _ETC
unset _ETCOPT
unset _HOME
unset _IPQTLOG
unset _IPSESSKEY
unset _LIB
unset _LOG
unset _MEDIA
unset _MNT
unset _NOTEBOOKS
unset _OPT
unset _PYLIB
unset _PYSITE
unset _ROOT
unset _SBIN
unset _SERVE_
unset _SHELL_
unset _SRC
unset _SRV
unset _SVCFG
unset _SVCFG_
unset _TEST_
unset _TMP
unset _USR
unset _USRBIN
unset _USRINCLUDE
unset _USRLIB
unset _USRLOCAL
unset _USRLOG
unset _USRSBIN
unset _USRSHARE
unset _USRSRC
unset _VAR
unset _VARCACHE
unset _VARLIB
unset _VARLOCK
unset _VARMAIL
unset _VAROPT
unset _VARRUN
unset _VARSPOOL
unset _VARTMP
unset _VENV
unset _WRD
unset _WRD_SETUPY
unset _WWW
declare -f '_usrlog_set__USRLOG' 2>&1 > /dev/null \
&& _usrlog_set__USRLOG "${__USRLOG}"
dotfiles_reload
}