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

fix pyproject scripts #30

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ cython_debug/
#.idea/


poetry.lock
poetry.lock
Dockerfile
9 changes: 2 additions & 7 deletions poetry_dockerize_plugin/builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import os.path
import re
import sys
Expand Down Expand Up @@ -191,9 +190,7 @@ def generate_apt_packages_str(apt_packages: List[str]) -> str:

def generate_add_project_toml_str(config: ProjectConfiguration, real_context_path: str) -> str:
add_str = "RUN mkdir /app\n"
if os.path.exists(os.path.join(real_context_path, "poetry.lock")):
add_str += "COPY poetry.lock /app/poetry.lock\n"
add_str += "COPY pyproject.toml /app/pyproject.toml\n"
add_str += "COPY pyproject.toml poetry.lock* README* /app/\n"
for package in list(set(config.deps_packages)):
if os.path.exists(os.path.join(real_context_path, package)):
add_str += f"COPY ./{package} /app/{package}\n"
Expand All @@ -219,19 +216,17 @@ def generate_docker_file_content(config: ProjectConfiguration, real_context_path
FROM {config.base_image} as builder
RUN pip install poetry==1.7.1

ENV POETRY_NO_INTERACTION=1
ENV POETRY_VIRTUALENVS_IN_PROJECT=1
ENV POETRY_VIRTUALENVS_CREATE=1
ENV POETRY_CACHE_DIR=/tmp/poetry_cache
RUN poetry config virtualenvs.create false && poetry config virtualenvs.in-project false

{generate_apt_packages_str(config.build_apt_packages)}
{generate_add_project_toml_str(config, real_context_path)}

{generate_add_packages_str(config, real_context_path)}
{generate_extra_instructions_str(config.extra_build_instructions)}

RUN cd /app && poetry install --no-interaction --no-ansi --no-root
RUN cd /app && poetry install --no-interaction --no-ansi

FROM {config.base_image} as runtime
{generate_apt_packages_str(config.runtime_apt_packages)}
Expand Down
9 changes: 5 additions & 4 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def test() -> None:
clean_dockerfile()
build_image(path=test_project)
assert os.path.exists(os.path.join(test_project, "Dockerfile")) is False
import docker
docker_client = docker.from_env()
docker_client.containers.run("poetry-sample-app:0.1.0", detach=True, command="sample-cli")


def test_and_generate() -> None:
Expand Down Expand Up @@ -117,11 +120,9 @@ def test_parse() -> None:
FROM python:3.11-slim-buster as builder
RUN pip install poetry==1.7.1

ENV POETRY_NO_INTERACTION=1
ENV POETRY_VIRTUALENVS_IN_PROJECT=1
ENV POETRY_VIRTUALENVS_CREATE=1
ENV POETRY_CACHE_DIR=/tmp/poetry_cache
RUN poetry config virtualenvs.create false && poetry config virtualenvs.in-project false


ARG DEBIAN_FRONTEND=noninteractive
Expand All @@ -131,14 +132,14 @@ def test_parse() -> None:
&& apt-get -y dist-upgrade \
&& apt-get -y install git
RUN mkdir /app
COPY pyproject.toml /app/pyproject.toml
COPY pyproject.toml poetry.lock* README* /app/


COPY ./app /app/app

RUN poetry -V

RUN cd /app && poetry install --no-interaction --no-ansi --no-root
RUN cd /app && poetry install --no-interaction --no-ansi

FROM python:3.11-slim-buster as runtime

Expand Down
10 changes: 8 additions & 2 deletions tests/test_project/app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def home():
return "Hello, World!"


if __name__ == "__main__":
def main():
port = int(os.environ.get('PORT'))
app.run(debug=True, host='0.0.0.0', port=port)
app.run(debug=True, host='0.0.0.0', port=port)

def main_test():
print("Hello, World!")

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tests/test_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ description = ""
authors = ["Nicolò Boschi <[email protected]>"]
packages = [{include = "app"}]

[tool.poetry.scripts]
sample-cli = "app.__main__:main_test"

[tool.poetry.dependencies]
python = "^3.11"
flask = "*"
Expand Down
Loading