Morning all - I am converting our build over to Pa...
# general
f
Morning all - I am converting our build over to Pants. I have tests running and am trying to get our wsgi app packaged. We have a Pyramid app that is scanning the package to pick up views registered with a decorator. I take it aside from manually listing the views in a BUILD file I would need to create a plugin?
h
Yes, that is one way to do that. Pants allows you to add several dependency inference rules, so you can add your custom logic to augment what pants already has
f
Are there any example plugins that we could leverage that does this?
h
One of the projects we want to do this year is to rewrite the example plug-in repository to have several examples like this. In the meantime, this might be a decent example: https://github.com/pantsbuild/pants/blob/main/src/python/pants/backend/codegen/protobuf/protobuf_dependency_inference.py The main idea generally with dependency inference is that you will want to parse out the relevant information from the file, and then associate the post information with some mapping that you precomputed. Pants already has a module mapping for Python, so that it knows imports ports of foo.bar -> src/py/foo/bar.py for example. You will be able to leverage that in your plug-in
To help me give you better advice, can you please explain more about how the dependency inference should work here? For example, what is the decorator?
f
Basically in pyramid you can decorate your views using: @view_config(route_name='xyz') https://docs.pylonsproject.org/projects/pyramid/en/2.0-branch/narr/urldispatch.html#route-configuration
In some wsgi file you can do a config.scan() This using something that scans your packages for these decorators and registers them with pyramid.
So it seems I need some kind of regex for the decorator, and somehow map these files to some other file.
So with the route name, how do you determine what that comes from? Does that corresponds to a file name?
By the way, in case it is not obvious, the reason that dependency inference & the dependencies field matters is it is what gives you fine grained caching. If you depended on everything in your project, then we could never safely cache test results for example Fine-grained dependencies also make project introspection more useful https://www.pantsbuild.org/v2.11/docs/project-introspection
f
Unfortunately it isn't so simple. The app is using pyramid traversals.
There is view_config(context=ResourceClassName) ResourceClassName: name = 'the-url-segment'
It seems pants picks up the relationship between the views and the resource. I just need a way to link it back to the wsgi class that does the scan.
h
So you need the wsgi class to depend on anything that has a
view_config
in it?
f
Yes, that's what I need to achieve.
h
I think that should be straightforward to implement as a plugin. The interesting question would be how to identify the wsgi class to the plugin though.
Does it have any distinguishing content that the plugin can identify?
f
Yes, it would do:
from pyramid.config import Configurator config = Configurator(...) config.scan(package='xyz')
Could I use the decorator like: @rule(desc="Inferring Protobuf dependencies by analyzing imports") How would I associate it back to another file that did the import. The protobuf one seems to map the current file to the import. I would need to somehow associate the file with the found decorator back to another file with the pyramid Configurator
h
Yeah, this is where your case is interesting! I think your plugin would have to gather two kinds of information for files: 1) do they contain a
config.scan(package='xyz')
and 2) do they contain a
view_config
. And then stitch them together into dependencies in a post-processing step.
(based on whatever the
package=
says)