Skip to content

Commit 4d13424

Browse files
authored
Add plugin requirements auto install functions (#514)
1 parent 860c96d commit 4d13424

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

backend/plugin/tools.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
import asyncio
34
import inspect
45
import os
6+
import subprocess
7+
import sys
58
import warnings
69

10+
from asyncio import subprocess as async_subprocess
11+
712
import rtoml
813

914
from fastapi import APIRouter
@@ -119,3 +124,42 @@ def plugin_router_inject() -> None:
119124

120125
# 将插件路由注入到目标 router 中
121126
target_router.include_router(plugin_router)
127+
128+
129+
def install_requirements() -> None:
130+
"""安装插件依赖"""
131+
plugins = get_plugins()
132+
for plugin in plugins:
133+
requirements_file = os.path.join(PLUGIN_DIR, plugin, 'requirements.txt')
134+
if not os.path.exists(requirements_file):
135+
continue
136+
else:
137+
try:
138+
subprocess.run([sys.executable, '-m', 'ensurepip', '--upgrade'])
139+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', requirements_file])
140+
except subprocess.CalledProcessError as e:
141+
raise EnvironmentError(f'插件 {plugin} 依赖安装失败:{e}') from e
142+
143+
144+
async def install_requirements_async() -> None:
145+
"""异步安装插件依赖"""
146+
plugins = get_plugins()
147+
for plugin in plugins:
148+
requirements_file = os.path.join(PLUGIN_DIR, plugin, 'requirements.txt')
149+
if not os.path.exists(requirements_file):
150+
continue
151+
else:
152+
await async_subprocess.create_subprocess_exec(sys.executable, '-m', 'ensurepip', '--upgrade')
153+
res = await async_subprocess.create_subprocess_exec(
154+
sys.executable,
155+
'-m',
156+
'pip',
157+
'install',
158+
'-r',
159+
requirements_file,
160+
stdout=asyncio.subprocess.PIPE,
161+
stderr=asyncio.subprocess.PIPE,
162+
)
163+
_, stderr = await res.communicate()
164+
if res.returncode != 0:
165+
raise EnvironmentError(f'插件 {plugin} 依赖包安装失败:{stderr}')

backend/scripts/init_data.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
# ruff: noqa: I001
4-
from backend.database.db import create_table
5-
import logging
6-
import sys
7-
84
from anyio import run
95

10-
sys.path.append('../')
11-
12-
13-
logging.basicConfig(level=logging.INFO)
14-
logger = logging.getLogger(__name__)
6+
from backend.database.db import create_table
157

168

179
async def init() -> None:
18-
logger.info('Creating initial data')
10+
print('Creating initial data')
1911
await create_table()
20-
logger.info('Initial data created')
12+
print('Initial data created')
2113

2214

2315
if __name__ == '__main__':

backend/scripts/init_plugin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# ruff: noqa: I001
4+
from anyio import run
5+
6+
from backend.plugin.tools import install_requirements_async
7+
8+
9+
async def init() -> None:
10+
print('Starting initial plugin')
11+
await install_requirements_async()
12+
print('Plugin successfully installed')
13+
14+
15+
if __name__ == '__main__':
16+
run(init) # type: ignore

0 commit comments

Comments
 (0)