Skip to content

Commit

Permalink
Update uninstall.sh and install.sh as per Michel's comments and sugge…
Browse files Browse the repository at this point in the history
…stions
  • Loading branch information
densnow committed Aug 24, 2023
1 parent 08310d3 commit 3a98edb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 59 deletions.
30 changes: 15 additions & 15 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CHECK="allowed.py m269.json"
FILES="$CSS $REQS $CHECK"
COURSE=m269-23j
VENV=~/venvs/$COURSE
CONFIG_VARS=("VENV" "COURSE")
UNINSTALL=uninstall.sh

# find out under which shell this script is running
parent_shell=$(ps -o command $PPID)
Expand Down Expand Up @@ -88,7 +88,8 @@ then
echo "Downloading and installing M269 files..."
for file in $FILES
do
curl -LO https://github.com/dsa-ou/m269-installer/raw/main/$file
# WARNING: CHANGE URL BACK TO MAIN BRANCH BEFORE MERGING!!!
curl -LO https://github.com/dsa-ou/m269-installer/raw/14-create-uninstallation-scripts/$file
done
mkdir -p ~/.jupyter/custom
# don't overwrite existing CSS file
Expand Down Expand Up @@ -118,11 +119,9 @@ else
else
cp -a $CSS ~/.jupyter/custom
fi
cp -a $CHECK "$FOLDER"
cp -a $CHECK $UNINSTALL "$FOLDER"
fi

CONFIG_VARS+=("FOLDER")

echo "Creating Python environment $VENV... (this will take a bit)"
python3.10 -m venv --prompt $COURSE $VENV

Expand All @@ -147,8 +146,6 @@ else
SHELL_CONFIG_FILE=~/.${shell}rc
fi

CONFIG_VARS+=("SHELL_CONFIG_FILE")

if [ $shell = "csh" ] || [ $shell = "tcsh" ]
then
echo "alias $COURSE '$M269.csh'" >> $SHELL_CONFIG_FILE
Expand All @@ -165,13 +162,16 @@ else
echo "alias allowed='$ALLOWED'" >> $SHELL_CONFIG_FILE
fi

M269_CONFIG_FILE=$FOLDER/.m269rc
CONFIG_VARS+=("M269_CONFIG_FILE")

# Write the name=value pairs to m269 config file
for var_name in "${CONFIG_VARS[@]}"; do
var_value="${!var_name}"
echo "$var_name=$var_value" >> "$M269_CONFIG_FILE"
done
# Set variables in uninstall.sh
if [[ -f "$FOLDER/$UNINSTALL" ]]
then
echo "Setting variables into uninstall.sh ..."
sed -i "14iFOLDER=$FOLDER" "$FOLDER/$UNINSTALL"
sed -i "15iSHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL"
chmod +x "$FOLDER/$UNINSTALL"
else
echo "Warning: $FOLDER/uninstall.sh does not exist."
echo "critical Variables have not been set in uninstall.sh."
fi

echo "All done. Go to $SITE for further instructions."
100 changes: 56 additions & 44 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
#!/bin/bash

# This script performs the uninstallation of the M269 course environment
# and will do the following:
# - Seeks user confirmation before proceeding.
# - Deletes the M269 virtual environment.
# - Erases course-specific aliases from the shell config file.
# - Removes files related to `allowed` from the M269 folder.
# - Optionally clears M269 styling from Jupyter's custom.css.

# Note: this script expects FOLDER and SHELL_CONFIG_FILE to be set during the
# installation process (inserted with sed via install.sh).

COURSE=m269-23j
VENV=~/venvs/$COURSE
CSS_FILE=~/.jupyter/custom/custom.css
COURSE_YEAR=23
SITE=https://dsa-ou.github.io/m269-installer

# Check FOLDER and SHELL_CONFIG_FILE have been set i.e inserted via install.sh
if [[ -z "$FOLDER" ]] || [[ -z "$SHELL_CONFIG_FILE" ]]; then
echo "Error: critical variables have not been set"
exit 1
fi

BOLD="\033[1m"
NORMAL="\033[0m"

