Skip to content

Commit

Permalink
Add --with-groups option to specify additional dependency groups on top
Browse files Browse the repository at this point in the history
of default/main to include in the blixbuild and validations
  • Loading branch information
spoorn committed Aug 4, 2022
1 parent ba20eaa commit 56ddcd8
Show file tree
Hide file tree
Showing 19 changed files with 568 additions and 29 deletions.
1 change: 1 addition & 0 deletions devtool
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set -euo pipefail
set -x

bootstrap() {
install_poetry
poetry install
local_plugin_add
}
Expand Down
16 changes: 15 additions & 1 deletion src/poeblix/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ def __init__(
data_files: Optional[List[Dict]] = None,
no_lock: bool = False,
only_lock: bool = False,
with_groups: List[str] = None,
) -> None:
super().__init__(poetry, executable=executable) # type: ignore
self._env = env
self._locker = locker
self._data_files = data_files
self._no_lock = no_lock
self._only_lock = only_lock
self._with_groups = with_groups

def _get_abs_path(self, rel_path: str) -> Path:
"""Transform a relative path to absolute path"""
Expand Down Expand Up @@ -90,7 +92,7 @@ def _write_metadata(self, wheel: zipfile.ZipFile) -> None:
# for package in locked_repository.packages:
# logger.info(f"Package {package.__dict__}")
logger.info("Resolving dependencies using poetry's solver to get rid of unneeded packages")
ops = util.resolve_dependencies(self._poetry, self._env, locked_repository)
ops = util.resolve_dependencies(self._poetry, self._env, locked_repository, self._with_groups)

# logger.info(f"dependency groups: {self._poetry.package._dependency_groups}")

Expand Down Expand Up @@ -170,13 +172,24 @@ class BlixBuildCommand(EnvCommand):
None,
"Uses lock dependencies only which are pinned to exact versions, instead of pyproject.toml",
),
option(
"with-groups",
None,
"Specify which dependency groups to use to build the wheel file, on top of required groups from "
"pyproject.toml. Can be specified multiple times or as a comma delimited list.",
flag=False,
multiple=True,
),
]

# Pick up Poetry's WheelBuilder logger
loggers = ["poetry.core.masonry.builders.wheel"]

def handle(self) -> None:
util.validate_options_mutually_exclusive(self.option, "no-lock", "only-lock")
with_groups = []
for group in self.option("with-groups"):
with_groups.extend(group.split(","))

package = self.poetry.package
self.line(f"Building <c1>{package.pretty_name}</c1> (<c2>{package.version}</c2>)")
Expand Down Expand Up @@ -210,6 +223,7 @@ def handle(self) -> None:
data_files=data_files,
no_lock=self.option("no-lock"),
only_lock=self.option("only-lock"),
with_groups=with_groups,
)
builder.build()

Expand Down
8 changes: 5 additions & 3 deletions src/poeblix/util/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence, Callable
from typing import Sequence, Callable, List

from cleo.io.null_io import NullIO

Expand All @@ -13,7 +13,9 @@
from poetry.utils.env import Env


