Skip to content

Commit

Permalink
[Utils] Uninstall Script: Parsing comments from env
Browse files Browse the repository at this point in the history
* The script checks for comments with block start as `# Please`
* It echoes those comments after uncommenting
* This echoed output is grabbed and passed to ask and remove wrapper
which as per the user request proceeds.
* Changes also include a wrapper for `rm` and `rmdir` which checks for
files/ symbolic files/ directories

Signed-off-by: Shreyas Atre <[email protected]>
  • Loading branch information
SAtacker authored and hydai committed Sep 30, 2021
1 parent 258f8ed commit a075a24
Showing 1 changed file with 55 additions and 11 deletions.
66 changes: 55 additions & 11 deletions utils/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GREEN=$'\e[0;32m'
YELLOW=$'\e[0;33m'
NC=$'\e[0m' # No Color
PERM_ROOT=1
BLOCK_BEGIN="# Please"

if [[ $EUID -ne 0 ]]; then
echo "${YELLOW}No root permissions.${NC}"
Expand Down Expand Up @@ -78,6 +79,38 @@ usage() {
EOF
}

parse_env() {
local begin
begin=0
while IFS= read -r line; do
if [ ! "${line:0:1}" = "#" ]; then
continue
else
if [[ "$line" =~ $BLOCK_BEGIN ]]; then
begin=1
fi
fi
if [ $begin -eq 1 ] && [[ ! "$line" =~ $BLOCK_BEGIN ]]; then
echo "${line#"#"}"
fi
done <"$IPATH/env"
if [ -f "$IPATH/env" ]; then
echo "$IPATH/env"
fi
}

remove_parsed() {
if [ -f "$IPATH/env" ]; then
IFS=$'\n'
ask_remove $(parse_env)
ask_remove $(find "$IPATH" -type d -empty -print)
if [ -z "$(ls -A "$IPATH")" ] && [[ "$IPATH" =~ "wasmedge" ]]; then
ask_remove "$IPATH"
fi
exit 0
fi
}

detect_bin_path() {
set +e
_path=$(which "$1")
Expand All @@ -87,34 +120,45 @@ detect_bin_path() {
exit 1
else
echo "${GREEN}Installation path found at $IPATH${NC}"
remove_parsed
fi
else
IPATH=${_path%"/bin/$1"}
echo "${GREEN}Installation path found at $IPATH${NC}"
remove_parsed
fi
set -e
}

_rm() {
local var=$1
if [ -f "$var" ] || [ -h "$var" ]; then
rm -f "$var"
elif [ -d "$var" ]; then
rmdir --ignore-fail-on-non-empty "$var"
fi
}

ask_remove() {
local libs=("$@")
if [ $ASK == 1 ]; then
while true; do
echo "Do you wish to uninstall the following libs?"
echo "Do you wish to uninstall the following?"
for var in "${libs[@]}"; do
echo " $var"
echo "$var"
done
read -p "Please answer [Y/N | y/n]" yn
case $yn in
[Yy]*)
for var in "${libs[@]}"; do
echo "Removing $var"
rm -f "$var"
_rm "$var"
done
ldconfig
break
;;
[Nn]*)
echo "Aborted Uninstalling wasmedge_image_deps"
echo "Aborted Uninstalling"
break
;;
*) echo "Please answer [Y/N | y/n]" ;;
Expand All @@ -123,7 +167,7 @@ ask_remove() {
else
for var in "${libs[@]}"; do
echo "Removing $var"
rm -f "$var"
_rm "$var"
done
_ldconfig
fi
Expand Down Expand Up @@ -252,6 +296,11 @@ main() {
shift
done

if [ ! $VERBOSE == 0 ]; then
echo "Verbose Mode"
set -xv
fi

detect_bin_path wasmedge

if [ ! $default == 1 ] && [ ! $ASK == 0 ]; then
Expand All @@ -268,11 +317,6 @@ main() {
done
fi

if [ ! $VERBOSE == 0 ]; then
echo "Verbose Mode"
set -xv
fi

if [ -d "$IPATH" ]; then
echo "WasmEdge uninstallation from $IPATH"

Expand Down Expand Up @@ -304,7 +348,7 @@ main() {
wasmedgec-tensorflow \
wasmedge-tensorflow-lite
elif [ "$EXT" = "none" ]; then
echo "No extensions to be installed"
echo "No extensions to be uninstalled"
else
echo "Invalid extension"
fi
Expand Down

0 comments on commit a075a24

Please sign in to comment.