Skip to content

Commit

Permalink
Implement dep group attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
njlr committed Jan 6, 2025
1 parent b01f92d commit 5f749a9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/poetry/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pycross_poetry_lock_model(
name = "example_lock_model",
lock_file = "poetry.lock",
project_file = "pyproject.toml",
optional_groups = ["dev"],
)

pycross_lock_file(
Expand Down
10 changes: 10 additions & 0 deletions pycross/private/lock_attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ POETRY_IMPORT_ATTRS = dict(
allow_single_file = True,
mandatory = True,
),
default = attr.bool(
doc = "Whether to install dependencies from the default group.",
default = True,
),
optional_groups = attr.string_list(
doc = "List of optional dependency groups to install.",
),
all_optional_groups = attr.bool(
doc = "Install all optional dependencies.",
),
)

def handle_resolve_attrs(attrs, environment_files_and_labels, local_wheel_names_and_labels):
Expand Down
9 changes: 9 additions & 0 deletions pycross/private/poetry_lock_model.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def _pycross_poetry_lock_model_impl(ctx):
args.add("--lock-file", ctx.file.lock_file)
args.add("--output", out)

if ctx.attr.default:
args.add("--default")

for group in ctx.attr.optional_groups:
args.add_all(["--optional-group", group])

if ctx.attr.all_optional_groups:
args.add("--all-optional-groups")

ctx.actions.run(
inputs = (
ctx.files.project_file +
Expand Down
43 changes: 38 additions & 5 deletions pycross/private/tools/poetry_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ def get_files_for_package(
return result


def translate(project_file: Path, lock_file: Path) -> RawLockSet:
def translate(
project_file: Path,
lock_file: Path,
default_group: bool,
optional_groups: List[str],
all_optional_groups: bool,
) -> RawLockSet:
try:
with open(project_file, "rb") as f:
project_dict = tomli.load(f)
Expand All @@ -116,12 +122,14 @@ def translate(project_file: Path, lock_file: Path) -> RawLockSet:

dependency_items = []

dependency_items.extend((project_dict.get("tool", {}).get("poetry", {}).get("dependencies", {})).items())
if default_group:
dependency_items.extend((project_dict.get("tool", {}).get("poetry", {}).get("dependencies", {})).items())

groups = project_dict.get("tool", {}).get("poetry", {}).get("group", {})

for _, group in groups.items():
dependency_items.extend(group.get("dependencies", {}).items())
for group_name, group in groups.items():
if all_optional_groups or group_name in optional_groups:
dependency_items.extend(group.get("dependencies", {}).items())

for pin, pin_info in dependency_items:
pin = package_canonical_name(pin)
Expand Down Expand Up @@ -249,7 +257,13 @@ def parse_file_info(file_info) -> PackageFile:
def main(args: Any) -> None:
output = args.output

lock_set = translate(args.project_file, args.lock_file)
lock_set = translate(
project_file=args.project_file,
lock_file=args.lock_file,
default_group=args.default_group,
optional_groups=args.optional_group,
all_optional_groups=args.all_optional_groups,
)

with open(output, "w") as f:
f.write(lock_set.to_json(indent=2))
Expand All @@ -272,6 +286,25 @@ def parse_flags() -> Any:
help="The path to pdm.lock.",
)

parser.add_argument(
"--default-group",
action="store_true",
help="Whether to install dependencies from the default group.",
)

parser.add_argument(
"--optional-group",
action="append",
default=[],
help="Optional dependency groups to install.",
)

parser.add_argument(
"--all-optional-groups",
action="store_true",
help="Install all optional dependency groups.",
)

parser.add_argument(
"--output",
type=Path,
Expand Down

0 comments on commit 5f749a9

Please sign in to comment.