Skip to content

Commit

Permalink
feat: Export poetry dependencies and install them with pip --no-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat committed Jun 17, 2022
1 parent 5e54af0 commit 7914fab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 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 install" will also be executed
# Create zip-archive of a single directory where "poetry export" & "pip install --no-deps" will also be executed
module "package_dir_poetry" {
source = "../../"

Expand Down
13 changes: 7 additions & 6 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ def install_poetry_dependencies(query, path):

working_dir = os.getcwd()

log.info("Installing python dependencies with poetry: %s", poetry_lock_file)
log.info("Installing python dependencies with poetry & pip: %s", poetry_lock_file)
with tempdir() as temp_dir:
def copy_file_to_target(file, temp_dir):
filename = os.path.basename(file)
Expand All @@ -1066,6 +1066,7 @@ def copy_file_to_target(file, temp_dir):
pyproject_target_file = copy_file_to_target(pyproject_file, temp_dir)

poetry_exec = "poetry"
python_exec = runtime
subproc_env = None

if not docker:
Expand All @@ -1075,12 +1076,13 @@ def copy_file_to_target(file, temp_dir):
# Install dependencies into the temporary directory.
with cd(temp_dir):
# NOTE: poetry must be available, which is the case with lambci/lambda:build-python* docker images
# FIXME: poetry install does not currently allow to specify the target directory so we extract
# installed packages from .venv/lib/python*/site-packages
# 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, "install", "--no-interaction" ]),
shlex_join([ poetry_exec, "export", "-f", "requirements.txt", "--output", "requirements.txt" ]),
shlex_join([ python_exec, '-m', 'pip', 'install', '--no-compile', '--no-deps', '--prefix=', '--target=.','--requirement=requirements.txt']),
]
if docker:
with_ssh_agent = docker.with_ssh_agent
Expand Down Expand Up @@ -1115,11 +1117,10 @@ def copy_file_to_target(file, temp_dir):
for poetry_command in poetry_commands:
check_call(poetry_command, env=subproc_env)

# FIXME: not really needed as only the content of a subdirectory is exposed
os.remove(poetry_lock_target_file)
os.remove(pyproject_target_file)

yield os.path.join(temp_dir, ".venv/lib/", runtime, "site-packages")
yield temp_dir


@contextmanager
Expand Down

0 comments on commit 7914fab

Please sign in to comment.