-
Notifications
You must be signed in to change notification settings - Fork 5
[ASIM - 6224] - Update hookman to deal with multiple versions of a plugin #147
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,18 +2,31 @@ | |
| import shutil | ||
| from collections.abc import Callable | ||
| from collections.abc import Sequence | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import List | ||
| from typing import Optional | ||
| from zipfile import ZipFile | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
| from hookman import hookman_utils | ||
| from hookman.exceptions import InvalidDestinationPathError | ||
| from hookman.exceptions import PluginAlreadyInstalledError | ||
| from hookman.hookman_utils import change_path_env | ||
| from hookman.plugin_config import PluginInfo | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class InstalledPluginInfo: | ||
| """ | ||
| Responsible to store the information about an installed plugin. | ||
| """ | ||
|
|
||
| id: str | ||
| version: Version | ||
|
|
||
|
|
||
| class HookSpecs: | ||
| """ | ||
| A class that holds the specification of the hooks, currently the following specification are available: | ||
|
|
@@ -89,19 +102,22 @@ def __init__(self, *, specs: HookSpecs, plugin_dirs: List[Path]): | |
| for hook in specs.hooks | ||
| } | ||
|
|
||
| def install_plugin(self, plugin_file_path: Path, dest_path: Path) -> str: | ||
| def install_plugin(self, plugin_file_path: Path, dest_path: Path) -> InstalledPluginInfo: | ||
| """ | ||
| Extract the content of the zip file into dest_path. | ||
| If the installation occurs successfully the name of the installed plugin will be returned. | ||
| If the installation occurs successfully a InstalledPluginInfo will be returned. | ||
|
|
||
| The following checks will be executed to validate the consistency of the inputs: | ||
|
|
||
| 1. The destination Path should be one of the paths informed during the initialization of HookMan (plugins_dirs field). | ||
|
|
||
| 2. The plugins_dirs cannot have two plugins with the same name. | ||
| 2. The plugins_dirs cannot have two plugins with the same name and version. | ||
|
|
||
| :plugin_file_path: The Path for the ``.hmplugin`` | ||
| :dest_path: The destination to where the plugin should be placed. | ||
| :param: plugin_file_path: | ||
| The Path for the ``.hmplugin`` | ||
|
|
||
| :param dest_path: | ||
| The destination to where the plugin should be placed. | ||
| """ | ||
| plugin_file_zip = ZipFile(plugin_file_path) | ||
| PluginInfo.validate_plugin_file(plugin_file_zip=plugin_file_zip) | ||
|
|
@@ -114,17 +130,21 @@ def install_plugin(self, plugin_file_path: Path, dest_path: Path) -> str: | |
| ) | ||
|
|
||
| yaml_content = plugin_file_zip.open("assets/plugin.yaml").read().decode("utf-8") | ||
| plugin_id = PluginInfo._load_yaml_file(yaml_content)["id"] | ||
|
|
||
| yaml_data = PluginInfo._load_yaml_file(yaml_content) | ||
| plugin_id: str = yaml_data["id"] | ||
| plugin_version: str = yaml_data["version"] | ||
| plugin_id_version = f"{plugin_id}-{plugin_version}" | ||
|
|
||
| plugins_dirs = [x for x in dest_path.iterdir() if x.is_dir()] | ||
|
|
||
| if plugin_id in [x.name for x in plugins_dirs]: | ||
| if plugin_id_version in [x.name for x in plugins_dirs]: | ||
| raise PluginAlreadyInstalledError("Plugin already installed") | ||
|
|
||
| plugin_destination_folder = dest_path / plugin_id | ||
| plugin_destination_folder = dest_path / plugin_id_version | ||
| plugin_destination_folder.mkdir(parents=True) | ||
| plugin_file_zip.extractall(plugin_destination_folder) | ||
| return plugin_id | ||
| return InstalledPluginInfo(version=Version(plugin_version), id=plugin_id) | ||
|
Comment on lines
+133
to
+147
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How will existing installed plugins be handled? |
||
|
|
||
| def _move_to_trash(self, root_dir, name): | ||
| """ | ||
|
|
@@ -157,16 +177,29 @@ def _try_clear_trash(self, root_dir): | |
| with suppress(OSError): | ||
| filename.unlink() | ||
|
|
||
| def remove_plugin(self, caption: str): | ||
| def remove_plugin(self, caption: str, version: Version | None = None) -> None: | ||
| """ | ||
| This method receives the name of the plugin as input, and will remove completely the plugin from ``plugin_dirs``. | ||
| This method receives the name and version of plugin as input, and will remove completely the | ||
| plugin from ``plugin_dirs``. | ||
|
|
||
| :param caption: | ||
| Name of the plugin to be removed. | ||
|
|
||
| :caption: Name of the plugin to be removed | ||
| :param version: | ||
| Optional parameter used to remove a specific version of plugin. Case it is not specified, | ||
| all versions of a given plugin will be removed. | ||
| """ | ||
| for plugin in self.get_plugins_available(): | ||
| if plugin.id == caption: | ||
| plugin_dir = plugin.yaml_location.parents[1] | ||
| root_dir = plugin_dir.parent | ||
| plugin_dir = plugin.yaml_location.parents[1] | ||
| root_dir = plugin_dir.parent | ||
| remove_plugin = False | ||
|
|
||
| if version is None and caption in plugin_dir.name: | ||
| remove_plugin = True | ||
| elif plugin.id == caption and version == plugin.version: | ||
| remove_plugin = True | ||
|
|
||
| if remove_plugin: | ||
| self._move_to_trash(root_dir, plugin_dir.name) | ||
| self._try_clear_trash(root_dir) | ||
| break | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.