Skip to content

Commit 3aa288f

Browse files
feat: Support passing extra args to poetry export (terraform-aws-modules#584)
1 parent acbd63c commit 3aa288f

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ source_path = [
459459
- `pip_requirements` - Controls whether to execute `pip install`. Set to `false` to disable this feature, `true` to run `pip install` with `requirements.txt` found in `path`. Or set to another filename which you want to use instead. When `source_path` is passed as a string containing a path (and not a list of maps), and `requirements.txt` is present, `pip install` is automatically executed.
460460
- `pip_tmp_dir` - Set the base directory to make the temporary directory for pip installs. Can be useful for Docker in Docker builds.
461461
- `poetry_install` - Controls whether to execute `poetry export` and `pip install`. Set to `false` to disable this feature, `true` to run `poetry export` with `pyproject.toml` and `poetry.lock` found in `path`. When `source_path` is passed as a string containing a path (and not a list of maps), and `pyproject.toml` with a build system `poetry` is present, `poetry export` and `pip install` are automatically executed.
462+
- `poetry_export_extra_args` - A list of additional poetry arguments to add to the poetry export command
462463
- `npm_requirements` - Controls whether to execute `npm install`. Set to `false` to disable this feature, `true` to run `npm install` with `package.json` found in `path`. Or set to another filename which you want to use instead.
463464
- `npm_tmp_dir` - Set the base directory to make the temporary directory for npm installs. Can be useful for Docker in Docker builds.
464465
- `prefix_in_zip` - If specified, will be used as a prefix inside zip-archive. By default, everything installs into the root of zip-archive.

package.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,9 @@ def pip_requirements_step(path, prefix=None, required=False, tmp_dir=None):
693693
step("pip", runtime, requirements, prefix, tmp_dir)
694694
hash(requirements)
695695

696-
def poetry_install_step(path, prefix=None, required=False):
696+
def poetry_install_step(
697+
path, poetry_export_extra_args=[], prefix=None, required=False
698+
):
697699
pyproject_file = path
698700
if os.path.isdir(path):
699701
pyproject_file = os.path.join(path, "pyproject.toml")
@@ -703,7 +705,7 @@ def poetry_install_step(path, prefix=None, required=False):
703705
"poetry configuration not found: {}".format(pyproject_file)
704706
)
705707
else:
706-
step("poetry", runtime, path, prefix)
708+
step("poetry", runtime, path, poetry_export_extra_args, prefix)
707709
hash(pyproject_file)
708710
pyproject_path = os.path.dirname(pyproject_file)
709711
poetry_lock_file = os.path.join(pyproject_path, "poetry.lock")
@@ -807,6 +809,7 @@ def commands_step(path, commands):
807809
prefix = claim.get("prefix_in_zip")
808810
pip_requirements = claim.get("pip_requirements")
809811
poetry_install = claim.get("poetry_install")
812+
poetry_export_extra_args = claim.get("poetry_export_extra_args", [])
810813
npm_requirements = claim.get("npm_package_json")
811814
runtime = claim.get("runtime", query.runtime)
812815

@@ -828,7 +831,12 @@ def commands_step(path, commands):
828831

829832
if poetry_install and runtime.startswith("python"):
830833
if path:
831-
poetry_install_step(path, prefix, required=True)
834+
poetry_install_step(
835+
path,
836+
prefix=prefix,
837+
poetry_export_extra_args=poetry_export_extra_args,
838+
required=True,
839+
)
832840

833841
if npm_requirements and runtime.startswith("nodejs"):
834842
if isinstance(npm_requirements, bool) and path:
@@ -898,8 +906,16 @@ def execute(self, build_plan, zip_stream, query):
898906
# XXX: timestamp=0 - what actually do with it?
899907
zs.write_dirs(rd, prefix=prefix, timestamp=0)
900908
elif cmd == "poetry":
901-
runtime, path, prefix = action[1:]
902-
with install_poetry_dependencies(query, path) as rd:
909+
(
910+
runtime,
911+
path,
912+
poetry_export_extra_args,
913+
prefix,
914+
) = action[1:]
915+
log.info("poetry_export_extra_args: %s", poetry_export_extra_args)
916+
with install_poetry_dependencies(
917+
query, path, poetry_export_extra_args
918+
) as rd:
903919
if rd:
904920
if pf:
905921
self._zip_write_with_filter(zs, pf, rd, prefix, timestamp=0)
@@ -1094,7 +1110,7 @@ def install_pip_requirements(query, requirements_file, tmp_dir):
10941110

10951111

10961112
@contextmanager
1097-
def install_poetry_dependencies(query, path):
1113+
def install_poetry_dependencies(query, path, poetry_export_extra_args):
10981114
# TODO:
10991115
# 1. Emit files instead of temp_dir
11001116

@@ -1183,6 +1199,17 @@ def copy_file_to_target(file, temp_dir):
11831199
# 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
11841200
# FIXME: poetry install does not currently allow to specify the target directory so we export the
11851201
# requirements then install them with "pip --no-deps" to avoid using pip dependency resolver
1202+
1203+
poetry_export = [
1204+
poetry_exec,
1205+
"export",
1206+
"--format",
1207+
"requirements.txt",
1208+
"--output",
1209+
"requirements.txt",
1210+
"--with-credentials",
1211+
] + poetry_export_extra_args
1212+
11861213
poetry_commands = [
11871214
[
11881215
poetry_exec,
@@ -1198,15 +1225,7 @@ def copy_file_to_target(file, temp_dir):
11981225
"virtualenvs.in-project",
11991226
"true",
12001227
],
1201-
[
1202-
poetry_exec,
1203-
"export",
1204-
"--format",
1205-
"requirements.txt",
1206-
"--output",
1207-
"requirements.txt",
1208-
"--with-credentials",
1209-
],
1228+
poetry_export,
12101229
[
12111230
python_exec,
12121231
"-m",

0 commit comments

Comments
 (0)