Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix: add uninstall.sh and update install.sh to modify it during install #24

Closed
wants to merge 7 commits into from
55 changes: 42 additions & 13 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ DOC="See $SITE for details."
CSS=custom.css
REQS=requirements.txt
CHECK="allowed.py m269.json"
FILES="$CSS $REQS $CHECK"
UNINSTALL=uninstall.sh
FILES="$CSS $REQS $CHECK $UNINSTALL"
COURSE=m269-23j
VENV=~/venvs/$COURSE

Expand Down Expand Up @@ -87,7 +88,13 @@ 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
if [ $? -ne 0 ]
then
echo "Failed to download $file"
exit 1
fi
done
mkdir -p ~/.jupyter/custom
# don't overwrite existing CSS file
Expand Down Expand Up @@ -117,7 +124,12 @@ else
else
cp -a $CSS ~/.jupyter/custom
fi
cp -a $CHECK "$FOLDER"
cp -a $CHECK $UNINSTALL "$FOLDER"
if [ $? -ne 0 ]
then
echo "Failed to copy $CHECK and $UNINSTALL"
exit 1
fi
fi

echo "Creating Python environment $VENV... (this will take a bit)"
Expand All @@ -139,25 +151,42 @@ ALLOWED="python3.10 \"$FOLDER/allowed.py\" -c \"$FOLDER/m269.json\""

if [ $shell = "fish" ]
then
FILE=~/.config/fish/config.fish
SHELL_CONFIG_FILE=~/.config/fish/config.fish
else
FILE=~/.${shell}rc
SHELL_CONFIG_FILE=~/.${shell}rc
fi

if [ $shell = "csh" ] || [ $shell = "tcsh" ]
then
echo "alias $COURSE '$M269.csh'" >> $FILE
echo "alias nb '$NB'" >> $FILE
echo "alias allowed '$ALLOWED'" >> $FILE
echo "alias $COURSE '$M269.csh'" >> $SHELL_CONFIG_FILE
echo "alias nb '$NB'" >> $SHELL_CONFIG_FILE
echo "alias allowed '$ALLOWED'" >> $SHELL_CONFIG_FILE
else
if [ $shell = "fish" ]
then
echo "alias $COURSE='$M269.fish'" >> $FILE
echo "alias $COURSE='$M269.fish'" >> $SHELL_CONFIG_FILE
else
echo "alias $COURSE='$M269'" >> $FILE
echo "alias $COURSE='$M269'" >> $SHELL_CONFIG_FILE
fi
echo "alias nb='$NB'" >> $FILE
echo "alias allowed='$ALLOWED'" >> $FILE
echo "alias nb='$NB'" >> $SHELL_CONFIG_FILE
echo "alias allowed='$ALLOWED'" >> $SHELL_CONFIG_FILE
fi

# Set variables in uninstall.sh
if [[ -f "$FOLDER/$UNINSTALL" ]]; then
echo "Setting variables in uninstall.sh ..."
# Insert FOLDER variable at line 14
sed "14i\\
FOLDER=$FOLDER" "$FOLDER/$UNINSTALL" > "$FOLDER/$UNINSTALL.tmp"
mv "$FOLDER/$UNINSTALL.tmp" "$FOLDER/$UNINSTALL"
# Insert SHELL_CONFIG_FILE variable at line 15
sed "15i\\
SHELL_CONFIG_FILE=$SHELL_CONFIG_FILE" "$FOLDER/$UNINSTALL" > "$FOLDER/$UNINSTALL.tmp"
mv "$FOLDER/$UNINSTALL.tmp" "$FOLDER/$UNINSTALL"
chmod +x "$FOLDER/$UNINSTALL"
else
echo "Warning: $FOLDER/$UNINSTALL does not exist."
echo "Unable to set variables in uninstall.sh..."
fi

echo "All done. Go to $SITE for further instructions."
echo "All done. Go to $SITE for further instructions."
107 changes: 107 additions & 0 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/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
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
;;
*)
return 1
;;
esac
}

# Confirm uninstallation
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 0
fi

# Verify and remove the virtual environment.
remove_venv() {
test -n "$VENV" || return 1
test -d "$VENV" || return 1
# Check for existence of typical virtual environment files
test -f "$VENV/bin/activate" || return 1
test -f "$VENV/pyvenv.cfg" || return 1
echo "Removing the virtual environment..."
rm -r "$VENV"
}
remove_venv || { echo "Error: failed to remove the virtual environment."; exit 1; }

# Remove allowed.py and m269.json from M269 folder
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

ALIASES=("alias nb" "alias allowed" "alias $COURSE")
NB="jupyter notebook &"
COURSE_PATTERN="[Mm]269-$YEAR[Jj]"

# Remove aliases from the configuration file
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
# Pattern is: Start with $alias, and contains $COURSE_PATTERN or $NB in rest of line.
alias_pattern="/^$alias.*\(\($COURSE_PATTERN\)\|\($NB\)\)/d"
sed "$alias_pattern" "$SHELL_CONFIG_FILE" > "$SHELL_CONFIG_FILE.tmp"
mv "$SHELL_CONFIG_FILE.tmp" "$SHELL_CONFIG_FILE"
done
else
echo "Warning: $SHELL_CONFIG_FILE does not exist."
fi

# Optionally Remove custom M269 styling
if confirm "Remove M269 custom styling from $CSS_FILE ?"; then
START_DELIM="\/\* Start of $COURSE_PATTERN notebook styling. \*\/"
END_DELIM="\/\* End of $COURSE_PATTERN notebook styling. \*\/"
if [[ -f "$CSS_FILE" ]]; then
cp "$CSS_FILE" "$CSS_FILE".backup
echo "Removing M269 custom styling from $CSS_FILE..."
sed "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE" > "$CSS_FILE.tmp"
mv "$CSS_FILE.tmp" "$CSS_FILE"
else
echo "Warning: $CSS_FILE does not exist."
fi
fi

echo "All done. To reinstall please visit $SITE and follow the provided instructions."