https://pantsbuild.org/ logo
#general
Title
# general
c

cold-soccer-63228

05/23/2022, 4:38 PM
When writing a plugin, what's the best way to say "get me all Python targets in the codebase that end with `"_some_suffix.py"`"? I saw that
AllTargets
and
AllTargetsRequest
exist, and also saw that in the Python backend, there was
PythonSourceTarget
. Was wondering if there was a way to make a request to get all Python targets with some suffix
b

bitter-ability-32190

05/23/2022, 4:40 PM
PathGlobs
perhaps?
f

fast-nail-55400

05/23/2022, 4:40 PM
Just write a rule to produce a
MySuffixPythonTargets
type. It would take
AllPythonTargets
as a parameter.
c

cold-soccer-63228

05/23/2022, 4:40 PM
Would I pass it into the
AllTargetsRequest
? Something like the following with some other arguments?
Copy code
await Get(AllTargets, AllTargetsRequest(PathGlobs("*_some_suffxi.py"), ...)
f

fast-nail-55400

05/23/2022, 4:41 PM
Then filter
AllPythonTargets.first_party
to file targets with
.is_file_target
and with the desired suffix by checking the
tgt.address
attribute.
b

bitter-ability-32190

05/23/2022, 4:41 PM
Ah I missed the targets part of this.
PathGLobs
likely isn't your path
(pun intended)
😂 1
😛 1
h

hundreds-father-404

05/23/2022, 4:54 PM
is_file_target
That won't handle if you manually declared
PythonSourceTarget
What you want is something like: https://github.com/pantsbuild/pants/blob/3ef6f122e35230f52631368cdbb3468f798ade97/src/python/pants/backend/python/dependency_inference/module_mapper.py#L67-L82 Check
if target.has_field(PythonSourceField) and PurePath(target[PythonSourceField].file_path).name == "_some_suffix.py"
1
👍 1
and then please comment on https://github.com/pantsbuild/pants/issues/14720 with your use case: specifically, it is not enough for you to filter by the presence of fields. You need to support arbitrary logic based on inspection of the
Target
and its fields