Skip to content

Commit

Permalink
fix: Properly construct poetry commands when not using docker for bui…
Browse files Browse the repository at this point in the history
…lding package (#420)
  • Loading branch information
pdecat authored Feb 13, 2023
1 parent eface07 commit 97b00d3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 45 deletions.
1 change: 1 addition & 0 deletions examples/build-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Note that this example may create resources which cost money. Run `terraform des
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |
| <a name="module_package_dir_poetry_no_docker"></a> [package\_dir\_poetry\_no\_docker](#module\_package\_dir\_poetry\_no\_docker) | ../../ | n/a |
| <a name="module_package_dir_with_npm_install"></a> [package\_dir\_with\_npm\_install](#module\_package\_dir\_with\_npm\_install) | ../../ | n/a |
| <a name="module_package_dir_without_npm_install"></a> [package\_dir\_without\_npm\_install](#module\_package\_dir\_without\_npm\_install) | ../../ | n/a |
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a |
Expand Down
19 changes: 18 additions & 1 deletion examples/build-package/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "../../"

Expand All @@ -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 = "../../"
Expand Down
83 changes: 39 additions & 44 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 97b00d3

Please sign in to comment.