Skip to content

Commit

Permalink
Unix: add uninstall.sh; update install.sh to create and add variables…
Browse files Browse the repository at this point in the history
… to m269 config file
  • Loading branch information
densnow committed Aug 24, 2023
1 parent ff53a18 commit cb01cad
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 40 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ you select and copy them from this page and paste them into the command line.
5. Run the installation script:
- Windows: `.\install.ps1`
- Unix: `./install.sh`
6. Once the script has finished, remove it:
- Windows: `rm install.ps1`
- Unix: `rm install.sh`
7. Close the terminal.
6. Close the terminal.

You will have the following files in your M269 folder:
- the installation script in `install.ps1` or `install.sh`
- the list of M269 software in `requirements.txt`
- the style changes to Jupyter notebooks in `custom.css`
- the code checker (see Section 4 below) in `allowed.py` and `m269.json`

We recommend you do _not_ remove these files, so that you can re-install
the software, if needed.

## 3 Usage

Expand Down Expand Up @@ -160,6 +166,19 @@ If you want to remove the M269 styling of notebooks:
2. Delete all lines from the comment marking the start of M269-23J styling up to the comment ending it.
3. Save the file and close the editor.

## 6 Re-installing

If you haven't removed the installation files from your M269 folder,
you can re-install the software after uninstalling it,
e.g. to work again on your M269 notebooks. Do as follows:

1. Open a PowerShell (Windows) or a terminal (Unix).
2. Go to your M269 folder by entering `cd <M269 folder path>`, e.g. `cd ~/OU/m269-23j`.
3. Run the installation script with `.\install.ps1 .` (Windows) or `./install.sh .` (Unix).
Note the extra space and period after the command, compared to step 5 in Section 2.
(During re-installation, ignore messages saying that a file can't be copied onto itself.)
4. Close the terminal.

## Licence

The code and text in this repository are
Expand Down
26 changes: 13 additions & 13 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ $VENV = "$HOME\venvs\$COURSE"
function is-m269-folder {
param($path)
if (-not (Test-Path $path -PathType Container)) {
$folder = $path
$msg="doesn't exist or isn't a folder"
}
elseif (-not ($path -match "[Mm]269-23[Jj]$")) {
$msg="must be named m269-23j or M269-23J"
}
else
{
return
else {
$folder = Convert-Path $path
if (-not ((Get-Item $folder).Name -match "[Mm]269-23[Jj]")) {
$msg="must be named m269-23j or M269-23J"
}
else
{
return
}
}
Write-Host "$path can't be your M269 folder: it $msg."; Write-Host $DOC
Write-Host "$folder can't be your M269 folder: it $msg."; Write-Host $DOC
exit
}

Expand All @@ -51,9 +55,8 @@ if ($args.Length -gt 1) {
mkdir $HOME\.jupyter\custom -ErrorAction SilentlyContinue
if (Test-Path $HOME\.jupyter\custom\custom.css) {
Add-Content -Path $HOME\.jupyter\custom\custom.css -Value (Get-Content custom.css)
Remove-Item custom.css
} else {
Move-Item -Path custom.css -Destination $HOME\.jupyter\custom
Copy-Item -Path custom.css -Destination $HOME\.jupyter\custom
}
} else {
is-m269-folder $args[0]
Expand Down Expand Up @@ -84,9 +87,6 @@ Write-Host "Downloading and installing Python packages... (this will take long)"
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
deactivate
if ($args.Length -eq 0) {
Remove-Item requirements.txt
}

Write-Host "Adding shortcut commands to the PowerShell config file..."

Expand All @@ -98,7 +98,7 @@ function m269-23j {
$VENV\Scripts\Activate.ps1
}
function nb {
Start-process -NoNewWindow jupyter -ArgumentList "notebook $ESC$FOLDER$ESC"
Start-process -NoNewWindow jupyter -ArgumentList "notebook"
}
function allowed {
param(
Expand Down
58 changes: 35 additions & 23 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CHECK="allowed.py m269.json"
FILES="$CSS $REQS $CHECK"
COURSE=m269-23j
VENV=~/venvs/$COURSE
CONFIG_VARS=("VENV" "COURSE")

# find out under which shell this script is running
parent_shell=$(ps -o command $PPID)
Expand Down Expand Up @@ -58,14 +59,18 @@ fi
is_m269_folder () {
if [[ ! -d $1 ]] # single bracket would split path string on spaces
then
folder=$1
msg="doesn't exist or isn't a folder"
elif [[ $(basename "$1") != [Mm]269-23[Jj] ]]
then
msg="must be named m269-23j or M269-23J"
else
return
folder=$(cd "$1"; pwd)
if [[ $(basename "$folder") != [Mm]269-23[Jj] ]]
then
msg="must be named m269-23j or M269-23J"
else
return
fi
fi
echo "$1 can't be your M269 folder: it $msg." ; echo $DOC
echo "$folder can't be your M269 folder: it $msg." ; echo $DOC
exit 1
}

Expand All @@ -90,9 +95,8 @@ then
if [ -f ~/.jupyter/custom/$CSS ]
then
cat $CSS >> ~/.jupyter/custom/$CSS
rm $CSS
else
mv $CSS ~/.jupyter/custom
cp -a $CSS ~/.jupyter/custom
fi
else
is_m269_folder "$1"
Expand All @@ -117,6 +121,8 @@ else
cp -a $CHECK "$FOLDER"
fi

CONFIG_VARS+=("FOLDER")

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

Expand All @@ -126,40 +132,46 @@ pip install --upgrade pip
pip install -r $REQS
pip install pytype==2023.4.27 # install pytype only for Unix
deactivate
# if we're in the M269 folder, remove the no longer needed file
if [ $# -eq 0 ]
then
rm $REQS
fi
echo "Software has been installed."

echo "Adding shortcut commands to $shell's startup file..."

M269="cd \"$FOLDER\";source $VENV/bin/activate"
NB="jupyter notebook \"$FOLDER\"&"
NB="jupyter notebook &"
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

CONFIG_VARS+=("SHELL_CONFIG_FILE")

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

echo "All done. Go to $SITE for further instructions."
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

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

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 1 # false
;;
esac
}

# Confirm uninstallation
echo "Warning: After uninstallation, you will need a Jupyter environment to open and run your notebooks."
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
fi
CSS_FILE=~/.jupyter/custom/custom.css
COURSE_YEAR="${COURSE:5:2}"

# 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"
}
if confirm "Remove the $COURSE virtual environment?"; then
remove_venv || echo "Warning: failed to remove the virtual environment."
fi

# 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

# 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
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
cp "$CSS_FILE" "$CSS_FILE".backup
echo "Removing M269 custom styling from $CSS_FILE..."
sed -i "/$START_DELIM/,/$END_DELIM/d" "$CSS_FILE"
fi

fi

echo "The uninstallation process has now completed."

0 comments on commit cb01cad

Please sign in to comment.