forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_targets.py
114 lines (96 loc) · 3.91 KB
/
list_targets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from typing import Dict, cast
from pants.engine.addresses import Address, Addresses
from pants.engine.console import Console
from pants.engine.goal import Goal, GoalSubsystem, LineOriented
from pants.engine.rules import collect_rules, goal_rule
from pants.engine.selectors import Get
from pants.engine.target import DescriptionField, ProvidesField, Targets
class ListSubsystem(LineOriented, GoalSubsystem):
"""Lists all targets matching the file or target arguments."""
name = "list"
@classmethod
def register_options(cls, register):
super().register_options(register)
register(
"--provides",
type=bool,
default=False,
help=(
"List only targets that provide an artifact, displaying the columns specified by "
"--provides-columns."
),
)
register(
"--provides-columns",
default="address,artifact_id",
help=(
"Display these columns when --provides is specified. Available columns are: "
"address, artifact_id, repo_name, repo_url, push_db_basedir"
),
removal_version="2.0.1.dev0",
removal_hint=(
"The option `--provides-columns` no longer does anything. It was specific to the "
"JVM backend, so no longer makes sense with Pants 2.0 initially only supporting "
"Python."
),
)
register(
"--documented",
type=bool,
default=False,
help="Print only targets that are documented with a description.",
)
@property
def provides(self) -> bool:
return cast(bool, self.options.provides)
@property
def documented(self) -> bool:
return cast(bool, self.options.documented)
class List(Goal):
subsystem_cls = ListSubsystem
@goal_rule
async def list_targets(
addresses: Addresses, list_subsystem: ListSubsystem, console: Console
) -> List:
if not addresses:
console.print_stderr(f"WARNING: No targets were matched in goal `{list_subsystem.name}`.")
return List(exit_code=0)
if list_subsystem.provides and list_subsystem.documented:
raise ValueError(
"Cannot specify both `--list-documented` and `--list-provides` at the same time. "
"Please choose one."
)
if list_subsystem.provides:
targets = await Get(Targets, Addresses, addresses)
addresses_with_provide_artifacts = {
tgt.address: tgt[ProvidesField].value
for tgt in targets
if tgt.get(ProvidesField).value is not None
}
with list_subsystem.line_oriented(console) as print_stdout:
for address, artifact in addresses_with_provide_artifacts.items():
print_stdout(f"{address.spec} {artifact}")
return List(exit_code=0)
if list_subsystem.documented:
targets = await Get(Targets, Addresses, addresses)
addresses_with_descriptions = cast(
Dict[Address, str],
{
tgt.address: tgt[DescriptionField].value
for tgt in targets
if tgt.get(DescriptionField).value is not None
},
)
with list_subsystem.line_oriented(console) as print_stdout:
for address, description in addresses_with_descriptions.items():
formatted_description = "\n ".join(description.strip().split("\n"))
print_stdout(f"{address.spec}\n {formatted_description}")
return List(exit_code=0)
with list_subsystem.line_oriented(console) as print_stdout:
for address in sorted(addresses):
print_stdout(address.spec)
return List(exit_code=0)
def rules():
return collect_rules()