Skip to content

Commit

Permalink
Still improving modularity in the shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nbro committed Feb 15, 2017
1 parent b891d8b commit 31c4aa9
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 280 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ venv/
env/
*.egg-info/
__pycache__/
/scripts/is_function.sh
103 changes: 49 additions & 54 deletions scripts/_source_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,52 @@
# names are passed as parameters to the same function. #
#################################################################################

_source_script()
{
# Define a few colors for the possible error messages below.
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)

EXPECTED_DIR="scripts"

if [ "$#" -ge "1" ]
then
EXPECTED_DIR=$1
fi

# Based on: http://stackoverflow.com/a/1371283/3924118
CURRENT_DIR=${PWD##*/}

if [ "$CURRENT_DIR" = "$EXPECTED_DIR" ]
then
for (( arg = 2; arg <= $#; arg++ ))
do
# Based on:
# - http://askubuntu.com/questions/306851/how-to-import-a-variable-from-a-script
# - http://unix.stackexchange.com/questions/114300/whats-the-meaning-of-a-dot-before-a-command-in-shell
# - http://stackoverflow.com/questions/20094271/bash-using-dot-or-source-calling-another-script-what-is-difference

printf ". ./${!arg}.sh\n"
# Try to load script ${!arg}.sh
. ./${!arg}.sh

# If it was not loaded successfully, exit with status 1.
if [ $? -ne 0 ]
then
printf "${RED}Script '${!arg}.sh' not loaded successfully. Exiting...${NORMAL}\n"
exit 1
fi
done
else
printf "No script loaded: $CURRENT_DIR != $EXPECTED_DIR.\n"
exit 1
fi
}

is_function()
{
# Based on: http://stackoverflow.com/a/85903/3924118
if [ -n "$(type -t $1)" ] && [ "$(type -t $1)" = function ]
then
# true
return 0
else
# false
return 1
fi
}
export ALREADY_SOURCED__SOURCE_SCRIPT

if [ -z "${ALREADY_SOURCED__SOURCE_SCRIPT}" ]
then
_source_script()
{
# Define a few colors for the possible error messages below.
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)

EXPECTED_DIR="scripts"

if [ "$#" -ge "1" ]
then
EXPECTED_DIR=$1
fi

# Based on: http://stackoverflow.com/a/1371283/3924118
CURRENT_DIR=${PWD##*/}

if [ "$CURRENT_DIR" = "$EXPECTED_DIR" ]
then
for (( arg = 2; arg <= $#; arg++ ))
do
# Based on:
# - http://askubuntu.com/questions/306851/how-to-import-a-variable-from-a-script
# - http://unix.stackexchange.com/questions/114300/whats-the-meaning-of-a-dot-before-a-command-in-shell
# - http://stackoverflow.com/questions/20094271/bash-using-dot-or-source-calling-another-script-what-is-difference

printf ". ./%s.sh\n" "${!arg}"
# Try to load script ${!arg}.sh
# shellcheck source=/dev/null
. ./${!arg}.sh

# If it was not loaded successfully, exit with status 1.
if [ $? -ne 0 ]
then
printf "%sScript '${!arg}.sh' not loaded successfully. Exiting...%s\n" "${RED}" "${NORMAL}"
exit 1
fi
done
else
printf "No script loaded: %s != %s.\n" "$CURRENT_DIR" "$EXPECTED_DIR"
exit 1
fi
}

ALREADY_SOURCED__SOURCE_SCRIPT="true"
fi
118 changes: 62 additions & 56 deletions scripts/asserts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,82 @@
# if not installed, it asks if the user wants to install it. #
#################################################################################

assert_installed()
{
export ALREADY_SOURCED_ASSERTS

# Check if the user wants to exit or return from this function.
EXIT_IF_NOT_INSTALLED=1 # false
if [ -z "${ALREADY_SOURCED_ASSERTS}" ]
then
assert_installed()
{

if [ "$#" -ge "1" ]
then
# In the case the first parameter is "contaminated", we just use the default value.
if [ "$1" = "0" ]
# Check if the user wants to exit or return from this function.
EXIT_IF_NOT_INSTALLED=1 # false

if [ "$#" -ge "1" ]
then
EXIT_IF_NOT_INSTALLED=0 # true
# In the case the first parameter is "contaminated", we just use the default value.
if [ "$1" = "0" ]
then
EXIT_IF_NOT_INSTALLED=0 # true
fi
fi
fi

# Iterate through the arguments starting from the second,
# since the first represents the intent of the caller
# to decide if to exit or return from this function.
for (( arg = 2; arg <= $#; arg++ ))
do
command -v ${!arg}
if [ $? != 0 ]
then
printf "${RED}'${!arg}' not installed. Install it first before proceeding. Exiting...${NORMAL}\n"
# Based on: http://stackoverflow.com/questions/9640660/any-way-to-exit-bash-script-but-not-quitting-the-terminal
if [ "$EXIT_IF_NOT_INSTALLED" = "0" ]
# Iterate through the arguments starting from the second,
# since the first represents the intent of the caller
# to decide if to exit or return from this function.
for (( arg = 2; arg <= $#; arg++ ))
do
command -v ${!arg}
if [ $? != 0 ]
then
exit 1
else
return 1 # not ok
printf "%s'${!arg}' not installed. Install it first before proceeding. Exiting...%s\n" "${RED}" "${NORMAL}"
# Based on: http://stackoverflow.com/questions/9640660/any-way-to-exit-bash-script-but-not-quitting-the-terminal
if [ "$EXIT_IF_NOT_INSTALLED" = "0" ]
then
exit 1
else
return 1
fi
fi
done

if [ "$EXIT_IF_NOT_INSTALLED" = "1" ]
then
return 0
fi
done
}

if [ "$EXIT_IF_NOT_INSTALLED" = "1" ]
then
return 0 # ok
fi
}
assert_python_module_installed()
{
if [ "$#" -ne "1" ]
then
printf "%sCall this function with only one parameter, which represents the python module.%s\n" "${RED}" "${NORMAL}"
return 1
fi

assert_python_module_installed()
{
if [ "$#" -ne "1" ]
then
printf "${RED}Call this function with only one parameter, which represents the python module.${NORMAL}\n"
return 1
fi
command -v "$1"

command -v "$1"
if [ $? != 0 ]
then
# Based on: http://stackoverflow.com/a/27875395/3924118

if [ $? != 0 ]
then
# Based on: http://stackoverflow.com/a/27875395/3924118
printf "%sCommand '$1' not found.\n" "${RED}"
printf "Do you want me to install '$1' using 'pip3.5' (y/n)?%s " "${NORMAL}"
read -r ANSWER

printf "${RED}Command '$1' not found.\n"
printf "Do you want me to install '$1' using 'pip3.5' (y/n)?${NORMAL} "
read ANSWER
if echo "$ANSWER" | grep -iq "^y"
then
pip3.5 install "$1"
printf "%sDone.%s\n" "${GREEN}" "${NORMAL}"
else
printf "%sCannot proceed. Exiting...%s\n" "${RED}" "${NORMAL}"
exit 1
fi

if echo "$ANSWER" | grep -iq "^y"
then
pip3.5 install "$1"
printf "${GREEN}Done.${NORMAL}\n"
else
printf "${RED}Cannot proceed. Exiting...${NORMAL}\n"
exit 1
fi
}

fi
}
. ./_source_script.sh
_source_script scripts colors

. ./_source_script.sh
_source_script scripts colors
_source_script scripts clean_environment
ALREADY_SOURCED_ASSERTS="true"
fi
29 changes: 18 additions & 11 deletions scripts/cd_back_n_times.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
# corresponding to the value of the first parameter #
#################################################################################

cd_back_n_times()
{
if [ "$#" -ne "1" ]
then
return 1
fi
export ALREADY_SOURCED_CD_BACK_N_TIMES

for (( i = 0; i < $1; i++ ))
do
cd ..
done
}
if [ -z "${ALREADY_SOURCED_CD_BACK_N_TIMES}" ]
then
cd_back_n_times()
{
if [ "$#" -ne "1" ]
then
return 1
fi

for (( i = 0; i < $1; i++ ))
do
cd ..
done
}

ALREADY_SOURCED_CD_BACK_N_TIMES="true"
fi
31 changes: 19 additions & 12 deletions scripts/clean_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
# CALL FROM COMMAND LINE: . ./clean_environment.sh && clean_environment #
##############################################################################################

clean_environment()
{
printf "${YELLOW}Cleaning developing environment at '${PWD}'...${NORMAL}\n"
find . -type f -name "*.py[co]" -delete
find . -type d -name "__pycache__" -delete
find . -type f -name ".coverage" -delete
rm -rf ands.egg-info
rm -rf venv
printf "${GREEN}Done.${NORMAL}\n"
}
export ALREADY_SOURCED_CLEAN_ENVIRONMENT

. ./_source_script.sh
_source_script scripts colors
if [ -z "${ALREADY_SOURCED_CLEAN_ENVIRONMENT}" ]
then
clean_environment()
{
printf "%sCleaning developing environment at '%s'...%s" "${YELLOW}" "${PWD}" "${NORMAL}"
find . -type f -name "*.py[co]" -delete
find . -type d -name "__pycache__" -delete
find . -type f -name ".coverage" -delete
rm -rf ands.egg-info
rm -rf venv
printf "%s done.%s\n" "${GREEN}" "${NORMAL}"
}

. ./_source_script.sh
_source_script scripts colors

ALREADY_SOURCED_CLEAN_ENVIRONMENT="true"
fi
24 changes: 19 additions & 5 deletions scripts/colors.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#!/usr/bin/env bash

# Colors used when printing.
export GREEN=$(tput setaf 2)
export RED=$(tput setaf 1)
export NORMAL=$(tput sgr0)
export YELLOW=$(tput setaf 3)
export ALREADY_SOURCED_COLORS

if [ -z "${ALREADY_SOURCED_COLORS}" ]
then
# Colors used when printing.
export NORMAL
NORMAL=$(tput sgr0)

export RED
RED=$(tput setaf 1)

export GREEN
GREEN=$(tput setaf 2)

export YELLOW
YELLOW=$(tput setaf 3)

ALREADY_SOURCED_COLORS="true"
fi
29 changes: 18 additions & 11 deletions scripts/count_occurrences.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
# in the second parameter, which should be a string #
#################################################################################

count_occurrences()
{
# First parameter should be the character.
# Second should be the string.
export ALREADY_SOURCED_COUNT_OCCURRENCES

# Based on: http://stackoverflow.com/a/16679459/3924118.
if [ "$#" -ne "2" ]
then
echo -1
fi
if [ -z "${ALREADY_SOURCED_COUNT_OCCURRENCES}" ]
then
count_occurrences()
{
# First parameter should be the character.
# Second should be the string.

echo "$(grep -o "$1" <<< "$2" | wc -l)"
}
# Based on: http://stackoverflow.com/a/16679459/3924118.
if [ "$#" -ne "2" ]
then
echo -1
fi

echo "$(grep -o "$1" <<< "$2" | wc -l)"
}

ALREADY_SOURCED_COUNT_OCCURRENCES="true"
fi
Loading

0 comments on commit 31c4aa9

Please sign in to comment.