Skip to content

Commit 97b00d3

Browse files
authored
fix: Properly construct poetry commands when not using docker for building package (#420)
1 parent eface07 commit 97b00d3

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

examples/build-package/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Note that this example may create resources which cost money. Run `terraform des
4040
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
4141
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
4242
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |
43+
| <a name="module_package_dir_poetry_no_docker"></a> [package\_dir\_poetry\_no\_docker](#module\_package\_dir\_poetry\_no\_docker) | ../../ | n/a |
4344
| <a name="module_package_dir_with_npm_install"></a> [package\_dir\_with\_npm\_install](#module\_package\_dir\_with\_npm\_install) | ../../ | n/a |
4445
| <a name="module_package_dir_without_npm_install"></a> [package\_dir\_without\_npm\_install](#module\_package\_dir\_without\_npm\_install) | ../../ | n/a |
4546
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a |

examples/build-package/main.tf

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module "package_dir_pip_dir" {
4545
artifacts_dir = "${path.root}/builds/package_dir_pip_dir/"
4646
}
4747

48-
# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed
48+
# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed (using docker)
4949
module "package_dir_poetry" {
5050
source = "../../"
5151

@@ -65,6 +65,23 @@ module "package_dir_poetry" {
6565
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
6666
}
6767

68+
# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed (not using docker)
69+
module "package_dir_poetry_no_docker" {
70+
source = "../../"
71+
72+
create_function = false
73+
74+
runtime = "python3.9"
75+
76+
source_path = [
77+
{
78+
path = "${path.module}/../fixtures/python3.9-app-poetry"
79+
poetry_install = true
80+
}
81+
]
82+
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
83+
}
84+
6885
# Create zip-archive of a single directory without running "pip install" (which is default for python runtime)
6986
module "package_dir_without_pip_install" {
7087
source = "../../"

package.py

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,51 +1107,43 @@ def copy_file_to_target(file, temp_dir):
11071107
# Install dependencies into the temporary directory.
11081108
with cd(temp_dir):
11091109
# 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
1110-
# FIXME: poetry install does not currently allow to specify the target directory so we export the
1110+
# FIXME: poetry install does not currently allow to specify the target directory so we export the
11111111
# requirements then install them with "pip --no-deps" to avoid using pip dependency resolver
11121112
poetry_commands = [
1113-
shlex_join(
1114-
[
1115-
poetry_exec,
1116-
"config",
1117-
"--no-interaction",
1118-
"virtualenvs.create",
1119-
"true",
1120-
]
1121-
),
1122-
shlex_join(
1123-
[
1124-
poetry_exec,
1125-
"config",
1126-
"--no-interaction",
1127-
"virtualenvs.in-project",
1128-
"true",
1129-
]
1130-
),
1131-
shlex_join(
1132-
[
1133-
poetry_exec,
1134-
"export",
1135-
"--format",
1136-
"requirements.txt",
1137-
"--output",
1138-
"requirements.txt",
1139-
"--with-credentials",
1140-
]
1141-
),
1142-
shlex_join(
1143-
[
1144-
python_exec,
1145-
"-m",
1146-
"pip",
1147-
"install",
1148-
"--no-compile",
1149-
"--no-deps",
1150-
"--prefix=",
1151-
"--target=.",
1152-
"--requirement=requirements.txt",
1153-
]
1154-
),
1113+
[
1114+
poetry_exec,
1115+
"config",
1116+
"--no-interaction",
1117+
"virtualenvs.create",
1118+
"true",
1119+
],
1120+
[
1121+
poetry_exec,
1122+
"config",
1123+
"--no-interaction",
1124+
"virtualenvs.in-project",
1125+
"true",
1126+
],
1127+
[
1128+
poetry_exec,
1129+
"export",
1130+
"--format",
1131+
"requirements.txt",
1132+
"--output",
1133+
"requirements.txt",
1134+
"--with-credentials",
1135+
],
1136+
[
1137+
python_exec,
1138+
"-m",
1139+
"pip",
1140+
"install",
1141+
"--no-compile",
1142+
"--no-deps",
1143+
"--prefix=",
1144+
"--target=.",
1145+
"--requirement=requirements.txt",
1146+
],
11551147
]
11561148
if docker:
11571149
with_ssh_agent = docker.with_ssh_agent
@@ -1167,7 +1159,10 @@ def copy_file_to_target(file, temp_dir):
11671159
)
11681160

11691161
chown_mask = "{}:{}".format(os.getuid(), os.getgid())
1170-
shell_commands = poetry_commands + [shlex_join(["chown", "-R", chown_mask, "."])]
1162+
poetry_commands += [["chown", "-R", chown_mask, "."]]
1163+
shell_commands = [
1164+
shlex_join(poetry_command) for poetry_command in poetry_commands
1165+
]
11711166
shell_command = [" && ".join(shell_commands)]
11721167
check_call(
11731168
docker_run_command(

0 commit comments

Comments
 (0)