From a31e294a871bdacaf812c499428600cdfceb8489 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Sat, 26 Feb 2022 17:19:37 -0800 Subject: [PATCH] feat: add source bundle function which will convert any module_load command into the prepended source. refactor: dirs module, cleanup feat: added prepend_file function to file module --- modules/utility/bundle.sh | 49 +++++++++++++++++++++++++++++++++++++++ modules/utility/dirs.sh | 47 +++++++++++-------------------------- modules/utility/file.lib | 35 +++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 36 deletions(-) create mode 100644 modules/utility/bundle.sh diff --git a/modules/utility/bundle.sh b/modules/utility/bundle.sh new file mode 100644 index 0000000..9e7a8e4 --- /dev/null +++ b/modules/utility/bundle.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# $1 file to bundle +# $2 bundled output file (optional) +# if no output file then the bundled source is returned +# it can be immediately invoked like this +# source <(bundle myscript.sh) + +bundle () { + +[[ ! -f $1 ]] && return 1 +module_load file + +if [[ ! $2 == "__recurse__" ]]; then +tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX ) +chmod 600 "$tmp_file" +cp $1 $tmp_file +else +tmp_file=$1 +fi +# echo current temp file: $tmp_file +modules=$(sed -n -e 's/^module_load //p' < $tmp_file) +# echo found: $modules +# return +if [[ $modules ]]; then + # echo Modules: $modules + sed -i '/module_load/d' $tmp_file + for module in $(printf '%s\n' "${modules[@]}"|tac); + do + # echo module: $module + # echo module path: $(module_find $module) + prepend_file $(module_find $module) $tmp_file + done + bundle $tmp_file __recurse__ +fi + +if [[ ! $2 == "__recurse__" ]]; then + if [[ $2 ]]; then + cp $tmp_file $2 + echo $2 + else + echo -e "$(cat $tmp_file)" + fi + rm -f $tmp_file; + return 0 + fi +} + +# # if script was executed then call the function +(return 0 2>/dev/null) || bundle $@ diff --git a/modules/utility/dirs.sh b/modules/utility/dirs.sh index ac030de..ce215d1 100644 --- a/modules/utility/dirs.sh +++ b/modules/utility/dirs.sh @@ -1,35 +1,4 @@ - -# find /path/to/base/dir -type d -exec chmod 755 {} + -# To recursively give files read privileges: - -# find /path/to/base/dir -type f -exec chmod 644 {} + -# Or, if there are many objects to process: - -# chmod 755 $(find /path/to/base/dir -type d) -# chmod 644 $(find /path/to/base/dir -type f) -# Or, to reduce chmod spawning: - -# find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 -# find /path/to/base/dir -type f -print0 | xargs -0 chmod 644 - - - -# chmodr.sh -# -# author: Francis Byrne -# date: 2011/02/12 -# -# Generic Script for recursively setting permissions for directories and files -# to defined or default permissions using chmod. -# -# Takes a path to recurse through and options for specifying directory and/or -# file permissions. -# Outputs a list of affected directories and files. -# -# If no options are specified, it recursively resets all directory and file -# permissions to the default for most OSs (dirs: 755, files: 644). - -# Usage message +################## BEGIN: MODULE dirs ############### isDir() { if [[ -d $1 ]] @@ -43,6 +12,16 @@ isDir() { chmodr () { +# Generic Script for recursively setting permissions for directories and files +# to defined or default permissions using chmod. +# +# Takes a path to recurse through and options for specifying directory and/or +# file permissions. +# Outputs a list of affected directories and files. +# +# If no options are specified, it recursively resets all directory and file +# permissions to the default for most OSs (dirs: 755, files: 644). + usage() { echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS" @@ -95,4 +74,6 @@ if [ -n "$FILEPERMS" ] ; then find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS fi -} \ No newline at end of file +} + +################## END: MODULE dirs ############### \ No newline at end of file diff --git a/modules/utility/file.lib b/modules/utility/file.lib index f10d5d0..7de9595 100644 --- a/modules/utility/file.lib +++ b/modules/utility/file.lib @@ -1,7 +1,8 @@ #!/bin/bash # export BASH_DEBUG=true -module_load debug +# module_load debug +module_load iecho isFile() { if [[ -f $1 ]] @@ -14,11 +15,11 @@ isFile() { } build_file () { - [[ -f "$2" ]] || (echo "output file $2 does not exist";return 1) + [[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1) if [[ -f "$1" ]]; then echo -e "\n####### ADDED $1 ########" >> $2 cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2 - else debug "no such file $1 to append" + else iecho "no such file $1 to append" fi } @@ -201,3 +202,31 @@ source_dir () { done } + + + +prepend_file () { +# ---------------------------------------------------------------------------------------------------------------------- +# usage prepend_file +# ---------------------------------------------------------------------------------------------------------------------- +# Prepend the contents of [$1], to [$2], leaving the result in [$2]. +# insert a newline at the end of [$1] if necessary +# ---------------------------------------------------------------------------------------------------------------------- + +# check # echo $1 $2 +[[ -f $1 ]] || { iecho "no file $1 to prepend";return 1; } +[[ -f $2 ]] || { iecho "no file $2 to which to prepend $1";return 2; } +# init +tmp_fn=$( mktemp -t TEMP_FILE_prepend.XXXXXXXX ) +chmod 600 "$tmp_fn" +\cp $1 $tmp_fn +sed -i '$a\' $tmp_fn +cat $2 >> $tmp_fn +\mv "$tmp_fn" "$2" +# cleanup +rm -f "$tmp_fn" +iecho "file $1 prepened to $2" +return 0 + +# [End] +} \ No newline at end of file