-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin.py
More file actions
79 lines (67 loc) · 2.77 KB
/
Copy pathplugin.py
File metadata and controls
79 lines (67 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import io
import os
import sys
import zipfile
import click
import requests
from morph.cli.flags import Flags
from morph.config.project import load_project
from morph.constants import MorphConstant
from morph.task.base import BaseTask
from morph.task.utils.morph import find_project_root_dir
class PluginTask(BaseTask):
def __init__(self, args: Flags):
super().__init__(args)
self.args = args
self.plugin_name: str = args.PLUGIN_NAME
try:
self.project_root = find_project_root_dir(os.getcwd())
except FileNotFoundError as e:
click.echo(click.style(f"Error: {str(e)}", fg="red"))
sys.exit(1)
project = load_project(self.project_root)
if not project:
click.echo(click.style("Project configuration not found.", fg="red"))
sys.exit(1)
def run(self):
branch = "main"
package_name = "morph-plugins"
organization = "morph-data"
plugin_git_url = f"https://github.com/{organization}/{package_name}"
plugin_dir = os.path.join(self.project_root, MorphConstant.PLUGIN_DIR)
zip_url = f"{plugin_git_url}/archive/refs/heads/{branch}.zip"
try:
response = requests.get(zip_url)
response.raise_for_status()
with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
if not any(
file.startswith(f"{package_name}-{branch}/{self.plugin_name}/")
for file in zip_ref.namelist()
):
raise Exception(f"{self.plugin_name} not found in plugins.")
for file in zip_ref.namelist():
if file.startswith(f"{package_name}-{branch}/{self.plugin_name}/"):
relative_path = file.replace(f"{package_name}-{branch}/", "", 1)
extract_path = os.path.join(plugin_dir, relative_path)
if file.endswith("/"):
os.makedirs(extract_path, exist_ok=True)
continue
os.makedirs(os.path.dirname(extract_path), exist_ok=True)
with zip_ref.open(file) as source, open(
extract_path, "wb"
) as target:
target.write(source.read())
except Exception as e:
click.echo(
click.style(
f"Error: {str(e)}\nFailed to fetch plugin {self.plugin_name}.",
fg="red",
)
)
sys.exit(1)
click.echo(
click.style(
f"✅ Plugin {self.plugin_name} has been added to {plugin_dir}/{self.plugin_name}/.",
fg="green",
)
)