Skip to content

Commit 4a72a86

Browse files
committed
Introduce PluginSource, PluginSourceGit, PluginSourceLocal
- they'll be used to install or update plugins - the idea is to support both raw directories and git repos from local media - and of course support for git remote repositories - PluginSources are expected to be configured by UI
1 parent 5863d60 commit 4a72a86

File tree

1 file changed

+57
-26
lines changed

1 file changed

+57
-26
lines changed

picard/plugin3/plugin.py

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,57 @@ def transfer_progress(self, stats):
3535
print(f'{stats.indexed_objects}/{stats.total_objects}')
3636

3737

38+
class PluginSourceSyncError(Exception):
39+
pass
40+
41+
42+
class PluginSource:
43+
"""Abstract class for plugin sources"""
44+
45+
def sync(self, target_directory: str):
46+
raise NotImplementedError
47+
48+
49+
class PluginSourceGit(PluginSource):
50+
"""Plugin is stored in a git repository, local or remote"""
51+
def __init__(self, url: str, ref: str = None):
52+
super().__init__()
53+
# Note: url can be a local directory
54+
self.url = url
55+
self.ref = ref or 'main'
56+
57+
def sync(self, target_directory: str):
58+
if os.path.isdir(target_directory):
59+
print(f'{target_directory} exists, fetch changes')
60+
repo = pygit2.Repository(target_directory)
61+
for remote in repo.remotes:
62+
remote.fetch(callbacks=GitRemoteCallbacks())
63+
else:
64+
print(f'Cloning {self.url} to {target_directory}')
65+
repo = pygit2.clone_repository(self.url, target_directory, callbacks=GitRemoteCallbacks())
66+
print(list(repo.references))
67+
print(list(repo.branches))
68+
print(list(repo.remotes))
69+
70+
if self.ref:
71+
commit = repo.revparse_single(self.ref)
72+
else:
73+
commit = repo.revparse_single('HEAD')
74+
75+
print(commit)
76+
print(commit.message)
77+
# hard reset to passed ref or HEAD
78+
repo.reset(commit.id, pygit2.enums.ResetMode.HARD)
79+
80+
81+
class PluginSourceLocal(PluginSource):
82+
"""Plugin is stored in a local directory, but is not a git repo"""
83+
84+
def sync(self, target_directory: str):
85+
# TODO: copy tree to plugin directory (?)
86+
pass
87+
88+
3889
class Plugin:
3990
local_path: str = None
4091
remote_url: str = None
@@ -52,34 +103,14 @@ def __init__(self, plugins_dir: str, plugin_name: str):
52103
self.module_name = f'picard.plugins.{self.plugin_name}'
53104
self.local_path = os.path.join(self.plugins_dir, self.plugin_name)
54105

55-
def sync(self, url: str = None, ref: str = None):
106+
def sync(self, plugin_source: PluginSource = None):
56107
"""Sync plugin source
57-
Use remote url or local path, and sets the repository to ref
58108
"""
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)
109+
if plugin_source:
110+
try:
111+
plugin_source.sync(self.local_path)
112+
except Exception as e:
113+
raise PluginSourceSyncError(e)
83114

84115
def read_manifest(self):
85116
"""Reads metadata for the plugin from the plugin's MANIFEST.toml

0 commit comments

Comments
 (0)