-
Notifications
You must be signed in to change notification settings - Fork 23
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
Conda Generic Plugin Hooks #45
Open
travishathaway
wants to merge
14
commits into
conda:main
Choose a base branch
from
travishathaway:cep-conda-generic-plugin-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e51c394
adding initial draft
travishathaway 9ad8e22
updating abstract
travishathaway 7496dc2
making updates to the CEP; adding examples and explanations
travishathaway 2fbb831
various updates and tweaks to new cep
travishathaway b85a643
Apply suggestions from code review
travishathaway 4eb363a
renaming the *_run command hooks to *_command instead
travishathaway 3107b9f
adding content to the FAQ section
travishathaway 760d134
Apply suggestions from code review
travishathaway ec6b777
Apply suggestions from code review
travishathaway 2e5a032
updating to rework proposoal for exception related hook
travishathaway 3bc9b8a
Merge branch 'cep-conda-generic-plugin-hooks' of github.com:travishat…
travishathaway 3d666b0
Update new-cep.md
travishathaway dc02b4f
removing the exception handler from this CEP
travishathaway 0e350ee
adding a yet to be answered question
travishathaway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<table> | ||
<tr><td> Title </td><td> Conda Generic Plugin Hooks</td> | ||
<tr><td> Status </td><td> Draft </td></tr> | ||
<tr><td> Author(s) </td><td> Full Name <thathaway@anaconda.com></td></tr> | ||
<tr><td> Created </td><td> Dec 22, 2022</td></tr> | ||
<tr><td> Updated </td><td> Dec 22, 2022</td></tr> | ||
<tr><td> Discussion </td><td> _TBD_ </td></tr> | ||
<tr><td> Implementation </td><td> _TBD_ </td></tr> | ||
</table> | ||
|
||
[conda-pre-invoke-location]: https://github.com/conda/conda/blob/48f51e6c1d412270efbbdb1d9ff571087568b6ea/conda/cli/main.py#L69 | ||
|
||
## Abstract | ||
|
||
In order to support a variety of use cases and extensions to conda's default | ||
behavior, we propose a set of generic plugin hooks in this CEP. Included will | ||
be `pre_command` and `post_command` hooks that will allow | ||
plugin authors to execute their plugin code before or after conda commands | ||
run, respectively. For each hook, we outline example use cases and | ||
show exactly how plugin authors will define these new hooks. | ||
|
||
## Specification | ||
|
||
This specification calls for the creation of the following three new plugin hooks: | ||
|
||
- `pre_command`: runs before the invoked conda command is run | ||
- `post_command`: runs after the invoked conda command is run | ||
|
||
Below, we discuss an example showing the use of the `pre_command` and `post_command` | ||
hooks together. | ||
|
||
### `pre_command` and `post_command` | ||
|
||
Plugin authors create the `pre_command` and `post_command` hooks by first defining a `conda.plugins.hookimpl` | ||
decorated function called either `conda_pre_command` or `conda_post_command` which return either a `CondaPreCommand` | ||
or `CondaPostCommand` class, respectively. These classes will both receive the following three properties: | ||
|
||
- `name`: unique name which identifies this plugin hook | ||
- `action`: a callable which contains the code to be run | ||
- `run_for`: a Python `set` of strings representing the commands this will be run on (e.g. `install` and `create`) | ||
|
||
#### Example | ||
|
||
```python | ||
from conda.plugins import hookimpl, CondaPreCommand, CondaPostCommand | ||
|
||
|
||
PLUGIN_NAME = "custom_plugin" | ||
|
||
|
||
def custom_plugin_pre_command_action(command): | ||
""" | ||
Defines our custom pre-run action which simply prints a message. | ||
""" | ||
print(f"pre-run action before {command}") | ||
|
||
|
||
@hookimpl | ||
def conda_pre_commands(): | ||
""" | ||
Yields our CondaPreCommand instance which attaches our ``custom_plugin_pre_command_action`` | ||
to the "install" and "create" command. | ||
""" | ||
yield CondaPreCommand( | ||
name=f"{PLUGIN_NAME}_pre_command", | ||
action=custom_plugin_pre_command_action, | ||
run_for={"install", "create"} | ||
) | ||
|
||
|
||
def custom_plugin_post_command_action(command): | ||
""" | ||
Defines our custom post-run action which simply prints a message. | ||
""" | ||
print(f"post-run action after {command}") | ||
|
||
|
||
@hookimpl | ||
def conda_post_commands(): | ||
""" | ||
Yields our CondaPostCommand instance which attaches our ``custom_plugin_post_command_action`` to | ||
the "install" and "create" command. | ||
""" | ||
yield CondaPostCommand( | ||
name=f"{PLUGIN_NAME}_post_command", | ||
action=custom_plugin_post_command_action, | ||
run_for={"install", "create"} | ||
) | ||
``` | ||
|
||
## Motivation | ||
|
||
Our immediate motivations for adding these plugin hooks include: | ||
|
||
### Better authentication handling | ||
|
||
The `pre_command` hook will enable plugin authors to interrupt the normal start up of conda commands. | ||
For better authentication handling, this opens the door for either asking the user directly | ||
for their credentials or retrieving credentials from an OS keyring. These credentials can | ||
then be stored and used for the duration of the running command and subsequent runs too. | ||
|
||
A prototype of how this could function can be found in the following pull request: | ||
|
||
- https://github.com/conda/conda/pull/12139 | ||
|
||
### Enabling the creativity of our community | ||
|
||
The above use case is just one example, and we know that our users and | ||
community will have many more ideas for how best to utilize these generic plugin hooks. | ||
|
||
## Rationale | ||
|
||
By introducing a set of generic hooks like the above, we grant plugin authors with quite a bit of | ||
flexibility for customizing how conda behaves. We believe starting out with a small set of generic | ||
plugin hooks is best so that we do not overwhelm would-be conda plugin authors. Depending on | ||
how much these plugin hooks are used and whether there is a demand, we may choose add more in | ||
the future with a new CEP. For now though, we believe it is best to stick with this narrow | ||
selection as we slowly grow our plugin ecosystem. | ||
|
||
## FAQ | ||
|
||
- Will I have access to global variables such as `conda.base.context.context` in my plugin hook? | ||
- Yes, for all plugin hooks listed in this CEP, they will be placed after the global context | ||
object has been initialized. | ||
|
||
- Where exactly will the `pre_command` hooks be called? | ||
- All registered `pre_command` hooks will be called before the `do_call` call in the | ||
[`conda.cli.main:main_subshell`][conda-pre-invoke-location] function. | ||
|
||
- Where exactly will the `post_command` hooks be called? | ||
- All registered `post_command` hooks will be called after the `do_call` call in the | ||
[`conda.cli.main:main_subshell`][conda-pre-invoke-location] function. | ||
|
||
- Will these hooks be available for `conda activate` and `conda deactivate` commands? | ||
- Initially, no. But, if there is demand for this we will reconsider making these generics | ||
available for these two commands. The reason for this is the slightly different way these | ||
commands are handled versus others. Their implementation is tightly coupled to the shell | ||
of the current user and adequately handling this use case would be more complex than what | ||
we are currently prepared to implement. | ||
|
||
- Will I be able to combine the use of these hooks with others in a single plugin? | ||
- Yes! We want to create a rich set of plugin hooks to allow authors a variety of possibilities | ||
for extending conda. We believe the true power of these generics will show when they are | ||
combined with existing plugin hooks. | ||
|
||
- What is the execution order of the registered plugin hooks? | ||
- _TBD_ | ||
|
||
|
||
## Resolution | ||
|
||
_TBD_ | ||
|
||
## Copyright | ||
|
||
All CEPs are explicitly [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it optional or mandatory parameter?
(also asked similar question if exception handler hook should have it as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I think making this argument optional with the default behavior of it running for every command makes the most sense.