From b09d5232188ef99d735a393947af55549b17e24d Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 31 Dec 2021 10:27:27 -0800 Subject: [PATCH] [zsh] Update shell init; use ZSH autoloaded functions instead of a bunch of files --- bin/shell-log | 26 +++++++--- env | 87 --------------------------------- env.darwin | 26 ---------- env.linux | 5 -- env.local | 5 -- zsh/func/append_to_path | 13 +++++ zsh/func/init_env | 21 ++++++++ zsh/func/init_env_darwin | 38 ++++++++++++++ zsh/func/init_env_darwin_python | 29 +++++++++++ zsh/func/init_env_python | 34 +++++++++++++ zsh/func/init_path | 25 ++++++++++ zsh/func/prepend_to_path | 13 +++++ zshenv | 24 ++++++--- 13 files changed, 208 insertions(+), 138 deletions(-) delete mode 100644 env delete mode 100644 env.darwin delete mode 100644 env.linux delete mode 100644 env.local create mode 100644 zsh/func/append_to_path create mode 100644 zsh/func/init_env create mode 100644 zsh/func/init_env_darwin create mode 100644 zsh/func/init_env_darwin_python create mode 100644 zsh/func/init_env_python create mode 100644 zsh/func/init_path create mode 100644 zsh/func/prepend_to_path diff --git a/bin/shell-log b/bin/shell-log index 6affe87..6c08abb 100755 --- a/bin/shell-log +++ b/bin/shell-log @@ -12,11 +12,19 @@ import os.path _LOGFILE = os.path.expanduser('~/.shell.log') _LOGGER = None +LOG_LEVELS = { + 'crit': logging.CRITICAL, + 'error': logging.ERROR, + 'warn': logging.WARNING, + 'info': logging.INFO, + 'debug': logging.DEBUG +} + def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('-b', '--bold', action='store_true') - parser.add_argument('-f', '--file', default=LOGFILE) - parser.add_argument('-l', '--level', type=int, default=logging.INFO) + parser.add_argument('-f', '--file', default=_LOGFILE) + parser.add_argument('-l', '--level', default='info', choices=list(LOG_LEVELS.keys())) parser.add_argument('-n', '--noeol', action='store_true') parser.add_argument('msg') parser.add_argument('args', nargs=argparse.REMAINDER) @@ -31,19 +39,23 @@ def logger(args): stdout_handler.setFormatter(formatter) logger.addHandler(stdout_handler) + # TODO: Add file handler + return logger def main(): - # Set up. args = parse_args() log = logger(args) - # Set the level from the shell. - level = int(os.environ.get('NOISY', logging.INFO)) + level = os.environ.get('SHELL_LOG_LEVEL', logging.INFO) log.setLevel(level) - # Log it. - log.log(args.level, args.msg, *args.args) + try: + message_log_level = LOG_LEVELS[args.level] + except KeyError: + message_log_level = logging.INFO + + log.log(message_log_level, args.msg, *args.args) if __name__ == '__main__': main() diff --git a/env b/env deleted file mode 100644 index 07c804e..0000000 --- a/env +++ /dev/null @@ -1,87 +0,0 @@ -# .env -# vim: ft=zsh: -# -# Environment settings for bash and derivatives. The env scripts are sourced by -# Zsh for every shell, even the non-interactive ones, so this needs to be small -# and quick. Any configuration that will only be used for interactive sessions -# should be in the rc scripts. -# -# Eryn Wells - -[[ -e "$HOME/.shell-functions" ]] && source "$HOME/.shell-functions" - -export SYS=`uname -s | tr A-Z a-z` - -# Set this to a non-zero integer to see startup messages -export NOISY=0 - -print_heading -l 1 'Initializing environment' - -export PAGER="less" -export MANPAGER=$PAGER -export EDITOR="vim" -export VISUAL=$EDITOR -export LESSHISTFILE="-" -export GREP_OPTIONS="--color=auto" -export GREP_COLOR="1;32" - -function prepend-to-path -{ - if [[ -d "$1" ]]; then - path=("$1" $path) - export path - fi -} - -function append-to-path -{ - if [[ -d "$1" ]]; then - path+="$1" - export path - fi -} - -function setup-path -{ - export path=() - append-to-path "/usr/local/bin" - append-to-path "/usr/bin" - append-to-path "/bin" - append-to-path "/usr/local/sbin" - append-to-path "/usr/sbin" - append-to-path "/sbin" - prepend-to-path "/usr/X11/bin" - prepend-to-path "/opt/local/bin" - prepend-to-path "$HOME/.local/bin" - prepend-to-path "$HOME/.gem/ruby/2.2.0/bin" - prepend-to-path "$HOME/.cargo/bin" - prepend-to-path "$HOME/bin" -} - -setup-path - -if which virtualenvwrapper.sh 1>/dev/null 2>&1; then - export WORKON_HOME="$HOME/src/py/.envs" - source `which virtualenvwrapper.sh` -fi - -# Make sure gpg2 knows what to do with the curses-based smartcard PIN prompt. -export GPG_TTY=`tty` - -# System-specific settings -if [[ -e "$HOME/.env.$SYS" ]]; then - print_info -l 2 "Sourcing system-specific environment settings for $SYS" - source "$HOME/.env.$SYS" -fi - -host_env="$HOME/.env.`hostname -s`" -if [[ -e "$host_env" ]]; then - print_info -l 2 "Sourcing host-specific environment settings: $host_env" - source "$host_env" -fi - -# Local environment settings -if [[ -e "$HOME/.env.local" ]]; then - print_info -l 2 "Sourcing local environment setup" - source "$HOME/.env.local" -fi diff --git a/env.darwin b/env.darwin deleted file mode 100644 index 043c640..0000000 --- a/env.darwin +++ /dev/null @@ -1,26 +0,0 @@ -# vim: set ft=zsh: -# Eryn Wells - -export OSBUILD=`sysctl -n kern.osversion` -export OSVERSION=`sysctl -n kern.osproductversion` -export HWMODEL=`sysctl -n hw.model` - -pythonroot="$HOME/Library/Python" -if [[ -d "$pythonroot" ]]; then - for f in `ls "$pythonroot"`; do - prepend-to-path "$pythonroot/$f/bin" - done -fi -prepend-to-path "/usr/local/go/bin" - -py27local="$pythonroot/2.7/lib/python/site-packages" -if [[ -d "$py27local" ]]; then - if [[ ! -z $PYTHONPATH ]]; then - PYTHONPATH=$py27local:$PYTHONPATH - else - PYTHONPATH=$py27local - fi -fi -unset py27local -unset pythonroot -export PYTHONPATH diff --git a/env.linux b/env.linux deleted file mode 100644 index 99e2238..0000000 --- a/env.linux +++ /dev/null @@ -1,5 +0,0 @@ -# vim: set ft=zsh: -# Linux specific environment settings -# Eryn Wells - -export MAIL="/var/mail/$USER" diff --git a/env.local b/env.local deleted file mode 100644 index fe87613..0000000 --- a/env.local +++ /dev/null @@ -1,5 +0,0 @@ -# .env.local -# vim: set ft=zsh: -# Local environment customizations -# Eryn Wells - diff --git a/zsh/func/append_to_path b/zsh/func/append_to_path new file mode 100644 index 0000000..3c21108 --- /dev/null +++ b/zsh/func/append_to_path @@ -0,0 +1,13 @@ +#!/usr/bin/env zsh +# vim:ft=zsh: +# Eryn Wells + +function append_to_path +{ + if [[ -d "$1" ]]; then + path+="$1" + export path + fi +} + +append_to_path "$@" diff --git a/zsh/func/init_env b/zsh/func/init_env new file mode 100644 index 0000000..c3d576f --- /dev/null +++ b/zsh/func/init_env @@ -0,0 +1,21 @@ +#!/usr/bin/env zsh +# vim:ft=zsh: +# Eryn Wells + +function init_env +{ + shell-log 'Initializing basic environment' + + export PAGER="less" + export MANPAGER=$PAGER + export EDITOR="vim" + export VISUAL=$EDITOR + export LESSHISTFILE="-" + export GREP_OPTIONS="--color=auto" + export GREP_COLOR="1;32" + + # Make sure gpg2 knows what to do with the curses-based smartcard PIN prompt. + export GPG_TTY=`tty` +} + +init_env "$@" diff --git a/zsh/func/init_env_darwin b/zsh/func/init_env_darwin new file mode 100644 index 0000000..e7448b1 --- /dev/null +++ b/zsh/func/init_env_darwin @@ -0,0 +1,38 @@ +#!/usr/bin/env zsh +# vim:ft=zsh: +# Eryn Wells + +autoload append_to_path +autoload prepend_to_path + +function init_env_darwin +{ + shell-log "Initializing Darwin environment" + + export OSBUILD=`sysctl -n kern.osversion` + export OSVERSION=`sysctl -n kern.osproductversion` + export HWMODEL=`sysctl -n hw.model` + + local pythonRoot + local python27SitePackages + + pythonRoot="$HOME/Library/Python" + if [[ -d "$pythonRoot" ]]; then + for f in `ls "$pythonRoot"`; do + prepend_to_path "$pythonRoot/$f/bin" + done + fi + + python27SitePackages="$pythonroot/2.7/lib/python/site-packages" + if [[ -d "$python27SitePackages" ]]; then + if [[ ! -z $PYTHONPATH ]]; then + PYTHONPATH=$python27SitePackages:$PYTHONPATH + else + PYTHONPATH=$python27SitePackages + fi + fi + + export PYTHONPATH +} + +init_env_darwin "$@" diff --git a/zsh/func/init_env_darwin_python b/zsh/func/init_env_darwin_python new file mode 100644 index 0000000..615ce19 --- /dev/null +++ b/zsh/func/init_env_darwin_python @@ -0,0 +1,29 @@ +#!/usr/bin/env zsh +# vim:ft=zsh: +# Eryn Wells + +function init_env_darwin_python +{ + local pythonRoot + local python27SitePackages + + pythonRoot="$HOME/Library/Python" + if [[ -d "$pythonRoot" ]]; then + for f in `ls "$pythonRoot"`; do + prepend_to_path "$pythonRoot/$f/bin" + done + fi + + python27SitePackages="$pythonroot/2.7/lib/python/site-packages" + if [[ -d "$python27SitePackages" ]]; then + if [[ ! -z $PYTHONPATH ]]; then + PYTHONPATH=$python27SitePackages:$PYTHONPATH + else + PYTHONPATH=$python27SitePackages + fi + fi + + export PYTHONPATH +} + +init_env_darwin_python "$@" diff --git a/zsh/func/init_env_python b/zsh/func/init_env_python new file mode 100644 index 0000000..f5d2625 --- /dev/null +++ b/zsh/func/init_env_python @@ -0,0 +1,34 @@ +#!/usr/bin/env zsh +# vim:ft=zsh: +# Eryn Wells + +function init_env_python +{ + local pythonRoot + local python27SitePackages + + pythonRoot="$HOME/Library/Python" + if [[ -d "$pythonRoot" ]]; then + for f in `ls "$pythonRoot"`; do + prepend_to_path "$pythonRoot/$f/bin" + done + fi + + python27SitePackages="$pythonroot/2.7/lib/python/site-packages" + if [[ -d "$python27SitePackages" ]]; then + if [[ ! -z $PYTHONPATH ]]; then + PYTHONPATH=$python27SitePackages:$PYTHONPATH + else + PYTHONPATH=$python27SitePackages + fi + fi + + export PYTHONPATH + + if which virtualenvwrapper.sh 1>/dev/null 2>&1; then + export WORKON_HOME="$HOME/src/py/.envs" + source `which virtualenvwrapper.sh` + fi +} + +init_env_python "$@" diff --git a/zsh/func/init_path b/zsh/func/init_path new file mode 100644 index 0000000..1b22195 --- /dev/null +++ b/zsh/func/init_path @@ -0,0 +1,25 @@ +#!/usr/bin/env zsh +# Initialize the path to a standard default +# Eryn Wells + +autoload prepend_to_path +autoload append_to_path + +function init_path +{ + export path=() + append_to_path "/usr/local/bin" + append_to_path "/usr/bin" + append_to_path "/bin" + append_to_path "/usr/local/sbin" + append_to_path "/usr/sbin" + append_to_path "/sbin" + prepend_to_path "/usr/X11/bin" + prepend_to_path "/opt/local/bin" + prepend_to_path "$HOME/.local/bin" + prepend_to_path "$HOME/.gem/ruby/2.2.0/bin" + prepend_to_path "$HOME/.cargo/bin" + prepend_to_path "$HOME/bin" +} + +init_path diff --git a/zsh/func/prepend_to_path b/zsh/func/prepend_to_path new file mode 100644 index 0000000..2d2aed4 --- /dev/null +++ b/zsh/func/prepend_to_path @@ -0,0 +1,13 @@ +#!/usr/bin/env zsh +# vim:ft=zsh: +# Eryn Wells + +function prepend_to_path +{ + if [[ -d "$1" ]]; then + path=("$1" $path) + export path + fi +} + +prepend_to_path "$@" diff --git a/zshenv b/zshenv index 9ce7aa4..2687d6d 100644 --- a/zshenv +++ b/zshenv @@ -1,16 +1,24 @@ # .zshenv # vim: ft=zsh -# -# Environment settings for zsh -# # Eryn Wells # Don't read global startup. It messes things up... unsetopt GLOBAL_RCS -[ -e $HOME/.env ] && source $HOME/.env +export SYS=`uname -s | tr A-Z a-z` -# System specific environment settings -[ -e $HOME/.zshenv.$SYS ] && source $HOME/.zshenv.$SYS -# Local environment settings -[ -e $HOME/.zshenv.local ] && source $HOME/.zshenv.local +fpath=("$HOME/.zsh/func" $fpath) +autoload +X prepend_to_path +autoload +X append_to_path +autoload +X init_env +autoload +X init_env_python +autoload +X init_path + +init_path +init_env +init_env_python + +autoload +X init_env_$SYS +if [[ $? ]]; then + init_env_$SYS +fi