11import os
22import subprocess
3- import sys
43
54from importlib .metadata import PackageNotFoundError , distribution
65
@@ -15,61 +14,6 @@ class PluginInstallError(Exception):
1514 """插件安装错误"""
1615
1716
18- def _ensure_pip_available () -> bool :
19- """确保 pip 在虚拟环境中可用"""
20- try :
21- result = subprocess .run ([sys .executable , '-m' , 'pip' , '--version' ], capture_output = True , text = True )
22- if result .returncode == 0 :
23- return True
24- except (subprocess .TimeoutExpired , subprocess .SubprocessError , FileNotFoundError ):
25- pass
26-
27- # 尝试使用 ensurepip
28- try :
29- subprocess .check_call (
30- [sys .executable , '-m' , 'ensurepip' , '--default-pip' ],
31- stdout = subprocess .DEVNULL ,
32- stderr = subprocess .DEVNULL ,
33- )
34- result = subprocess .run ([sys .executable , '-m' , 'pip' , '--version' ], capture_output = True , text = True )
35- if result .returncode == 0 :
36- return True
37- except (subprocess .CalledProcessError , subprocess .TimeoutExpired , subprocess .SubprocessError , FileNotFoundError ):
38- pass
39-
40- # 尝试下载并安装
41- try :
42- import os
43- import tempfile
44-
45- import httpx
46-
47- try :
48- with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.py' , delete = False ) as f :
49- with httpx .Client (timeout = 3 ) as client :
50- get_pip_url = 'https://bootstrap.pypa.io/get-pip.py'
51- response = client .get (get_pip_url )
52- response .raise_for_status ()
53- f .write (response .text )
54- temp_file = f .name
55- except Exception : # noqa: ignore
56- return False
57-
58- try :
59- subprocess .check_call ([sys .executable , temp_file ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
60- result = subprocess .run ([sys .executable , '-m' , 'pip' , '--version' ], capture_output = True , text = True )
61- return result .returncode == 0
62- finally :
63- try :
64- os .unlink (temp_file )
65- except OSError :
66- pass
67- except Exception : # noqa: ignore
68- pass
69-
70- return False
71-
72-
7317def get_plugins () -> list [str ]:
7418 """
7519 获取插件列表
@@ -111,10 +55,7 @@ def install_requirements(plugin: str | None) -> None: # noqa: C901
11155
11256 if missing_dependencies :
11357 try :
114- if not _ensure_pip_available ():
115- raise PluginInstallError (f'pip 安装失败,无法继续安装插件 { plugin } 依赖' )
116-
117- pip_install = [sys .executable , '-m' , 'pip' , 'install' , '-r' , requirements_file ]
58+ pip_install = ['uv' , 'pip' , 'install' , '-r' , requirements_file ]
11859 if settings .PLUGIN_PIP_CHINA :
11960 pip_install .extend (['-i' , settings .PLUGIN_PIP_INDEX_URL ])
12061
@@ -149,7 +90,7 @@ def uninstall_requirements(plugin: str) -> None:
14990 requirements_file = PLUGIN_DIR / plugin / 'requirements.txt'
15091 if os .path .exists (requirements_file ):
15192 try :
152- pip_uninstall = [sys . executable , '-m ' , 'pip' , 'uninstall' , '-r' , requirements_file , '-y' ]
93+ pip_uninstall = ['uv ' , 'pip' , 'uninstall' , '-r' , str ( requirements_file ) , '-y' ]
15394 subprocess .check_call (pip_uninstall , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
15495 except subprocess .CalledProcessError as e :
15596 raise PluginInstallError (f'插件 { plugin } 依赖卸载失败:{ e } ' ) from e
0 commit comments