commit 6d72287d070d2511e767f9f8911bbc7a8cf2c6fa Author: Sofus Albert Høgsbro Rose Date: Thu Aug 24 13:48:06 2023 +0200 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). diff --git a/README.md b/README.md new file mode 100644 index 0000000..d8d1ea0 --- /dev/null +++ b/README.md @@ -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. diff --git a/exer_1/exer_1_2/.gitignore b/exer_1/exer_1_2/.gitignore new file mode 100644 index 0000000..379356a --- /dev/null +++ b/exer_1/exer_1_2/.gitignore @@ -0,0 +1,2 @@ +miniconda-prefix +install-miniconda.sh diff --git a/exer_1/exer_1_2/run.sh b/exer_1/exer_1_2/run.sh new file mode 100755 index 0000000..060dbcf --- /dev/null +++ b/exer_1/exer_1_2/run.sh @@ -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 diff --git a/exer_2/.gitignore b/exer_2/.gitignore new file mode 100644 index 0000000..e1a9c30 --- /dev/null +++ b/exer_2/.gitignore @@ -0,0 +1,2 @@ +.venv-1 +.venv-2 diff --git a/exer_2/run.sh b/exer_2/run.sh new file mode 100755 index 0000000..08adfcd --- /dev/null +++ b/exer_2/run.sh @@ -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 diff --git a/exer_3/.gitignore b/exer_3/.gitignore new file mode 100644 index 0000000..1d17dae --- /dev/null +++ b/exer_3/.gitignore @@ -0,0 +1 @@ +.venv diff --git a/exer_3/run.sh b/exer_3/run.sh new file mode 100755 index 0000000..34b8207 --- /dev/null +++ b/exer_3/run.sh @@ -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 diff --git a/exer_6/.gitignore b/exer_6/.gitignore new file mode 100644 index 0000000..2c67845 --- /dev/null +++ b/exer_6/.gitignore @@ -0,0 +1 @@ +02002students diff --git a/exer_6/run.sh b/exer_6/run.sh new file mode 100755 index 0000000..5839e61 --- /dev/null +++ b/exer_6/run.sh @@ -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 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..cd8b024 --- /dev/null +++ b/run.sh @@ -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