Skip to content

Commit

Permalink
drop --include-dep-rules option.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaos committed Feb 7, 2023
1 parent 9c91ba6 commit 60563c4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 68 deletions.
91 changes: 35 additions & 56 deletions src/python/pants/backend/project_info/peek.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
UnexpandedTargets,
)
from pants.option.option_types import BoolOption
from pants.util.strutil import softwrap


@runtime_checkable
Expand All @@ -53,16 +52,6 @@ class PeekSubsystem(Outputting, GoalSubsystem):
help="Whether to leave off values that match the target-defined default values.",
)

include_dep_rules = BoolOption(
default=False,
help=softwrap(
"""
Whether to include `_dependencies_rules`, `_dependents_rules` and `_effective_dep_rules`
that apply to the target and its dependencies.
"""
),
)


class Peek(Goal):
subsystem_cls = PeekSubsystem
Expand All @@ -86,7 +75,7 @@ class TargetData:
dependents_rules: tuple[str, ...] | None = None
effective_dep_rules: tuple[str, ...] | None = None

def to_dict(self, exclude_defaults: bool = False, include_dep_rules: bool = False) -> dict:
def to_dict(self, exclude_defaults: bool = False) -> dict:
nothing = object()
fields = {
(
Expand All @@ -100,10 +89,9 @@ def to_dict(self, exclude_defaults: bool = False, include_dep_rules: bool = Fals
if self.expanded_sources is not None:
fields["sources"] = self.expanded_sources

if include_dep_rules:
fields["_dependencies_rules"] = self.dependencies_rules
fields["_dependents_rules"] = self.dependents_rules
fields["_effective_dep_rules"] = self.effective_dep_rules
fields["_dependencies_rules"] = self.dependencies_rules
fields["_dependents_rules"] = self.dependents_rules
fields["_effective_dep_rules"] = self.effective_dep_rules

return {
"address": self.target.address.spec,
Expand All @@ -116,10 +104,8 @@ class TargetDatas(Collection[TargetData]):
pass


def render_json(
tds: Iterable[TargetData], exclude_defaults: bool = False, include_dep_rules: bool = False
) -> str:
return f"{json.dumps([td.to_dict(exclude_defaults, include_dep_rules) for td in tds], indent=2, cls=_PeekJsonEncoder)}\n"
def render_json(tds: Iterable[TargetData], exclude_defaults: bool = False) -> str:
return f"{json.dumps([td.to_dict(exclude_defaults) for td in tds], indent=2, cls=_PeekJsonEncoder)}\n"


class _PeekJsonEncoder(json.JSONEncoder):
Expand Down Expand Up @@ -193,42 +179,35 @@ async def get_target_data(
for tgt, hs in zip(targets_with_sources, hydrated_sources_per_target)
}

if not subsys.include_dep_rules:
dependencies_rules_map = {}
dependents_rules_map = {}
effective_dep_rules_map = {}
else:
family_adaptors = await _get_target_family_and_adaptor_for_dep_rules(
*(tgt.address for tgt in sorted_targets),
description_of_origin="`peek` goal",
)
dependencies_rules_map = {
tgt.address: describe_ruleset(
family.dependencies_rules.get_ruleset(tgt.address, adaptor)
)
for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors)
if family.dependencies_rules is not None
}
dependents_rules_map = {
tgt.address: describe_ruleset(family.dependents_rules.get_ruleset(tgt.address, adaptor))
for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors)
if family.dependents_rules is not None
}
all_effective_dep_rules = await MultiGet(
Get(
DependenciesRuleApplication,
DependenciesRuleApplicationRequest(
tgt.address,
Addresses(dep.address for dep in deps),
description_of_origin="`peek` goal",
),
)
for tgt, deps in zip(sorted_targets, dependencies_per_target)
family_adaptors = await _get_target_family_and_adaptor_for_dep_rules(
*(tgt.address for tgt in sorted_targets),
description_of_origin="`peek` goal",
)
dependencies_rules_map = {
tgt.address: describe_ruleset(family.dependencies_rules.get_ruleset(tgt.address, adaptor))
for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors)
if family.dependencies_rules is not None
}
dependents_rules_map = {
tgt.address: describe_ruleset(family.dependents_rules.get_ruleset(tgt.address, adaptor))
for tgt, (family, adaptor) in zip(sorted_targets, family_adaptors)
if family.dependents_rules is not None
}
all_effective_dep_rules = await MultiGet(
Get(
DependenciesRuleApplication,
DependenciesRuleApplicationRequest(
tgt.address,
Addresses(dep.address for dep in deps),
description_of_origin="`peek` goal",
),
)
effective_dep_rules_map = {
application.address: tuple(str(rule) for rule in application.dependencies_rule.values())
for application in all_effective_dep_rules
}
for tgt, deps in zip(sorted_targets, dependencies_per_target)
)
effective_dep_rules_map = {
application.address: tuple(str(rule) for rule in application.dependencies_rule.values())
for application in all_effective_dep_rules
}

return TargetDatas(
TargetData(
Expand All @@ -250,7 +229,7 @@ async def peek(
targets: UnexpandedTargets,
) -> Peek:
tds = await Get(TargetDatas, UnexpandedTargets, targets)
output = render_json(tds, subsys.exclude_defaults, subsys.include_dep_rules)
output = render_json(tds, subsys.exclude_defaults)
with subsys.output(console) as write_stdout:
write_stdout(output)
return Peek(exit_code=0)
Expand Down
38 changes: 26 additions & 12 deletions src/python/pants/backend/project_info/peek_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@


@pytest.mark.parametrize(
"expanded_target_infos, exclude_defaults, include_dep_rules, expected_output",
"expanded_target_infos, exclude_defaults, expected_output",
[
pytest.param(
[],
False,
False,
"[]\n",
id="null-case",
),
Expand All @@ -42,13 +41,15 @@
)
],
True,
False,
dedent(
"""\
[
{
"address": "example:files_target",
"target_type": "files",
"_dependencies_rules": null,
"_dependents_rules": null,
"_effective_dep_rules": null,
"dependencies": [],
"overrides": {
"('foo.txt',)": {
Expand All @@ -68,7 +69,7 @@
]
"""
),
id="single-files-target/exclude-defaults",
id="single-files-target/exclude-defaults-regression",
),
pytest.param(
[
Expand All @@ -81,13 +82,15 @@
)
],
False,
False,
dedent(
"""\
[
{
"address": "example:files_target",
"target_type": "files",
"_dependencies_rules": null,
"_dependents_rules": null,
"_effective_dep_rules": null,
"dependencies": [],
"description": null,
"overrides": null,
Expand Down Expand Up @@ -128,13 +131,15 @@
),
],
True,
False,
dedent(
"""\
[
{
"address": "example:files_target",
"target_type": "files",
"_dependencies_rules": null,
"_dependents_rules": null,
"_effective_dep_rules": null,
"dependencies": [],
"sources": [],
"sources_raw": [
Expand All @@ -147,6 +152,9 @@
{
"address": "example:archive_target",
"target_type": "archive",
"_dependencies_rules": null,
"_dependents_rules": null,
"_effective_dep_rules": null,
"dependencies": [
"foo/bar:baz",
"qux:quux"
Expand Down Expand Up @@ -176,7 +184,6 @@
),
],
True,
True,
dedent(
"""\
[
Expand Down Expand Up @@ -212,10 +219,8 @@
),
],
)
def test_render_targets_as_json(
expanded_target_infos, exclude_defaults, include_dep_rules, expected_output
):
actual_output = peek.render_json(expanded_target_infos, exclude_defaults, include_dep_rules)
def test_render_targets_as_json(expanded_target_infos, exclude_defaults, expected_output):
actual_output = peek.render_json(expanded_target_infos, exclude_defaults)
assert actual_output == expected_output


Expand Down Expand Up @@ -260,31 +265,40 @@ def test_get_target_data(rule_runner: RuleRunner) -> None:
GenericTarget({"dependencies": [":baz"]}, Address("foo", target_name="bar")),
None,
("foo/a.txt:baz", "foo/b.txt:baz"),
effective_dep_rules=(
"foo -> foo : ALLOW\ntarget foo:bar -> files foo/a.txt:baz",
"foo -> foo : ALLOW\ntarget foo:bar -> files foo/b.txt:baz",
),
),
TargetData(
FilesGeneratorTarget({"sources": ["*.txt"]}, Address("foo", target_name="baz")),
("foo/a.txt", "foo/b.txt"),
("foo/a.txt:baz", "foo/b.txt:baz"),
effective_dep_rules=(
"foo -> foo : ALLOW\nfiles foo:baz -> files foo/a.txt:baz",
"foo -> foo : ALLOW\nfiles foo:baz -> files foo/b.txt:baz",
),
),
TargetData(
FileTarget(
{"source": "a.txt"}, Address("foo", relative_file_path="a.txt", target_name="baz")
),
("foo/a.txt",),
(),
effective_dep_rules=(),
),
TargetData(
FileTarget(
{"source": "b.txt"}, Address("foo", relative_file_path="b.txt", target_name="baz")
),
("foo/b.txt",),
(),
effective_dep_rules=(),
),
]


def test_get_target_data_with_dep_rules(rule_runner: RuleRunner) -> None:
rule_runner.set_options(["--peek-include-dep-rules"])
rule_runner.write_files(
{
"foo/BUILD": dedent(
Expand Down

0 comments on commit 60563c4

Please sign in to comment.