confirm() {
local message="$1"
echo -en "${BOLD}:: $message [y/N] ${NORMAL}"
read -r response
case "$response" in
[yY])
return 0 # true
return 0
;;
*)
return 1 # false
return 1
;;
esac
}

# Confirm uninstallation
echo "Warning: After uninstallation, you will need a Jupyter environment to open and run your notebooks."
echo "Warning: This script will make the following changes: "
echo " - Remove the virtual environment in $VENV."
echo " - Remove the aliases 'nb', 'allowed' and '$COURSE' from $SHELL_CONFIG_FILE"
echo " - Optionally removes M269 custom styling from $CSS_FILE"
echo "As a result, Jupyter notebooks will no longer be readable or executable unless another Jupyter environment exists."
if ! confirm "Proceed with uninstallation?"; then
exit 1
fi

# Assume variables VENV, COURSE, FOLDER, SHELL_CONFIG_FILE,
# will be in the M269 config file
if [ -e .m269rc ]; then
source .m269rc
else
echo "Failed to source the M269 configuration file "
exit 1
exit 0
fi
CSS_FILE=~/.jupyter/custom/custom.css
COURSE_YEAR="${COURSE:5:2}"

# Remove the virtual environment
# Verify and remove the virtual environment.
remove_venv() {
test -n "$VENV" || return 1
test -d "$VENV" || return 1
Expand All @@ -44,47 +59,44 @@ remove_venv() {
echo "Removing the virtual environment..."
rm -r "$VENV"
}
if confirm "Remove the $COURSE virtual environment?"; then
remove_venv || echo "Warning: failed to remove the virtual environment."
fi
remove_venv || { echo "Error: failed to remove the virtual environment."; exit 1; }

# Remove allowed.py and m269.json from M269 folder
if confirm "Remove allowed.py and m269.json from $FOLDER ?"; then
for file in "allowed.py" "m269.json"; do
target="$FOLDER/$file"
if [ -e "$target" ]; then
echo "Removing $file from $FOLDER..."
rm "$target"
else
echo "Warning: $target does not exist."
fi
done
fi
for file in "allowed.py" "m269.json"; do
target="$FOLDER/$file"
if [[ -f "$target" ]]; then
echo "Removing $file from $FOLDER..."
rm "$target"
else
echo "Warning: $target does not exist."
fi
done

# Remove aliases from the configuration file
if confirm "Remove shortcut commands from $SHELL_CONFIG_FILE ?"; then
ALIASES=("alias nb" "alias allowed" "alias $COURSE")
if [ -e $SHELL_CONFIG_FILE ]; then
echo "Removing shortcut commands from $SHELL_CONFIG_FILE..."
cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup
for alias in "${ALIASES[@]}"; do
# Delete lines that start with $alias and contain current course "code"
sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE"
done
fi
ALIASES=("alias nb" "alias allowed" "alias $COURSE")
if [[ -f "$SHELL_CONFIG_FILE" ]]; then
echo "Removing shortcut commands from $SHELL_CONFIG_FILE..."
cp "$SHELL_CONFIG_FILE" "$SHELL_CONFIG_FILE".backup
for alias in "${ALIASES[@]}"; do
# Delete lines that start with $alias and contain current course "code"
sed -i "/^$alias.*[Mm]269-$COURSE_YEAR[Jj]/d" "$SHELL_CONFIG_FILE"
done
else
echo "Warning: $SHELL_CONFIG_FILE does not exist."
fi

# Remove lines from ~/.jupyter/custom/custom.css
if confirm "Remove M269 custom styling from $CSS_FILE ?"; then
# Special characters need to be escaped for use in regex
START_DELIM="\/\* Start of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/"
END_DELIM="\/\* End of [Mm]269-$COURSE_YEAR[Jj] notebook styling. \*\/"
if [ -e $CSS_FILE ]; then
if [[ -f "$CSS_FILE" ]]; then
cp "$CSS_FILE" "$CSS_FILE".backup
echo "Removing M269 custom styling from $CSS_FILE..."
sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE"
else
echo "Warning: $CSS_FILE does not exist."
fi

fi

echo "The uninstallation process has now completed."
echo "All done. To reinstall please visit $SITE and follow the provided instructions."

0 comments on commit 3a98edb

Please sign in to comment.