Skip to content

Abstract away plugin lookup and querying #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 24, 2025

Conversation

mgorny
Copy link
Contributor

@mgorny mgorny commented Mar 21, 2025

Add a PluginLoader class that handles loading and querying plugins.
This removes some duplicate code, prepares for plugins having more
than one method in the future, and enables using a single cached plugin
list, rather than repeatedly querying and instantiating them.

mgorny added 2 commits March 21, 2025 05:13
Refactor `get_variant_hashes_by_priority()` to reuse the list of plugin
names that is already included in the result of
`_query_variant_plugins()` rather than iterating over entry points
again.
Add a `PluginLoader` class that handles loading and querying plugins.
This removes some duplicate code, prepares for plugins having more
than one method in the future, and enables using a single cached plugin
list, rather than repeatedly querying and instantiating them.
@mgorny mgorny requested a review from DEKHTIARJonathan March 21, 2025 04:27
@DEKHTIARJonathan
Copy link
Member

DEKHTIARJonathan commented Mar 21, 2025

I'm all for this change - however you have 2 caching mechanisms and I don't think that's very useful.

  • functools.cache in the plugins file
  • VariantCache in the platform file.

load_plugins() is never called by itself - always called as load_plugins().get_provider_configs()

I think that function load_plugins() is better served as a staticmethod - maybe even a singleton design-pattern (which is essentially what you weakly implemented).

Please preserve VariantCache class, we probably need a custom cache-to-disk logic. Functools won't allow experimentation on that front (can always be replaced later).

@mgorny
Copy link
Contributor Author

mgorny commented Mar 21, 2025

Yes, right now the plugin API provides only one method, so there's only one use case for load_plugins(). However, the function will become truly useful when we add new plugin methods — this is basically a preparation for the variant building work.

@mgorny
Copy link
Contributor Author

mgorny commented Mar 22, 2025

(I've switched to a static method)

@DEKHTIARJonathan
Copy link
Member

mgorny#1

@mgorny
Copy link
Contributor Author

mgorny commented Mar 24, 2025

Resolved merge conflicts after attrs removal.

@DEKHTIARJonathan DEKHTIARJonathan merged commit 91ee0e5 into wheelnext:variant-json Mar 24, 2025
4 of 6 checks passed
DEKHTIARJonathan added a commit that referenced this pull request Mar 25, 2025
* Implement getting sorted variants aided by `variants.json`

Add a `variants_json` argument to `get_variant_hashes_by_priority()`
that enables the function to use variant definitions from
`variants.json` instead of evaluating all possible combinations.
This can yield significant speedup and lower resource use if only
a handful of variants need to be considered.

The function is implemented without any plugin API changes.  After all,
the current API should be simpler for plugin authors, and generating
separate variant lists should not pose a performance problem.

Implemention-wise, the function first converts all variant descriptions
from `variants.json` into `VariantDescription` classes.  Then, the list
of variants is fitlered to include only variants that match
configurations obtained from plugins.  Finally, they are sorted using
the configuration order.

I am not 100% sure about the sorting algorithm, but it seems to yield
exactly the same order at least for the `dummy-project` wheels.
The first element of the sort key is the number of metas in the variant,
to prioritize variants that matched more items.  It is followed by
priorities (indices in configuration order) of all metas, ordered
from the most preferred to the least preferred.

* Fix crash when a plugin is not installed

* Warn if providers or variant keys are missing

* Add a (failing) round-trip test for variants.json-based sorting

* Add CI workflow

* Fix variant sorting algorithm

* Add fuzzing tests (failing now)

* Include the known-failing example explicitly

* Fix sorting algorithm again

* Dependency Update

* Relax whitespace requirements in `VariantMeta.from_str()` (#4)

Allow any (or no) whitespace between `::` separators in
`VariantMeta.from_str()`, in order to make it more feasible for parsing
user input.  Unless I'm missing something, there should be no need
to strictly ensure that input is strictly formatted as `A :: B :: C`
(rather than, say, `A::B::C`), as long as we properly format output.

* [Removal of `attrs`] Replacing with `dataclass` (#3)

* [Transition to dataclass] `meta.py` file

* [Transition to dataclass] `config.py` file

* attrs removed

* Cleanup

* Abstract away plugin lookup and querying (#5)

* Remove duplicate plugin querying in get_variant_hashes_by_priority()

Refactor `get_variant_hashes_by_priority()` to reuse the list of plugin
names that is already included in the result of
`_query_variant_plugins()` rather than iterating over entry points
again.

* Abstract away plugin lookup and querying

Add a `PluginLoader` class that handles loading and querying plugins.
This removes some duplicate code, prepares for plugins having more
than one method in the future, and enables using a single cached plugin
list, rather than repeatedly querying and instantiating them.

* Replace `load_plugins()` with `create()` static method

* [Switch to Singleton]

---------

Co-authored-by: Jonathan DEKHTIAR <[email protected]>

* plugins: Rename `run()` to `get_supported_configs()`, add tests (#8)

Rename the `run()` API to `get_supported_configs()`, to prepare for
adding other plugin methods.  Fix bugs introduced while introducing
the singleton pattern.  Add tests.

---------

Co-authored-by: Jonathan Dekhtiar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants