From 9ae8cdec2259e500c4ba005df992835c19b8354c Mon Sep 17 00:00:00 2001 From: JesusPerez Date: Wed, 1 Sep 2021 20:44:18 +0100 Subject: [PATCH] chore: add scripts --- clean_debug.sh | 6 +++++ clean_targets.sh | 6 +++++ data_test.sh | 54 ++++++++++++++++++++++++++++++++++++++++ lang_test.sh | 48 ++++++++++++++++++++++++++++++++++++ save.sh | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ size_targets.sh | 5 ++++ 6 files changed, 183 insertions(+) create mode 100755 clean_debug.sh create mode 100755 clean_targets.sh create mode 100755 data_test.sh create mode 100755 lang_test.sh create mode 100755 save.sh create mode 100755 size_targets.sh diff --git a/clean_debug.sh b/clean_debug.sh new file mode 100755 index 0000000..91678c3 --- /dev/null +++ b/clean_debug.sh @@ -0,0 +1,6 @@ +#!/bin/bash +for it in $(find . | grep "target/debug$") +do + echo "$it" + rm -r "$it" +done diff --git a/clean_targets.sh b/clean_targets.sh new file mode 100755 index 0000000..61d00f4 --- /dev/null +++ b/clean_targets.sh @@ -0,0 +1,6 @@ +#!/bin/bash +for it in $(find . | grep "target$") +do + echo "$it" + rm -r "$it" +done diff --git a/data_test.sh b/data_test.sh new file mode 100755 index 0000000..65d3586 --- /dev/null +++ b/data_test.sh @@ -0,0 +1,54 @@ +#!/bin/bash +USAGE="test_data.sh root-path data-path data-format total-entries out-format" +[ "$1" == "-h" ] && echo "$USAGE" && exit +[ -n "$1" ] && DATA_PATH=$1 +if [ -z "$DATA_PATH" ] && [ -r "." ] ; then + DATA_PATH="." + DATA_TARGET="data.yaml" + DATA_FORMAT="yaml" +fi +[ ! -d "$DATA_PATH" ] && echo "Root path: $DATA_PATH not found" && DATA_PATH="" +[ -z "$DATA_PATH" ] && echo -n "Root path for data: " && read -r DATA_PATH && echo "" +[ -n "$2" ] && DATA_TARGET=$2 +[ ! -r "$DATA_PATH/$DATA_TARGET" ] && echo "Target path: $DATA_TARGET not found in $DATA_PATH" && DATA_TARGET="" +[ -z "$DATA_TARGET" ] && echo -n "Path for target data: " && read -r DATA_TARGET && echo "" +[ -n "$3" ] && [ -z "$DATA_FORMAT" ] && DATA_FORMAT=$3 +DATA_TARGET=$(echo "$DATA_TARGET" | cut -f1 -d".") +format=$(basename "$DATA_TARGET" | cut -f2 -d".") +if [ -z "$DATA_FORMAT" ] ;then + echo -ne "Path: $DATA_PATH\nformat (yaml,toml,json): $format [ENTER or change] " + read -r DATA_FORMAT && echo "" +fi +case "$DATA_FORMAT" in + yaml|YAML) DATA_FORMAT="yaml" ;; + toml|TOML) DATA_FORMAT="toml" ;; + json|JSON) DATA_FORMAT="json" ;; + *) DATA_FORMAT=$format + # echo "Format \"$DATA_FORMAT\" not defined" && exit 1 +esac +[ -n "$4" ] && DATA_TOTAL=$4 +if [ -z "$DATA_TOTAL" ] ; then + echo -ne "Expected NUMBER or items to be loaded from\npath: $DATA_PATH\nwith format $DATA_FORMAT [number]: " + read -r DATA_TOTAL + [[ ! $DATA_TOTAL =~ ^[[:digit:]]+$ ]] && echo "DATA_TOTAL not a number" && exit 1 +fi +[ -n "$5" ] && DATA_OUT_FORMAT=$5 +out_format="" +if [ -z "$DATA_OUT_FORMAT" ] ;then + echo -ne "Path: $DATA_LANG_PATH\nOUTPUT format (yaml,toml,json): $out_format [ENTER or change] " + read -r DATA_OUT_FORMAT && echo "" +fi +case "$DATA_OUT_FORMAT" in + yaml|YAML) DATA_OUT_FORMAT="yaml" ;; + toml|TOML) DATA_OUT_FORMAT="toml" ;; + json|JSON) DATA_OUT_FORMAT="json" ;; + -) DATA_OUT_FORMAT="" ;; + *) echo "Format \"$DATA_LANG_FORMAT\" " + #DATA_OUT_FORMAT=$out_format +esac +export DATA_PATH +export DATA_TARGET +export DATA_FORMAT +export DATA_TOTAL +export DATA_OUT_FORMAT +cargo test load_data -- --nocapture diff --git a/lang_test.sh b/lang_test.sh new file mode 100755 index 0000000..043a970 --- /dev/null +++ b/lang_test.sh @@ -0,0 +1,48 @@ +#!/bin/bash +USAGE="test_lang.sh lang-file-root-path file-target file-format-extension format-output section-or-all" +[ "$1" == "-h" ] && echo "$USAGE" && exit +[ -r "$1" ] && DATA_LANG_PATH=$1 +if [ -z "$DATA_LANG_PATH" ] && [ -r "es.yaml" ] ; then + DATA_LANG_PATH="." + DATA_LANG_TARGET="es" + DATA_LANG_FORMAT="yaml" +fi +[ -n "$2" ] && [ -z "$DATA_LANG_TARGET" ] && DATA_LANG_TARGET=$2 +[ -n "$3" ] && [ -z "$DATA_LANG_FORMAT" ] && DATA_LANG_FORMAT=$3 +[ -z "$DATA_LANG_PATH" ] && echo -n "Root path for lang: " && read -r DATA_LANG_PATH && echo "" +[ -z "$DATA_LANG_TARGET" ] && echo -n "Target for lang: " && read -r DATA_LANG_PATH && echo "" +format=$(basename "$DATA_LANG_TARGET" | cut -f2 -d".") +[ -n "$format" ] && DATA_LANG_TARGET=$(echo "$DATA_LANG_TARGET" | cut -f1 -d"."); +if [ -z "$DATA_LANG_FORMAT" ] ;then + echo -ne "Path: $DATA_LANG_PATH\nformat (yaml,toml,json): $format [ENTER or change] " + read -r DATA_LANG_FORMAT && echo "" +fi +case "$DATA_LANG_FORMAT" in + yaml|YAML) DATA_LANG_FORMAT="yaml" ;; + toml|TOML) DATA_LANG_FORMAT="toml" ;; + json|JSON) DATA_LANG_FORMAT="json" ;; + *) DATA_LANG_FORMAT=$format + # echo "Format \"$DATA_LANG_FORMAT\" not defined" && exit 1 +esac +[ ! -r "$DATA_LANG_PATH/$DATA_LANG_TARGET.$DATA_LANG_FORMAT" ] && echo "$DATA_LANG_PATH/$DATA_LANG_TARGET.$DATA_LANG_FORMAT not found" && exit 1 +[ -n "$4" ] && DATA_LANG_OUT_FORMAT=$4 +out_format=$format +if [ -z "$DATA_LANG_OUT_FORMAT" ] ;then + echo -ne "Path: $DATA_LANG_PATH\nOUTPUT format (yaml,toml,json): $out_format [ENTER or change] " + read -r DATA_LANG_OUT_FORMAT && echo "" +fi +case "$DATA_LANG_OUT_FORMAT" in + yaml|YAML) DATA_LANG_OUT_FORMAT="yaml" ;; + toml|TOML) DATA_LANG_OUT_FORMAT="toml" ;; + json|JSON) DATA_LANG_OUT_FORMAT="json" ;; + -) DATA_LANG_OUT_FORMAT="" ;; + *) echo "Format \"$DATA_LANG_FORMAT\" " + #DATA_LANG_OUT_FORMAT=$out_format +esac +[ -n "$5" ] && DATA_LANG_SECTION=$5 +export DATA_LANG_PATH +export DATA_LANG_TARGET +export DATA_LANG_FORMAT +export DATA_LANG_SECTION +export DATA_LANG_OUT_FORMAT +cargo test load_lang -- --nocapture diff --git a/save.sh b/save.sh new file mode 100755 index 0000000..28f5630 --- /dev/null +++ b/save.sh @@ -0,0 +1,64 @@ +#!/bin/bash +USAGE="save.sh [-v (verbose)] list_files_to_save root_path_to_save" +[ "$1" == "-h" ] && echo "$USAGE" && exit 0 +[ "$1" == "-v" ] && VERBOSE="v" && shift +LIST_TO_SAVE=${LIST_TO_SAVE:-list_to_save} +[ -z "$TARGET_PATH" ] && echo "TARGET_PATH not defined in enviroment" && exit 1 +[ -n "$1" ] && [ -r "$1" ] && LIST_TO_SAVE=$1 +[ -n "$2" ] && [ -r "$2" ] && TARGET_PATH=$2 + +[ ! -d "$TARGET_PATH" ] && echo "$TARGET_PATH for root save not found" && exit 1 +# A manual method scanning path to get files +# Alternative method in one line: +# $(find "." -type f | grep -v "target/" | grep -v .git) +get_list() { + local path + for it in "$1"/??* + do + [[ "$it" =~ /node_modules ]] && continue + [[ "$it" =~ /target ]] && continue + [[ "$it" =~ /.git ]] && continue + if [ -d "$it" ] ; then + get_list "$it" + else + # Avoid use of sed (shellcheck SC2001) + path="${it//"$ROOT_PATH"//}" + # If no files in this path so copy path itself + [ "$(basename "$path")" == "??*" ] && path=$(dirname "$path") + echo "${path//\.\//}" + fi + done + # scan hidden files + for it in "$1"/.??* + do + [[ "$it" =~ /.git ]] && continue + [ "$(basename "$it")" == ".??*" ] && continue + echo "${it//\.\//}" + done +} +WK_FILE="/tmp/save.$$" +if [ ! -r "$LIST_TO_SAVE" ] ; then + echo "$LIST_TO_SAVE not defined" + echo "Using default list no 'target' no 'node_modules' no '.git'" + get_list "." > "$WK_FILE" + LIST="$WK_FILE" +else + LIST="$LIST_TO_SAVE" +fi + +DEBUG=${DEBUG:-0} + +now_is=$(date +%Y_%m_%d_%H%M%S) + +SRC_PATH=$(basename "$PWD") +TARGET=$TARGET_PATH/$SRC_PATH"_$now_is.tar.gz" + +echo "---------------------------" +line_info="$(wc -l "$LIST" | cut -f1 -d"/" | tr -d " " ) files" +[ -z "$VERBOSE" ] && echo "Saving $line_info ..." +if tar cz"$VERBOSE"f "$TARGET" --files-from "$LIST" ; then + [ -n "$VERBOSE" ] && echo -e "\n$line_info saved !" + echo -e "\nDone save $SRC_PATH to $TARGET" +fi +echo "---------------------------" +rm -f "$WK_FILE" diff --git a/size_targets.sh b/size_targets.sh new file mode 100755 index 0000000..e522c5f --- /dev/null +++ b/size_targets.sh @@ -0,0 +1,5 @@ +#!/bin/bash +for it in $(find . | grep "target$") +do + du -hs "$it" +done