def resolve_dependencies(poetry: "Poetry", env: Env, locked_repository: Repository) -> Sequence[Operation]:
def resolve_dependencies(
poetry: "Poetry", env: Env, locked_repository: Repository, with_groups: List[str] = None
) -> Sequence[Operation]:
"""
This uses poetry's solver to resolve dependencies and filters out packages from the lock file which are not
needed, such as packages that are not for our OS environment using markers (e.g. pywin32 is for Windows).
Expand All @@ -34,7 +36,7 @@ def resolve_dependencies(poetry: "Poetry", env: Env, locked_repository: Reposito
# See https://github.com/python-poetry/poetry/blob/master/src/poetry/installation/installer.py#L34 for poetry's
# usage of this
# TODO: Add support for adding more groups
groups = ["default", "main"]
groups = set(["default", "main"] + (with_groups if with_groups else []))

solver = Solver(
poetry.package.with_dependency_groups(groups=groups, only=True),
Expand Down
24 changes: 17 additions & 7 deletions src/poeblix/validatedocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ class ValidateDockerPlugin(EnvCommand):

name = "blixvalidatedocker"
description = (
"Validates a docker container contains dependencies as specified in pyproject.toml and poetry.lock. "
"By default, this validates in one direction, where dependencies specified in pyproject.toml/poetry.lock "
"should be present in the Docker container, but not the other way around. Note: "
"by default, the validation checks that the wheel's Requires Dist constraints are satisfied by "
"pyproject.toml/poetry.lock, not an exact 1:1 match."
"Validates a docker container contains dependencies that satisfies constraints in pyproject.toml and "
"poetry.lock. By default, this validates in one direction, where dependencies specified in "
"pyproject.toml/poetry.lock should be present in the Docker container, but not the other way around."
)

arguments = [argument("containerId", "Docker Container ID")]
Expand All @@ -34,7 +32,15 @@ class ValidateDockerPlugin(EnvCommand):
"no-lock",
None,
"Disables validating lock file dependencies.",
)
),
option(
"with-groups",
None,
"Specify which dependency groups to use to validate the wheel file, on top of required groups from "
"pyproject.toml. Can be specified multiple times or as a comma delimited list.",
flag=False,
multiple=True,
),
]

loggers = ["poetry.core.masonry.builders.wheel"]
Expand All @@ -58,9 +64,13 @@ def _validate_poetry_lock(self, docker_deps: dict):
self.line("Skipping poetry.lock validation as --no-lock was specified")
return

with_groups = []
for group in self.option("with-groups"):
with_groups.extend(group.split(","))

cid = self.argument("containerId")
locked_repo = self.poetry.locker.locked_repository()
ops = util.resolve_dependencies(self.poetry, self.env, locked_repo)
ops = util.resolve_dependencies(self.poetry, self.env, locked_repo, with_groups)
for op in ops:
dependency_package = op.package
name = dependency_package.pretty_name
Expand Down
22 changes: 16 additions & 6 deletions src/poeblix/validatewheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ class ValidateWheelPlugin(EnvCommand):

name = "blixvalidatewheel"
description = (
"Validates a wheel file contains Requires Dist as specified in pyproject.toml and poetry.lock "
"Validates a wheel file contains Requires Dist that satisfies constraints in pyproject.toml and poetry.lock "
"files in the project this command is ran. This by default validates in both directions, as in "
"it validates the wheel file's Requires Dist is specified in the project and vice versa. Note: "
"by default, the validation checks that the wheel's Requires Dist constraints are satisfied by "
"pyproject.toml/poetry.lock, not an exact 1:1 match."
"it validates the wheel file's Requires Dist is specified in the project and vice versa."
)

arguments = [argument("wheelPath", "Wheel file path")]
Expand All @@ -44,7 +42,15 @@ class ValidateWheelPlugin(EnvCommand):
"no-lock",
None,
"Disables validating lock file dependencies.",
)
),
option(
"with-groups",
None,
"Specify which dependency groups to use to validate the wheel file, on top of required groups from "
"pyproject.toml. Can be specified multiple times or as a comma delimited list.",
flag=False,
multiple=True,
),
]

loggers = ["poetry.core.masonry.builders.wheel"]
Expand Down Expand Up @@ -124,9 +130,13 @@ def _validate_poetry_lock(self, requires_dist: Dict[str, str], leftover_wheel_pa
self.line("Skipping poetry.lock validation as --no-lock was specified")
return

with_groups = []
for group in self.option("with-groups"):
with_groups.extend(group.split(","))

self.line("Validating against poetry.lock...")
locked_repo = self.poetry.locker.locked_repository()
ops = util.resolve_dependencies(self.poetry, self.env, locked_repo)
ops = util.resolve_dependencies(self.poetry, self.env, locked_repo, with_groups)
leftover_lock_packages = set([p.package.pretty_name for p in ops])
for op in ops:
dependency_package = op.package
Expand Down
Binary file not shown.
Loading

0 comments on commit 56ddcd8

Please sign in to comment.