Skip to content

Commit 3be29f3

Browse files
authored
Merge pull request jxxghp#5564 from DDSRem-Dev/dev
2 parents 1a88b53 + 7638db4 commit 3be29f3

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

app/core/plugin.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,7 @@ def get_plugin_remote_entry(plugin_id: str, dist_path: str) -> str:
777777
:return: 远程入口地址
778778
"""
779779
dist_path = dist_path.strip("/")
780-
api_prefix = settings.API_V1_STR.rstrip("/")
781780
path = posixpath.join(
782-
api_prefix,
783781
"plugin",
784782
"file",
785783
plugin_id.lower(),

app/helper/plugin.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
import aioshutil
1414
import httpx
1515
from anyio import Path as AsyncPath
16+
from packaging.requirements import Requirement
1617
from packaging.specifiers import SpecifierSet, InvalidSpecifier
1718
from packaging.version import Version, InvalidVersion
18-
from pkg_resources import Requirement, working_set
19+
from importlib.metadata import distributions
1920
from requests import Response
2021

2122
from app.core.cache import cached
@@ -729,18 +730,26 @@ def install_dependencies(self, dependencies: List[str]) -> Tuple[bool, str]:
729730
def __get_installed_packages(self) -> Dict[str, Version]:
730731
"""
731732
获取已安装的包及其版本
732-
使用 pkg_resources 获取当前环境中已安装的包,标准化包名并转换版本信息
733+
使用 importlib.metadata 获取当前环境中已安装的包,标准化包名并转换版本信息
733734
对于无法解析的版本,记录警告日志并跳过
734735
:return: 已安装包的字典,格式为 {package_name: Version}
735736
"""
736737
installed_packages = {}
737738
try:
738-
for dist in working_set:
739-
pkg_name = self.__standardize_pkg_name(dist.project_name)
739+
for dist in distributions():
740+
name = dist.metadata.get("Name")
741+
if not name:
742+
continue
743+
pkg_name = self.__standardize_pkg_name(name)
744+
version_str = dist.metadata.get("Version") or getattr(dist, "version", None)
745+
if not version_str:
746+
continue
740747
try:
741-
installed_packages[pkg_name] = Version(dist.version)
748+
v = Version(version_str)
749+
if pkg_name not in installed_packages or v > installed_packages[pkg_name]:
750+
installed_packages[pkg_name] = v
742751
except InvalidVersion:
743-
logger.debug(f"无法解析已安装包 '{pkg_name}' 的版本:{dist.version}")
752+
logger.debug(f"无法解析已安装包 '{pkg_name}' 的版本:{version_str}")
744753
continue
745754
return installed_packages
746755
except Exception as e:
@@ -844,12 +853,14 @@ def __merge_dependencies(dependencies: Dict[str, Set[str]]) -> Dict[str, str]:
844853
@staticmethod
845854
def __standardize_pkg_name(name: str) -> str:
846855
"""
847-
标准化包名,将包名转换为小写并将连字符替换为下划线
856+
标准化包名,将包名转换为小写,连字符与点替换为下划线(与 PEP 503 归一化风格一致)
848857
849858
:param name: 原始包名
850859
:return: 标准化后的包名
851860
"""
852-
return name.lower().replace("-", "_") if name else name
861+
if not name:
862+
return name
863+
return name.lower().replace("-", "_").replace(".", "_")
853864

854865
async def async_get_plugin_package_version(self, pid: str, repo_url: str,
855866
package_version: Optional[str] = None) -> Optional[str]:

requirements.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
setuptools
21
Cython~=3.1.2
32
pydantic>=2.0.0,<3.0.0
43
pydantic-settings>=2.0.0,<3.0.0

0 commit comments

Comments
 (0)