mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 12:05:54 +00:00
style: introduce ruff as linter and formatter (#1356)
* style: remove necessary imports * style: fix F841 * style: fix F401 * style: fix F811 * style: fix E402 * style: fix E721 * style: fix E722 * style: fix E722 * style: fix F541 * style: ruff format * style: all passed * style: add ruff in deps * style: more ignores in ruff.toml * style: add pre-commit
This commit is contained in:
committed by
GitHub
parent
09e70d70e9
commit
209f16af76
@@ -2,7 +2,6 @@ from __future__ import annotations
|
||||
|
||||
import re
|
||||
import os
|
||||
import shutil
|
||||
import zipfile
|
||||
import ssl
|
||||
import certifi
|
||||
@@ -18,33 +17,37 @@ from ...core import taskmgr
|
||||
|
||||
|
||||
class GitHubRepoInstaller(installer.PluginInstaller):
|
||||
"""GitHub仓库插件安装器
|
||||
"""
|
||||
"""GitHub仓库插件安装器"""
|
||||
|
||||
def get_github_plugin_repo_label(self, repo_url: str) -> list[str]:
|
||||
"""获取username, repo"""
|
||||
repo = re.findall(
|
||||
r"(?:https?://github\.com/|git@github\.com:)([^/]+/[^/]+?)(?:\.git|/|$)",
|
||||
r'(?:https?://github\.com/|git@github\.com:)([^/]+/[^/]+?)(?:\.git|/|$)',
|
||||
repo_url,
|
||||
)
|
||||
if len(repo) > 0:
|
||||
return repo[0].split("/")
|
||||
return repo[0].split('/')
|
||||
else:
|
||||
return None
|
||||
|
||||
async def download_plugin_source_code(self, repo_url: str, target_path: str, task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder()) -> str:
|
||||
async def download_plugin_source_code(
|
||||
self,
|
||||
repo_url: str,
|
||||
target_path: str,
|
||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||
) -> str:
|
||||
"""下载插件源码(全异步)"""
|
||||
repo = self.get_github_plugin_repo_label(repo_url)
|
||||
if repo is None:
|
||||
raise errors.PluginInstallerError('仅支持GitHub仓库地址')
|
||||
|
||||
|
||||
target_path += repo[1]
|
||||
self.ap.logger.debug("正在下载源码...")
|
||||
task_context.trace("下载源码...", "download-plugin-source-code")
|
||||
|
||||
zipball_url = f"https://api.github.com/repos/{'/'.join(repo)}/zipball/HEAD"
|
||||
self.ap.logger.debug('正在下载源码...')
|
||||
task_context.trace('下载源码...', 'download-plugin-source-code')
|
||||
|
||||
zipball_url = f'https://api.github.com/repos/{"/".join(repo)}/zipball/HEAD'
|
||||
zip_resp: bytes = None
|
||||
|
||||
|
||||
# 创建自定义SSL上下文,使用certifi提供的根证书
|
||||
ssl_context = ssl.create_default_context(cafile=certifi.where())
|
||||
|
||||
@@ -52,41 +55,44 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
||||
async with session.get(
|
||||
url=zipball_url,
|
||||
timeout=aiohttp.ClientTimeout(total=300),
|
||||
ssl=ssl_context # 使用自定义SSL上下文来验证证书
|
||||
ssl=ssl_context, # 使用自定义SSL上下文来验证证书
|
||||
) as resp:
|
||||
if resp.status != 200:
|
||||
raise errors.PluginInstallerError(f"下载源码失败: {await resp.text()}")
|
||||
raise errors.PluginInstallerError(
|
||||
f'下载源码失败: {await resp.text()}'
|
||||
)
|
||||
zip_resp = await resp.read()
|
||||
|
||||
if await aiofiles_os.path.exists("temp/" + target_path):
|
||||
await aioshutil.rmtree("temp/" + target_path)
|
||||
|
||||
if await aiofiles_os.path.exists('temp/' + target_path):
|
||||
await aioshutil.rmtree('temp/' + target_path)
|
||||
|
||||
if await aiofiles_os.path.exists(target_path):
|
||||
await aioshutil.rmtree(target_path)
|
||||
|
||||
await aiofiles_os.makedirs("temp/" + target_path)
|
||||
await aiofiles_os.makedirs('temp/' + target_path)
|
||||
|
||||
async with aiofiles.open("temp/" + target_path + "/source.zip", "wb") as f:
|
||||
async with aiofiles.open('temp/' + target_path + '/source.zip', 'wb') as f:
|
||||
await f.write(zip_resp)
|
||||
|
||||
self.ap.logger.debug("解压中...")
|
||||
task_context.trace("解压中...", "unzip-plugin-source-code")
|
||||
|
||||
with zipfile.ZipFile("temp/" + target_path + "/source.zip", "r") as zip_ref:
|
||||
zip_ref.extractall("temp/" + target_path)
|
||||
await aiofiles_os.remove("temp/" + target_path + "/source.zip")
|
||||
self.ap.logger.debug('解压中...')
|
||||
task_context.trace('解压中...', 'unzip-plugin-source-code')
|
||||
|
||||
with zipfile.ZipFile('temp/' + target_path + '/source.zip', 'r') as zip_ref:
|
||||
zip_ref.extractall('temp/' + target_path)
|
||||
await aiofiles_os.remove('temp/' + target_path + '/source.zip')
|
||||
|
||||
import glob
|
||||
unzip_dir = glob.glob("temp/" + target_path + "/*")[0]
|
||||
await aioshutil.copytree(unzip_dir, target_path + "/")
|
||||
|
||||
unzip_dir = glob.glob('temp/' + target_path + '/*')[0]
|
||||
await aioshutil.copytree(unzip_dir, target_path + '/')
|
||||
await aioshutil.rmtree(unzip_dir)
|
||||
|
||||
self.ap.logger.debug("源码下载完成。")
|
||||
|
||||
self.ap.logger.debug('源码下载完成。')
|
||||
return repo[1]
|
||||
|
||||
async def install_requirements(self, path: str):
|
||||
if os.path.exists(path + "/requirements.txt"):
|
||||
pkgmgr.install_requirements(path + "/requirements.txt")
|
||||
if os.path.exists(path + '/requirements.txt'):
|
||||
pkgmgr.install_requirements(path + '/requirements.txt')
|
||||
|
||||
async def install_plugin(
|
||||
self,
|
||||
@@ -94,12 +100,14 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||
):
|
||||
"""安装插件"""
|
||||
task_context.trace("下载插件源码...", "install-plugin")
|
||||
repo_label = await self.download_plugin_source_code(plugin_source, "plugins/", task_context)
|
||||
task_context.trace("安装插件依赖...", "install-plugin")
|
||||
await self.install_requirements("plugins/" + repo_label)
|
||||
task_context.trace("完成.", "install-plugin")
|
||||
|
||||
task_context.trace('下载插件源码...', 'install-plugin')
|
||||
repo_label = await self.download_plugin_source_code(
|
||||
plugin_source, 'plugins/', task_context
|
||||
)
|
||||
task_context.trace('安装插件依赖...', 'install-plugin')
|
||||
await self.install_requirements('plugins/' + repo_label)
|
||||
task_context.trace('完成.', 'install-plugin')
|
||||
|
||||
# Caution: in the v4.0, plugin without manifest will not be able to be updated
|
||||
# await self.ap.plugin_mgr.setting.record_installed_plugin_source(
|
||||
# "plugins/" + repo_label + '/', plugin_source
|
||||
@@ -115,9 +123,9 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
||||
if plugin_container is None:
|
||||
raise errors.PluginInstallerError('插件不存在或未成功加载')
|
||||
else:
|
||||
task_context.trace("删除插件目录...", "uninstall-plugin")
|
||||
task_context.trace('删除插件目录...', 'uninstall-plugin')
|
||||
await aioshutil.rmtree(plugin_container.pkg_path)
|
||||
task_context.trace("完成, 重新加载以生效.", "uninstall-plugin")
|
||||
task_context.trace('完成, 重新加载以生效.', 'uninstall-plugin')
|
||||
|
||||
async def update_plugin(
|
||||
self,
|
||||
@@ -126,14 +134,14 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||
):
|
||||
"""更新插件"""
|
||||
task_context.trace("更新插件...", "update-plugin")
|
||||
task_context.trace('更新插件...', 'update-plugin')
|
||||
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
|
||||
if plugin_container is None:
|
||||
raise errors.PluginInstallerError('插件不存在或未成功加载')
|
||||
else:
|
||||
if plugin_container.plugin_repository:
|
||||
plugin_source = plugin_container.plugin_repository
|
||||
task_context.trace("转交安装任务.", "update-plugin")
|
||||
task_context.trace('转交安装任务.', 'update-plugin')
|
||||
await self.install_plugin(plugin_source, task_context)
|
||||
else:
|
||||
raise errors.PluginInstallerError('插件无源码信息,无法更新')
|
||||
raise errors.PluginInstallerError('插件无源码信息,无法更新')
|
||||
|
||||
Reference in New Issue
Block a user