When writing a plugin, what's the best way to say ...
# general
c
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
PathGlobs
perhaps?
f
Just write a rule to produce a
MySuffixPythonTargets
type. It would take
AllPythonTargets
as a parameter.
c
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
Then filter
AllPythonTargets.first_party
to file targets with
.is_file_target
and with the desired suffix by checking the
tgt.address
attribute.
b
Ah I missed the targets part of this.
PathGLobs
likely isn't your path
(pun intended)
😂 1
😛 1
h
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