From f4db5690511d046c44dd1ab79df5b06fda6dd5bc Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Sun, 21 Apr 2024 13:23:21 +1000 Subject: [PATCH 1/5] Use arg/env variable for venv creation instead of hardcoded location Explicitly call the venv version of pip3 to install the wheel, so it is obvious the built prez module is being installed in the venv site_packages. --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index c3a97fe9..14ed16d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,8 +28,8 @@ WORKDIR /app COPY . . RUN poetry build -RUN python -m venv --system-site-packages /opt/venv -RUN pip install --no-cache-dir dist/*.whl +RUN python3 -m venv --system-site-packages ${VIRTUAL_ENV} +RUN ${VIRTUAL_ENV}/bin/pip3 install --no-cache-dir dist/*.whl # # Final From 70fcb45da9eeb9db0d90157d6c910a5a2bb0a7d7 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Sun, 21 Apr 2024 13:24:04 +1000 Subject: [PATCH 2/5] Don't copy the whole source tree into the final image, it already has the built prez module and all dependencies installed in the venv, it just needs main.py and supporting files. --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 14ed16d0..a34125ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,6 +50,7 @@ RUN apk update && \ bash WORKDIR /app -COPY . . +# prez module is already built as a package and installed in $VIRTUAL_ENV as a library +COPY main.py pyproject.toml ./ ENTRYPOINT uvicorn prez.app:app --host=${HOST:-0.0.0.0} --port=${PORT:-8000} --proxy-headers \ No newline at end of file From e314dca341c949a1f0faf363cbf9be691eadf0b0 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Sun, 21 Apr 2024 13:25:16 +1000 Subject: [PATCH 3/5] never include dist files in the docker env, this also ensures no dist files are included inside the sdist or wheel packages (poetry excludes packaging everything in gitignore). --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 331dca0c..9ef9d55c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ __pycache__/ .idea/ .pytest_cache/ .env* +dist/ !.env-template rdf/ From e026403259d5d5f8324793c8e7c4b3a435bfcc12 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Sun, 21 Apr 2024 13:26:05 +1000 Subject: [PATCH 4/5] Don't put Dockerfile inside its own Docker build context. Its not needed for the container built, and this allows incremental builds to work properly. --- .dockerignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.dockerignore b/.dockerignore index 8c7d01e3..bb381ce4 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,3 +8,5 @@ venv/ .git/ test_*.py .github/ +Dockerfile +*.Dockerfile From 5128d684f19bf59690aa542ac056e6c6eda94cd1 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Sun, 21 Apr 2024 13:29:29 +1000 Subject: [PATCH 5/5] Put prez module's pyproject.toml _inside_ the built prez module in the wheel package, this allows the config builder to find it when prez is installed as a system module. Modify the config builder to search inside the prez module for pyproject.toml if there is not one in the project top directory (enables the above functionality). Also explicitly list all the files to include in a sdist, because should be different than the wheel bdist. --- prez/config.py | 21 ++++++++++++++++++--- pyproject.toml | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/prez/config.py b/prez/config.py index db02415c..88320463 100644 --- a/prez/config.py +++ b/prez/config.py @@ -67,9 +67,24 @@ def get_version(cls, values): values["prez_version"] = version if version is None or version == "": - values["prez_version"] = toml.load( - Path(Path(__file__).parent.parent) / "pyproject.toml" - )["tool"]["poetry"]["version"] + possible_locations = ( + # dir above /prez, this is present in dev environments + # this is also used by derived projects to override the app version + Path(__file__).parent.parent, + # _inside_ /prez module, this is present in wheel builds + Path(__file__).parent, + ) + p: Path + for p in possible_locations: + if (p / "pyproject.toml").exists(): + values["prez_version"] = toml.load(p / "pyproject.toml")["tool"][ + "poetry" + ]["version"] + break + else: + raise RuntimeError( + "PREZ_VERSION not set, and cannot find a pyproject.toml to extract the version." + ) return values diff --git a/pyproject.toml b/pyproject.toml index 91363a86..0d1d52ad 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,20 @@ name = "prez" version = "0.1.0.dev0" description = "A python application for displaying linked data on the web" authors = ["Jamie Feiss ", "Nicholas Car ", "David Habgood "] +packages = [ + { include = "prez" }, + { include = "pyproject.toml", format = "wheel", to="prez" }, +] +include = [ + { path = "./*.md", format = "sdist" }, + { path = "LICENSE", format = "sdist" }, + { path = "demo", format = "sdist" }, + { path = "dev", format = "sdist" }, + { path = "tests", format = "sdist" }, + { path = "poetry.lock", format = "sdist" }, + { path = "./*.whl", format = "sdist" }, + { path = "*.toml", format = "sdist" } +] [tool.poetry.dependencies] python = "^3.11"