From 97b00d309a5b8e8c16f9790658db1fc411c124f4 Mon Sep 17 00:00:00 2001 From: Patrick Decat Date: Mon, 13 Feb 2023 15:05:36 +0100 Subject: [PATCH] fix: Properly construct poetry commands when not using docker for building package (#420) --- examples/build-package/README.md | 1 + examples/build-package/main.tf | 19 +++++++- package.py | 83 +++++++++++++++----------------- 3 files changed, 58 insertions(+), 45 deletions(-) diff --git a/examples/build-package/README.md b/examples/build-package/README.md index ac20cd70..eadf0245 100644 --- a/examples/build-package/README.md +++ b/examples/build-package/README.md @@ -40,6 +40,7 @@ Note that this example may create resources which cost money. Run `terraform des | [package\_dir](#module\_package\_dir) | ../../ | n/a | | [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a | | [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a | +| [package\_dir\_poetry\_no\_docker](#module\_package\_dir\_poetry\_no\_docker) | ../../ | n/a | | [package\_dir\_with\_npm\_install](#module\_package\_dir\_with\_npm\_install) | ../../ | n/a | | [package\_dir\_without\_npm\_install](#module\_package\_dir\_without\_npm\_install) | ../../ | n/a | | [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a | diff --git a/examples/build-package/main.tf b/examples/build-package/main.tf index 5ce7f9df..92035861 100644 --- a/examples/build-package/main.tf +++ b/examples/build-package/main.tf @@ -45,7 +45,7 @@ module "package_dir_pip_dir" { artifacts_dir = "${path.root}/builds/package_dir_pip_dir/" } -# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed +# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed (using docker) module "package_dir_poetry" { source = "../../" @@ -65,6 +65,23 @@ module "package_dir_poetry" { artifacts_dir = "${path.root}/builds/package_dir_poetry/" } +# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed (not using docker) +module "package_dir_poetry_no_docker" { + source = "../../" + + create_function = false + + runtime = "python3.9" + + source_path = [ + { + path = "${path.module}/../fixtures/python3.9-app-poetry" + poetry_install = true + } + ] + artifacts_dir = "${path.root}/builds/package_dir_poetry/" +} + # Create zip-archive of a single directory without running "pip install" (which is default for python runtime) module "package_dir_without_pip_install" { source = "../../" diff --git a/package.py b/package.py index 48a3c4bd..9571caa5 100644 --- a/package.py +++ b/package.py @@ -1107,51 +1107,43 @@ def copy_file_to_target(file, temp_dir): # Install dependencies into the temporary directory. with cd(temp_dir): # NOTE: poetry must be available in the build environment, which is the case with lambci/lambda:build-python* docker images but not public.ecr.aws/sam/build-python* docker images - # FIXME: poetry install does not currently allow to specify the target directory so we export the + # FIXME: poetry install does not currently allow to specify the target directory so we export the # requirements then install them with "pip --no-deps" to avoid using pip dependency resolver poetry_commands = [ - shlex_join( - [ - poetry_exec, - "config", - "--no-interaction", - "virtualenvs.create", - "true", - ] - ), - shlex_join( - [ - poetry_exec, - "config", - "--no-interaction", - "virtualenvs.in-project", - "true", - ] - ), - shlex_join( - [ - poetry_exec, - "export", - "--format", - "requirements.txt", - "--output", - "requirements.txt", - "--with-credentials", - ] - ), - shlex_join( - [ - python_exec, - "-m", - "pip", - "install", - "--no-compile", - "--no-deps", - "--prefix=", - "--target=.", - "--requirement=requirements.txt", - ] - ), + [ + poetry_exec, + "config", + "--no-interaction", + "virtualenvs.create", + "true", + ], + [ + poetry_exec, + "config", + "--no-interaction", + "virtualenvs.in-project", + "true", + ], + [ + poetry_exec, + "export", + "--format", + "requirements.txt", + "--output", + "requirements.txt", + "--with-credentials", + ], + [ + python_exec, + "-m", + "pip", + "install", + "--no-compile", + "--no-deps", + "--prefix=", + "--target=.", + "--requirement=requirements.txt", + ], ] if docker: with_ssh_agent = docker.with_ssh_agent @@ -1167,7 +1159,10 @@ def copy_file_to_target(file, temp_dir): ) chown_mask = "{}:{}".format(os.getuid(), os.getgid()) - shell_commands = poetry_commands + [shlex_join(["chown", "-R", chown_mask, "."])] + poetry_commands += [["chown", "-R", chown_mask, "."]] + shell_commands = [ + shlex_join(poetry_command) for poetry_command in poetry_commands + ] shell_command = [" && ".join(shell_commands)] check_call( docker_run_command(