Skip to content

Commit

Permalink
feat: Allow defining direct path to pyproject.toml (#525)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Decat <[email protected]>
  • Loading branch information
n8felton and pdecat authored Jan 17, 2024
1 parent dcb087d commit d33b722
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 7 deletions.
2 changes: 2 additions & 0 deletions examples/build-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Note that this example may create resources which cost money. Run `terraform des
| <a name="module_package_dir_without_pip_install"></a> [package\_dir\_without\_pip\_install](#module\_package\_dir\_without\_pip\_install) | ../../ | n/a |
| <a name="module_package_file"></a> [package\_file](#module\_package\_file) | ../../ | n/a |
| <a name="module_package_file_with_pip_requirements"></a> [package\_file\_with\_pip\_requirements](#module\_package\_file\_with\_pip\_requirements) | ../../ | n/a |
| <a name="module_package_src_poetry"></a> [package\_src\_poetry](#module\_package\_src\_poetry) | ../../ | n/a |
| <a name="module_package_src_poetry2"></a> [package\_src\_poetry2](#module\_package\_src\_poetry2) | ../../ | n/a |
| <a name="module_package_with_commands_and_patterns"></a> [package\_with\_commands\_and\_patterns](#module\_package\_with\_commands\_and\_patterns) | ../../ | n/a |
| <a name="module_package_with_docker"></a> [package\_with\_docker](#module\_package\_with\_docker) | ../../ | n/a |
| <a name="module_package_with_npm_requirements_in_docker"></a> [package\_with\_npm\_requirements\_in\_docker](#module\_package\_with\_npm\_requirements\_in\_docker) | ../../ | n/a |
Expand Down
39 changes: 39 additions & 0 deletions examples/build-package/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ module "package_dir_poetry" {
artifacts_dir = "${path.root}/builds/package_dir_poetry/"
}

# Create zip-archive of a src directory where "poetry export" & "pip install --no-deps" will also be executed (using docker)
module "package_src_poetry" {
source = "../../"

create_function = false

build_in_docker = true
runtime = "python3.9"
docker_image = "build-python3.9-poetry"
docker_file = "${path.module}/../fixtures/python3.9-app-src-poetry/docker/Dockerfile"

source_path = [
"${path.module}/../fixtures/python3.9-app-src-poetry/src",
{
path = "${path.module}/../fixtures/python3.9-app-src-poetry/pyproject.toml"
poetry_install = true
}
]
artifacts_dir = "${path.root}/builds/package_src_poetry/"
}

# Create zip-archive of a src directory where "poetry export" & "pip install --no-deps" will also be executed (using docker)
module "package_src_poetry2" {
source = "../../"

create_function = false

build_in_docker = true
runtime = "python3.9"
docker_image = "build-python3.9-poetry"
docker_file = "${path.module}/../fixtures/python3.9-app-src-poetry/docker/Dockerfile"

source_path = [
"${path.module}/../fixtures/python3.9-app-src-poetry/src",
"${path.module}/../fixtures/python3.9-app-src-poetry/pyproject.toml"
]
artifacts_dir = "${path.root}/builds/package_src_poetry2/"
}

# 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 = "../../"
Expand Down
Empty file.
31 changes: 31 additions & 0 deletions examples/fixtures/python3.9-app-src-poetry/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/fixtures/python3.9-app-src-poetry/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "python3-9-app-src-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
readme = "README.md"
packages = [{include = "python39_app_src_poetry", from = "src"}]

[tool.poetry.dependencies]
python = "^3.9"
colorful = "^0.5.5"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Empty file.
Empty file.
18 changes: 11 additions & 7 deletions package.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,11 @@ def poetry_install_step(path, prefix=None, required=False):
else:
step("poetry", runtime, path, prefix)
hash(pyproject_file)
poetry_lock_file = os.path.join(path, "poetry.lock")
pyproject_path = os.path.dirname(pyproject_file)
poetry_lock_file = os.path.join(pyproject_path, "poetry.lock")
if os.path.isfile(poetry_lock_file):
hash(poetry_lock_file)
poetry_toml_file = os.path.join(path, "poetry.toml")
poetry_toml_file = os.path.join(pyproject_path, "poetry.toml")
if os.path.isfile(poetry_toml_file):
hash(poetry_toml_file)

Expand Down Expand Up @@ -1029,14 +1030,17 @@ def install_poetry_dependencies(query, path):
# 1. Emit files instead of temp_dir

# pyproject.toml is always required by poetry
pyproject_file = os.path.join(path, "pyproject.toml")
pyproject_file = path
if os.path.isdir(path):
pyproject_file = os.path.join(path, "pyproject.toml")
if not os.path.exists(pyproject_file):
yield
return

# poetry.lock & poetry.toml are optional
poetry_lock_file = os.path.join(path, "poetry.lock")
poetry_toml_file = os.path.join(path, "poetry.toml")
pyproject_path = os.path.dirname(pyproject_file)
poetry_lock_file = os.path.join(pyproject_path, "poetry.lock")
poetry_toml_file = os.path.join(pyproject_path, "poetry.toml")

runtime = query.runtime
artifacts_dir = query.artifacts_dir
Expand Down Expand Up @@ -1085,13 +1089,13 @@ def copy_file_to_target(file, temp_dir):
pyproject_target_file = copy_file_to_target(pyproject_file, temp_dir)

if os.path.isfile(poetry_lock_file):
log.info("Using poetry lock file: %s", poetry_lock_file)
log.info("Using poetry.lock file: %s", poetry_lock_file)
poetry_lock_target_file = copy_file_to_target(poetry_lock_file, temp_dir)
else:
poetry_lock_target_file = None

if os.path.isfile(poetry_toml_file):
log.info("Using poetry configuration file: %s", poetry_lock_file)
log.info("Using poetry.toml configuration file: %s", poetry_toml_file)
poetry_toml_target_file = copy_file_to_target(poetry_toml_file, temp_dir)
else:
poetry_toml_target_file = None
Expand Down

0 comments on commit d33b722

Please sign in to comment.