feat: Working exercise implementation.

Small things may be missing, but this is the gist of it.
All runs in rootless container images (who think internally that they
are root).
main
Sofus Albert Høgsbro Rose 2023-08-24 13:48:06 +02:00
commit 6d72287d07
Signed by: so-rose
GPG Key ID: AD901CB0F3701434
10 changed files with 229 additions and 0 deletions

36
README.md 100644
View File

@ -0,0 +1,36 @@
# DTU Python Support Exercises
Implements the exercises at https://pythonsupport.dtu.dk/internal/exercises.html.
You'll need:
- `bash`: To run the `.sh` script.
- `podman`: To run the exercises in rootless containers.
- `python3`, `python3-pip`, `python3-future`: To pass Exercise 4 (too lazy to containerize `pip show`).
## Running the Exercises
Each exercise will run the corresponding `run.sh` in a rootless container, with `bash -x` (so you can see what they're doing exactly).
To run them all, run:
```
for exer in { exer_1_2 exer_2 exer_3 exer_4 exer_5 exer_6 exer_7 exer_8 }; do
./run.sh $exer
done
```
Each exercise will exit into a bash shell within the container, so you can play around.
**NOTE**: Exercise 3 should fail, as `numpy==1.23` is not compatible with Python 3.11.
**NOTE**: Exercise 4 will fail if system-level `pip` and `future` isn't installed.
## Reuse
These `run.sh` files can be used as templates for the described end-to-end installation procedure.
Note the MIT license.
## Running with Non-Debian Image
This may be useful for testing / as a baseline for testing installation procedures on any OS.
In `run.sh`, simply replace
```
IMAGE="docker.io/debian:bookworm-slim"
```
with the Docker image of the OS you wish to test.

2
exer_1/exer_1_2/.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
miniconda-prefix
install-miniconda.sh

View File

@ -0,0 +1,14 @@
#!/bin/bash
apt update
apt install wget -y
if [ ! -f /data/install-miniconda.sh ]; then
wget -O /data/install-miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x /data/install-miniconda.sh
fi
/data/install-miniconda.sh -b -f -p /data/miniconda-prefix
ln -s /data/miniconda-prefix/bin/conda /usr/local/bin/conda
bash

2
exer_2/.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
.venv-1
.venv-2

31
exer_2/run.sh 100755
View File

@ -0,0 +1,31 @@
#!/bin/bash
set -e ## Exit if Problems
set -u ## Fail on Undefined Variable
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
apt update
apt install python3 python3-venv python-is-python3 -y
cd "$SCRIPT_PATH"
if [ ! -d .venv-1 ]; then
python3 -m venv ".venv-1"
fi
if [ ! -d .venv-2 ]; then
python3 -m venv ".venv-2"
fi
. .venv-1/bin/activate
pip install numpy==1.24
pip install matplotlib==3.6
python3 -c "import numpy as np; print(np.array([1, 2, 3]))"
deactivate
. .venv-2/bin/activate
pip install numpy==1.23
pip install matplotlib==3.6
python3 -c "import numpy as np; print(np.array([1, 2, 3]))"
deactivate
cd -
bash

1
exer_3/.gitignore vendored 100644
View File

@ -0,0 +1 @@
.venv

21
exer_3/run.sh 100755
View File

@ -0,0 +1,21 @@
#!/bin/bash
set -e ## Exit if Problems
set -u ## Fail on Undefined Variable
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
apt update
apt install python3 python3-venv python-is-python3 git -y
cd "$SCRIPT_PATH"
if [ ! -d .venv ]; then
python3 -m venv .venv
fi
. .venv/bin/activate
pip install git+https://github.com/pyparsing/pyparsing.git@c8b76646bf7f883b40951a08365522d286af4963
pip freeze
deactivate
cd -
bash

1
exer_6/.gitignore vendored 100644
View File

@ -0,0 +1 @@
02002students

26
exer_6/run.sh 100755
View File

@ -0,0 +1,26 @@
#!/bin/bash
set -e ## Exit if Problems
set -u ## Fail on Undefined Variable
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
apt update
apt install python3 python3-venv python-is-python3 git -y
if [ ! -d "$SCRIPT_PATH/02002students" ]; then
git clone https://lab.compute.dtu.dk/cp/02002students.git
fi
cd "$SCRIPT_PATH/02002students"
if [ ! -d .venv ]; then
python3 -m venv .venv
fi
. .venv/bin/activate
pip install -r requirements.txt
python3 -c "import dtumathtools; dir(dtumathtools)"
pip check
deactivate
cd -
bash

95
run.sh 100755
View File

@ -0,0 +1,95 @@
#!/bin/bash
set -e ## Exit if Problems
set -u ## Fail on Undefined Variable
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
IMAGE="docker.io/debian:bookworm-slim"
####################
# - Actions
####################
action_exer_1_2() {
echo -e "Running Exercise 1.2...\n"
podman run \
--rm -it \
-v "$SCRIPT_PATH/exer_1/exer_1_2":/data \
-w /data \
"$IMAGE" \
bash -x run.sh
echo
echo
}
action_exer_2() {
echo -e "Running Exercise 2...\n"
podman run \
--rm -it \
-v "$SCRIPT_PATH/exer_2":/data \
-w /data \
"$IMAGE" \
bash -x run.sh
echo
echo
}
action_exer_3() {
echo -e "Running Exercise 3...\n"
podman run \
--rm -it \
-v "$SCRIPT_PATH/exer_3":/data \
-w /data \
"$IMAGE" \
bash -x run.sh
echo
echo
}
action_exer_4() {
echo -e "Running Exercise 4...\n"
pip show "${2-future}" | grep Location | cut -d ' ' -f 2
echo
echo
}
action_exer_5() {
echo -e "Running Exercise 5...\n"
echo "VSCodium: Activate the venv in the embedded terminal, and use that to run things. pip freeze for package list."
echo
echo
}
action_exer_6() {
echo -e "Running Exercise 6...\n"
echo "This does not use VSCode, as I do not consent to its proprietary licensing. See https://lab.compute.dtu.dk/cp/02002students/-/issues/1 ."
echo
echo
podman run \
--rm -it \
-v "$SCRIPT_PATH/exer_6":/data \
-w /data \
debian:bookworm-slim \
bash run.sh
}
action_exer_7() {
echo "'pip check' is run in exer_6."
}
action_exer_8() {
echo "At this level, it's identical (but slower than) venv. Just replace 'python3 -m venv' with 'virtualenv'. https://virtualenv.pypa.io/en/latest/"
}
####################
# - Check Dependencies
####################
case $1 in
exer_1_2)
action_exer_1_2
;;
exer_2)
action_exer_2
;;
exer_3)
action_exer_3
;;
exer_4)
action_exer_4
;;
exer_6)
action_exer_6
esac