|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Picard, the next-generation MusicBrainz tagger |
| 4 | +# |
| 5 | +# Copyright (C) 2024 Laurent Monin |
| 6 | +# Copyright (C) 2024 Philipp Wolfer |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or |
| 9 | +# modify it under the terms of the GNU General Public License |
| 10 | +# as published by the Free Software Foundation; either version 2 |
| 11 | +# of the License, or (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program; if not, write to the Free Software |
| 20 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + |
| 22 | +import importlib.util |
| 23 | +import os |
| 24 | +import sys |
| 25 | + |
| 26 | +from picard.plugin3.api import PluginApi |
| 27 | +from picard.plugin3.manifest import PluginManifest |
| 28 | + |
| 29 | +import pygit2 |
| 30 | + |
| 31 | + |
| 32 | +class GitRemoteCallbacks(pygit2.RemoteCallbacks): |
| 33 | + |
| 34 | + def transfer_progress(self, stats): |
| 35 | + print(f'{stats.indexed_objects}/{stats.total_objects}') |
| 36 | + |
| 37 | + |
| 38 | +class Plugin: |
| 39 | + local_path: str = None |
| 40 | + remote_url: str = None |
| 41 | + ref = None |
| 42 | + plugin_name: str = None |
| 43 | + module_name: str = None |
| 44 | + manifest: PluginManifest = None |
| 45 | + _module = None |
| 46 | + |
| 47 | + def __init__(self, plugins_dir: str, plugin_name: str): |
| 48 | + if not os.path.exists(plugins_dir): |
| 49 | + os.makedirs(plugins_dir) |
| 50 | + self.plugins_dir = plugins_dir |
| 51 | + self.plugin_name = plugin_name |
| 52 | + self.module_name = f'picard.plugins.{self.plugin_name}' |
| 53 | + self.local_path = os.path.join(self.plugins_dir, self.plugin_name) |
| 54 | + |
| 55 | + def sync(self, url: str = None, ref: str = None): |
| 56 | + """Sync plugin source |
| 57 | + Use remote url or local path, and sets the repository to ref |
| 58 | + """ |
| 59 | + if url: |
| 60 | + self.remote_url = url |
| 61 | + if os.path.isdir(self.local_path): |
| 62 | + print(f'{self.local_path} exists, fetch changes') |
| 63 | + repo = pygit2.Repository(self.local_path) |
| 64 | + for remote in repo.remotes: |
| 65 | + remote.fetch(callbacks=GitRemoteCallbacks()) |
| 66 | + else: |
| 67 | + print(f'Cloning {url} to {self.local_path}') |
| 68 | + repo = pygit2.clone_repository(url, self.local_path, callbacks=GitRemoteCallbacks()) |
| 69 | + |
| 70 | + print(list(repo.references)) |
| 71 | + print(list(repo.branches)) |
| 72 | + print(list(repo.remotes)) |
| 73 | + |
| 74 | + if ref: |
| 75 | + commit = repo.revparse_single(ref) |
| 76 | + else: |
| 77 | + commit = repo.revparse_single('HEAD') |
| 78 | + |
| 79 | + print(commit) |
| 80 | + print(commit.message) |
| 81 | + # hard reset to passed ref or HEAD |
| 82 | + repo.reset(commit.id, pygit2.enums.ResetMode.HARD) |
| 83 | + |
| 84 | + def read_manifest(self): |
| 85 | + """Reads metadata for the plugin from the plugin's MANIFEST.toml |
| 86 | + """ |
| 87 | + manifest_path = os.path.join(self.local_path, 'MANIFEST.toml') |
| 88 | + with open(manifest_path, 'rb') as manifest_file: |
| 89 | + self.manifest = PluginManifest(self.plugin_name, manifest_file) |
| 90 | + |
| 91 | + def load_module(self): |
| 92 | + """Load corresponding module from source path""" |
| 93 | + module_file = os.path.join(self.local_path, '__init__.py') |
| 94 | + spec = importlib.util.spec_from_file_location(self.module_name, module_file) |
| 95 | + module = importlib.util.module_from_spec(spec) |
| 96 | + sys.modules[self.module_name] = module |
| 97 | + spec.loader.exec_module(module) |
| 98 | + self._module = module |
| 99 | + return module |
| 100 | + |
| 101 | + def enable(self, tagger) -> None: |
| 102 | + """Enable the plugin""" |
| 103 | + api = PluginApi(self.manifest, tagger) |
| 104 | + self._module.enable(api) |
| 105 | + |
| 106 | + def disable(self) -> None: |
| 107 | + """Disable the plugin""" |
| 108 | + self._module.disable() |
0 commit comments