From f34d6d0790b8d525ed353a5b13b9eec7ebb428b6 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 8 Jul 2015 11:19:02 +0100 Subject: [PATCH] etc/profile.d --- etc/bashrc | 2 +- etc/bashrc.function | 299 ++++----------------------------------- etc/profile.d/darwin.sh | 18 +++ etc/profile.d/git.sh | 9 ++ etc/profile.d/lib.sh | 6 +- etc/profile.d/nb.sh | 156 ++++++++++++++++++++ etc/profile.d/puppet.sh | 19 +++ lib/perl/NB/Functions.pm | 17 +-- 8 files changed, 236 insertions(+), 290 deletions(-) create mode 100644 etc/profile.d/darwin.sh create mode 100644 etc/profile.d/git.sh create mode 100644 etc/profile.d/nb.sh create mode 100644 etc/profile.d/puppet.sh diff --git a/etc/bashrc b/etc/bashrc index 7c0c41cc..87fa72ba 100644 --- a/etc/bashrc +++ b/etc/bashrc @@ -20,7 +20,7 @@ unset path # # PATHS # -nb_env_add_path &>/dev/null && PATH=`nb_env_add_path "$PATH" \ +env_add_path &>/dev/null && PATH=`env_add_path "$PATH" \ /bin \ /sbin \ /usr/sbin \ diff --git a/etc/bashrc.function b/etc/bashrc.function index 5efd3a4b..900b04bc 100644 --- a/etc/bashrc.function +++ b/etc/bashrc.function @@ -3,9 +3,9 @@ # FUNCTIONS - SH # ################################################################################# -nb_env_add_path() { +env_add_path() { # Add paths to a variables - # Usage PATH=`nb_env_add_path "$PATH" "/blbabla"` + # Usage PATH=`env_add_path "$PATH" "/blbabla"` declare env_value; env_value=$1; shift for p in $@; do @@ -29,9 +29,7 @@ ascii() { } psmem() { - #ps -C $1 -O rss | xargs -r cat | gawk '\ - #ps -p `pgrep -f $1` -O rss 2>/dev/null| xargs -r | gawk '\ - ps=`ps ax -O rss 2>/dev/null` + declare ps; ps=`ps ax -O rss 2>/dev/null` echo "$ps"| awk "\ BEGIN { count = 0; sum= 0; } /^ *[0-9]+/ && /$1/ { count ++; sum += \$2 } @@ -46,43 +44,34 @@ END { } unzipurl() { - declare url=$1 - #declare file=`echo "$url"|sed 's,^.*?\([^/]+\.zip\)$,\1,'` - declare file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'` + declare url file + url=$1 + file=`echo "$url"|awk -F/ '/\.zip$/ { print $NF }'` [ -z "$file" ] && file="$FUNCNAME.zip" - #echo "$url -> $file" wget "$url" -O "$file" && unzip "$file" rm "$file" } bytes2h() { - declare in="cat" args="" + declare in args + if [ "$1" = "-regex" ]; then args="regex=$2" shift shift fi + in="cat" [ -z "$@" ] || in="echo $@" + $in | perl -pe 'BEGIN{ + use NB::Functions qw/&oct2h/; $exp = "([\\d\\.]+)"; if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) { shift @ARGV; $exp = $1; } - sub oct2h { - - my $o = shift @_; - $o = $o * 1024; - - if ($o >= 1099511627776) { return(sprintf("%.2fT",($o/1099511627776))); } # T - elsif ($o >= 1073741824) { return(sprintf("%.2fG",($o/1073741824))); } # G - elsif ($o >= 1048576) { return(int($o/1048576)."M"); } # M - elsif ($o >= 1024) { return(int($o/1024)."K"); } # K - else { return $o."bytes"; } - - } }; -s/$exp/oct2h($1)/ge; +s/$exp/oct2h($1*1024)/ge; ' $args } @@ -90,177 +79,36 @@ yaml2perl() { perl -MYAML -MData::Dumper -e 'print Dumper YAML::Load(join("",<>))' $@ } -puppet2yaml() { - perl -pe ' -s/^(.*)\{(.*?:)\s*/$1:\n $2\n/; -s/\s*=>\s*/: /; -s/,\s*/\n/; -s/\s*\}\s*//; -' $@ -} - -if [ -n "$DARWIN" ]; then - md5sum() { - md5 $@ | sed "s/^MD5 (\(.*\)) = \(.*\)$/\2 \1/" - } -fi - ################################################################################# # # CHECK SHELL # ################################################################################# -case "$BASH" in - */bash) ;; - *) - case "$SHELL" in - */bash) ;; - */zsh) ;; - *) return 0 ;; - esac - ;; -esac +# NB 08.07.15 case "$BASH" in +# NB 08.07.15 */bash) ;; +# NB 08.07.15 *) +# NB 08.07.15 case "$SHELL" in +# NB 08.07.15 */bash) ;; +# NB 08.07.15 */zsh) ;; +# NB 08.07.15 *) return 0 ;; +# NB 08.07.15 esac +# NB 08.07.15 ;; +# NB 08.07.15 esac ################################################################################# # # FUNCTIONS - BASH # ################################################################################# -if [ -n "$DARWIN" ]; then - - mac-xmessage() { - osascript -e "tell app \"System Events\" to display dialog \"$@\"" - } - - mac-config() { - echo "Finder path : YES" - defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES - echo "Finder quit : YES" - defaults write com.apple.finder QuitMenuItem -bool YES - echo "Finder show all files : FALSE" - defaults write com.apple.finder AppleShowAllFiles FALSE - } - -fi - -nb_mails() { - printf '| 1-$ cat\nx\n' | mail | perl -MDate::Manip -MEncode -ne ' -BEGIN { $SIG{__WARN__} = sub{ } } -$. == 2 and print; -/^From / .. /^$/ or next; -chomp; -if ($_ eq "") { - $h{From} =~ s/^.*?([^<\s]+@[^\s>]+).*?$/$1/; - $h{Date} = &UnixDate($h{Date},"%Y-%m-%d %H:%M"); - $h{Subject} = decode("MIME-Header",$h{Subject}); - push(@m,[ - $h{From}, - $h{Date}, - $h{Subject}, - ]); - %h = (); - next; -} -/^(Date|Subject|From): (.*)$/ and $h{$1} = $2; -END { print map {sprintf "%20.20s | %16.16s | %s\n",@$_;} sort {$b->[1] cmp $a->[1]} @m } -' -#/var/spool/mail/$USER - #printf 'f *\nx\n' | mail | perl -e '@_=<>; shift @_; print shift @_; print reverse @_' -} - -nb_git_create() { -# NB 19.02.15 root@pi:puppet# nb_git_create puppet/data -# NB 19.02.15 Initialized empty Git repository in /home/git/puppet/data.git/ -# NB 19.02.15 Cloning into 'data'... -# NB 19.02.15 warning: You appear to have cloned an empty repository. -# NB 19.02.15 Got to directory: data -# NB 19.02.15 Reinitialized existing Git repository in /etc/puppet/data/.git/ -# NB 19.02.15 [master (root-commit) 3ea2a65] Created by nb_git_create -# NB 19.02.15 1 file changed, 7 insertions(+) -# NB 19.02.15 create mode 100644 README.md -# NB 19.02.15 Counting objects: 3, done. -# NB 19.02.15 Compressing objects: 100% (2/2), done. -# NB 19.02.15 Writing objects: 100% (3/3), 272 bytes, done. -# NB 19.02.15 Total 3 (delta 0), reused 0 (delta 0) -# NB 19.02.15 To git@git.nbdom.net:puppet/data.git -# NB 19.02.15 * [new branch] master -> master -# NB 19.02.15 Branch master set up to track remote branch master from origin. -# NB 19.02.15 fatal: remote origin already exists. -# NB 19.02.15 Branch master set up to track remote branch master from origin. -# NB 19.02.15 Everything up-to-date - declare name="${1%.git}" - declare dir=$(basename $name) - declare repo='git@git.nbdom.net' - - # - # Check, create repo on server - # - [ -e "$dir" ] && echo ">$dir already exists!" 1>&2 && return 1 - - ssh git@git.nbdom.net "git init --bare ${name}.git" || return - git clone $repo:${name}.git || return - - echo ">Got to directory: $dir" - cd $dir || return - - # - # Create files - # - echo "$name -============= - -## Synopsis - -DESCRIPTION -" > README.md - - # - # Update repo - # - git init - git add * - git commit -m "Created by $FUNCNAME" - git push --set-upstream origin master - git remote add origin $repo:${name}.git -} - -nb_git_clone() { - git clone git@git.nbdom.net:${1%.git}.git -} - -git-commit-push() { - git commit -m "$@" -a - git push #|| echo "Try: git push --set-upstream origin master" -} - -git-commit-push-add() { - git add . - git-commit-push "$@" -} - -puppet-upgrade-modules() { - local m - local pwd=$PWD - cd /etc/puppet/modules - for m in $(puppet module list | awk '/\(.*v/ {print $2}'); do - puppet module upgrade "$m" - done - cd $pwd -} - -nb_rb() { - ruby -r/etc/puppet/modules/nb/lib/nb.rb -e "$@" -} - -nb_hl() { +hl() { cat | grep --color=auto -E "(^|($@))" } -ls-tree() { +ls_tree() { tree -tDphug } -find-sort-mtime() { +find_sort_mtime() { ( if [ -n "$DARWIN" ]; then find $@ -type f -exec stat -f '%m'$'\t''%N' {} \; @@ -270,102 +118,9 @@ find-sort-mtime() { ) | sort -k1 -n | cut -f 2 | head } -nb_alert() { - local cmd=". $NB_ROOT/etc/profile" - local host=macbook.brighton.loc - case "$1" in - led) - host=pi.brighton.loc - cmd="$cmd && echo $FUNCNAME > /var/run/gpiod.sock" - ;; - say) - host=pi.brighton.loc - shift - cmd="$cmd && say <<< \"$@\"" - ;; - beep) - host=pi.brighton.loc - cmd="$cmd && play 2>/dev/null -v 5 /home/pi/python_games/beep1.ogg" - ;; - -h|-help|--help) - echo "Usage: $FUNCNAME [led|say|beep|-h] OR TEXT" - return - ;; - *) - host=macbook.brighton.loc - shift - cmd="$cmd && mac-xmessage \"$FUNCNAME: $@\" 1>/dev/null" - ;; - esac - cmd="bash -c '$cmd'" - - local ip=`dig +short $host` - [ -z "$ip" ] && echo "Can't resolve macbook.brighton.loc" && return 1 - - nb_ips | grep -qFm1 "$ip" || cmd="ssh -o BatchMode=yes $host -- $cmd" - - #[ "$USER" != "nico" ] && cmd="su - nico -c '$cmd'" - [ "$USER" != "nico" ] && cmd="sudo -u nico -- $cmd" -# NB 09.06.15 if [ "$USER" != "nico" ]; then -# NB 09.06.15 cmd="sudo -u nico -- $cmd" -# NB 09.06.15 else -# NB 09.06.15 cmd="echo '$cmd'" -# NB 09.06.15 fi - eval "$cmd" -} - -nb_ips() { - ifconfig | perl -ne '/^\s*inet (?:addr:)?([\d\.]+)/ and $1 ne "127.0.0.1" and print "$1\n"' -} - -nb_radio_play() { - local radios="\ -1 | FranceInter | http://www.tv-radio.com/station/france_inter_mp3/france_inter_mp3-128k.m3u -2 | Indie | http://107.155.126.42:17160/listen.pls -3 | FranceInfo | http://players.creacast.com/creacast/france_info/playlist.m3u -4 | FranceCulture | http://www.tv-radio.com/station/france_culture_mp3/france_culture_mp3-128k.m3u - -Choose one > \ -" - if [ -z "$@" ]; then - read -p "$radios" - else - REPLY="$@" - fi - - test -z "$REPLY" 2>/dev/null && return - awk -F '|' '$1 == '$REPLY' {print $3; exit}' <<< "$radios" | sed 's/^ //' | xargs mplayer 2>/dev/null -} - -# NB 06.07.15 alias nb_img2jpg='convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85%' -# NB 06.07.15 -# NB 06.07.15 nb_img_compress() { -# NB 06.07.15 local IFS=$'\n' i='' j='' -# NB 06.07.15 for i in $@; do -# NB 06.07.15 [ -e "$i" ] || continue -# NB 06.07.15 j="${i%.*}.jpg" -# NB 06.07.15 echo "$i -> $j" -# NB 06.07.15 nb_img2jpg $i $j || continue -# NB 06.07.15 [ "$i" != "$j" ] && rm "$i" -# NB 06.07.15 done -# NB 06.07.15 } -# NB 06.07.15 -# NB 06.07.15 nb_img_compress_gray() { -# NB 06.07.15 # See: http://www.imagemagick.org/script/command-line-options.php#colorspace -# NB 06.07.15 # See: convert -list colorspace -# NB 06.07.15 local IFS=$'\n' i='' j='' -# NB 06.07.15 for i in $@; do -# NB 06.07.15 [ -e "$i" ] || continue -# NB 06.07.15 j="${i%.*}.jpg" -# NB 06.07.15 echo "$i -> $j" -# NB 06.07.15 nb_img2jpg -colorspace Gray -normalize -level 10%,90% -sharpen 0x1 $i $j || continue -# NB 06.07.15 [ "$i" != "$j" ] && rm "$i" -# NB 06.07.15 done -# NB 06.07.15 } - replace_tag_content() { - declare tag="$1"; shift - declare content="$@" + declare tag; tag="$1"; shift + declare content; content="$@" perl -ne 'BEGIN { $tag = shift(@ARGV)." - replace_tag_content"; $t1 = "# >>> $tag"; diff --git a/etc/profile.d/darwin.sh b/etc/profile.d/darwin.sh new file mode 100644 index 00000000..82e379dc --- /dev/null +++ b/etc/profile.d/darwin.sh @@ -0,0 +1,18 @@ +[ -z "$DARWIN" ] && return 0 + +md5sum() { + md5 $@ | sed "s/^MD5 (\(.*\)) = \(.*\)$/\2 \1/" +} + +mac-xmessage() { + osascript -e "tell app \"System Events\" to display dialog \"$@\"" +} + +mac-config() { + echo "Finder path : YES" + defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES + echo "Finder quit : YES" + defaults write com.apple.finder QuitMenuItem -bool YES + echo "Finder show all files : FALSE" + defaults write com.apple.finder AppleShowAllFiles FALSE +} diff --git a/etc/profile.d/git.sh b/etc/profile.d/git.sh new file mode 100644 index 00000000..fd53699e --- /dev/null +++ b/etc/profile.d/git.sh @@ -0,0 +1,9 @@ +git_commit_push() { + git commit -m "$@" -a + git push #|| echo "Try: git push --set-upstream origin master" +} + +git_commit_push_add() { + git add . + git-commit-push "$@" +} diff --git a/etc/profile.d/lib.sh b/etc/profile.d/lib.sh index 679a7f36..efb10da1 100644 --- a/etc/profile.d/lib.sh +++ b/etc/profile.d/lib.sh @@ -1,3 +1,3 @@ -export PERL5LIB=`nb_env_add_path "$PERL5LIB" "$NB_ROOT/lib/perl"` -export RUBYLIB=`nb_env_add_path "$RUBYLIB" "$NB_ROOT/lib/ruby|"` -export PYTHONPATH=`nb_env_add_path "$PYTHONPATH" "$NB_ROOT/lib/python"` +export PERL5LIB=`env_add_path "$PERL5LIB" "$NB_ROOT/lib/perl"` +export RUBYLIB=`env_add_path "$RUBYLIB" "$NB_ROOT/lib/ruby|"` +export PYTHONPATH=`env_add_path "$PYTHONPATH" "$NB_ROOT/lib/python"` diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh new file mode 100644 index 00000000..095608f5 --- /dev/null +++ b/etc/profile.d/nb.sh @@ -0,0 +1,156 @@ +nb_mails() { + printf '| 1-$ cat\nx\n' | mail | perl -MDate::Manip -MEncode -ne ' +BEGIN { $SIG{__WARN__} = sub{ } } +$. == 2 and print; +/^From / .. /^$/ or next; +chomp; +if ($_ eq "") { + $h{From} =~ s/^.*?([^<\s]+@[^\s>]+).*?$/$1/; + $h{Date} = &UnixDate($h{Date},"%Y-%m-%d %H:%M"); + $h{Subject} = decode("MIME-Header",$h{Subject}); + push(@m,[ + $h{From}, + $h{Date}, + $h{Subject}, + ]); + %h = (); + next; +} +/^(Date|Subject|From): (.*)$/ and $h{$1} = $2; +END { print map {sprintf "%20.20s | %16.16s | %s\n",@$_;} sort {$b->[1] cmp $a->[1]} @m } +' +#/var/spool/mail/$USER + #printf 'f *\nx\n' | mail | perl -e '@_=<>; shift @_; print shift @_; print reverse @_' +} + +nb_git_create() { +# NB 19.02.15 root@pi:puppet# nb_git_create puppet/data +# NB 19.02.15 Initialized empty Git repository in /home/git/puppet/data.git/ +# NB 19.02.15 Cloning into 'data'... +# NB 19.02.15 warning: You appear to have cloned an empty repository. +# NB 19.02.15 Got to directory: data +# NB 19.02.15 Reinitialized existing Git repository in /etc/puppet/data/.git/ +# NB 19.02.15 [master (root-commit) 3ea2a65] Created by nb_git_create +# NB 19.02.15 1 file changed, 7 insertions(+) +# NB 19.02.15 create mode 100644 README.md +# NB 19.02.15 Counting objects: 3, done. +# NB 19.02.15 Compressing objects: 100% (2/2), done. +# NB 19.02.15 Writing objects: 100% (3/3), 272 bytes, done. +# NB 19.02.15 Total 3 (delta 0), reused 0 (delta 0) +# NB 19.02.15 To git@git.nbdom.net:puppet/data.git +# NB 19.02.15 * [new branch] master -> master +# NB 19.02.15 Branch master set up to track remote branch master from origin. +# NB 19.02.15 fatal: remote origin already exists. +# NB 19.02.15 Branch master set up to track remote branch master from origin. +# NB 19.02.15 Everything up-to-date + declare name="${1%.git}" + declare dir=$(basename $name) + declare repo='git@git.nbdom.net' + + # + # Check, create repo on server + # + [ -e "$dir" ] && echo ">$dir already exists!" 1>&2 && return 1 + + ssh git@git.nbdom.net "git init --bare ${name}.git" || return + git clone $repo:${name}.git || return + + echo ">Got to directory: $dir" + cd $dir || return + + # + # Create files + # + echo "$name +============= + +## Synopsis + +DESCRIPTION +" > README.md + + # + # Update repo + # + git init + git add * + git commit -m "Created by $FUNCNAME" + git push --set-upstream origin master + git remote add origin $repo:${name}.git +} + +nb_git_clone() { + git clone git@git.nbdom.net:${1%.git}.git +} + +nb_rb() { + ruby -r/etc/puppet/modules/nb/lib/nb.rb -e "$@" +} + +nb_alert() { + local cmd=". $NB_ROOT/etc/profile" + local host=macbook.brighton.loc + case "$1" in + led) + host=pi.brighton.loc + cmd="$cmd && echo $FUNCNAME > /var/run/gpiod.sock" + ;; + say) + host=pi.brighton.loc + shift + cmd="$cmd && say <<< \"$@\"" + ;; + beep) + host=pi.brighton.loc + cmd="$cmd && play 2>/dev/null -v 5 /home/pi/python_games/beep1.ogg" + ;; + -h|-help|--help) + echo "Usage: $FUNCNAME [led|say|beep|-h] OR TEXT" + return + ;; + *) + host=macbook.brighton.loc + shift + cmd="$cmd && mac-xmessage \"$FUNCNAME: $@\" 1>/dev/null" + ;; + esac + cmd="bash -c '$cmd'" + + local ip=`dig +short $host` + [ -z "$ip" ] && echo "Can't resolve macbook.brighton.loc" && return 1 + + nb_ips | grep -qxFm1 "$ip" || cmd="ssh -o BatchMode=yes $host -- $cmd" + + #[ "$USER" != "nico" ] && cmd="su - nico -c '$cmd'" + [ "$USER" != "nico" ] && cmd="sudo -u nico -- $cmd" +# NB 09.06.15 if [ "$USER" != "nico" ]; then +# NB 09.06.15 cmd="sudo -u nico -- $cmd" +# NB 09.06.15 else +# NB 09.06.15 cmd="echo '$cmd'" +# NB 09.06.15 fi + eval "$cmd" +} + +nb_ips() { + ifconfig | perl -ne '/^\s*inet (?:addr:)?([\d\.]+)/ and $1 ne "127.0.0.1" and print "$1\n"' +} + +nb_radio_play() { + local radios="\ +1 | FranceInter | http://www.tv-radio.com/station/france_inter_mp3/france_inter_mp3-128k.m3u +2 | Indie | http://107.155.126.42:17160/listen.pls +3 | FranceInfo | http://players.creacast.com/creacast/france_info/playlist.m3u +4 | FranceCulture | http://www.tv-radio.com/station/france_culture_mp3/france_culture_mp3-128k.m3u + +Choose one > \ +" + if [ -z "$@" ]; then + read -p "$radios" + else + REPLY="$@" + fi + + test -z "$REPLY" 2>/dev/null && return + awk -F '|' '$1 == '$REPLY' {print $3; exit}' <<< "$radios" | sed 's/^ //' | xargs mplayer 2>/dev/null +} + diff --git a/etc/profile.d/puppet.sh b/etc/profile.d/puppet.sh new file mode 100644 index 00000000..6b4fb5fa --- /dev/null +++ b/etc/profile.d/puppet.sh @@ -0,0 +1,19 @@ +puppet2yaml() { + perl -pe ' +s/^(.*)\{(.*?:)\s*/$1:\n $2\n/; +s/\s*=>\s*/: /; +s/,\s*/\n/; +s/\s*\}\s*//; +' $@ +} + +puppet_upgrade_modules() { + local m + local pwd=$PWD + cd /etc/puppet/modules + for m in $(puppet module list | awk '/\(.*v/ {print $2}'); do + puppet module upgrade "$m" + done + cd $pwd +} + diff --git a/lib/perl/NB/Functions.pm b/lib/perl/NB/Functions.pm index 9d461240..937880ea 100644 --- a/lib/perl/NB/Functions.pm +++ b/lib/perl/NB/Functions.pm @@ -3,6 +3,7 @@ use strict; use Encode qw(&encode &decode &encode_utf8 &decode_utf8 &from_to); use Carp; +use POSIX; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $VAR1); @@ -273,18 +274,6 @@ sub sec2h { return sprintf("\%d day(s) %02d:%02d:%02d",$days,$hours,$minutes,$seconds); -# NB 10.05.11 my $time = shift @_; -# NB 10.05.11 -# NB 10.05.11 my $d = int(($time / (60 * 60 * 60))+0.6); -# NB 10.05.11 -# NB 10.05.11 my $h = int($time / (60 * 60))-($d*24); -# NB 10.05.11 -# NB 10.05.11 my $m = int($time / 60) % 60; -# NB 10.05.11 -# NB 10.05.11 my $s = $time % 60; -# NB 10.05.11 -# NB 10.05.11 return sprintf("\%d day(s) %02d:%02d:%02d",$d,$h,$m,$s); - } sub oct2h { @@ -293,8 +282,8 @@ sub oct2h { if ($o >= 1099511627776) { return(sprintf('%.2fT',($o/1099511627776))); } # T elsif ($o >= 1073741824) { return(sprintf('%.2fG',($o/1073741824))); } # G - elsif ($o >= 1048576) { return(int($o/1048576)."M"); } # M - elsif ($o >= 1024) { return(int($o/1024)."K"); } # K + elsif ($o >= 1048576) { return(POSIX::ceil($o/1048576)."M"); } # M + elsif ($o >= 1024) { return(POSIX::ceil($o/1024)."K"); } # K else { return $o."bytes"; } } -- 2.47.3