|
5 | 5 | import logging |
6 | 6 | from io import StringIO |
7 | 7 | from pathlib import Path |
| 8 | +from functools import wraps |
8 | 9 | from itertools import repeat |
9 | 10 | from contextlib import suppress |
10 | 11 | from typing import Any, TypeVar |
11 | | -from functools import wraps, lru_cache |
12 | 12 | from typing_extensions import Annotated |
13 | 13 | from dataclasses import field, dataclass |
14 | 14 | from collections.abc import Callable, Iterable |
|
48 | 48 | DependsInner = type(Depends()) |
49 | 49 |
|
50 | 50 |
|
51 | | -class _ReturnEq: |
52 | | - def __eq__(self, __o: _T) -> _T: |
53 | | - return __o |
54 | | - |
55 | | - |
56 | | -return_eq = _ReturnEq() |
57 | | - |
58 | | - |
59 | 51 | class LoguruHandler(logging.Handler): |
60 | 52 | def emit(self, record: logging.LogRecord): |
61 | 53 | try: |
@@ -226,31 +218,35 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> Iterable[_T]: |
226 | 218 | return wrapper |
227 | 219 |
|
228 | 220 |
|
229 | | -_packages_distributions = lru_cache(None)(packages_distributions) |
| 221 | +pkgs = packages_distributions() |
230 | 222 |
|
231 | 223 |
|
232 | 224 | def is_editable(plugin: Plugin) -> bool: |
233 | 225 | """Check if the distribution is installed in editable mode""" |
234 | 226 | while plugin.parent_plugin: |
235 | 227 | plugin = plugin.parent_plugin |
236 | 228 |
|
| 229 | + path = files(plugin.module) |
| 230 | + |
| 231 | + if not isinstance(path, Path) or "site-packages" in path.parts: |
| 232 | + return False |
| 233 | + |
237 | 234 | dist: Distribution | None = None |
238 | 235 |
|
239 | | - if plugin.metadata: |
240 | | - with suppress(PackageNotFoundError): |
241 | | - dist = distribution(plugin.metadata.name) |
242 | | - |
243 | | - # XXX: 有些包有不正确的 top_level.txt (例如: kiwisolver 的 src), |
244 | | - # 导致下面的代码有可能得出错误的结果. 参见: https://github.com/nucleic/kiwi/issues/169 |
245 | | - # if not dist: |
246 | | - # with suppress(KeyError, IndexError): |
247 | | - # dist = distribution( |
248 | | - # _packages_distributions()[plugin.module_name.split(".")[0]][0] |
249 | | - # ) |
250 | | - |
251 | | - if not dist: |
252 | | - path = files(plugin.module) |
253 | | - return isinstance(path, Path) and "site-packages" not in path.parts |
| 236 | + with suppress(PackageNotFoundError): |
| 237 | + dist = distribution(plugin.name.replace("_", "-")) |
| 238 | + |
| 239 | + if not (dist or plugin.module.__file__ is None): |
| 240 | + path = Path(plugin.module.__file__) |
| 241 | + for name in pkgs.get(plugin.module_name.split(".")[0], ()): |
| 242 | + dist = distribution(name) |
| 243 | + if path in map(methodcaller("locate"), dist.files or ()): |
| 244 | + break |
| 245 | + else: |
| 246 | + dist = None |
| 247 | + |
| 248 | + if dist is None: |
| 249 | + return True |
254 | 250 |
|
255 | 251 | # https://github.com/pdm-project/pdm/blob/fee1e6bffd7de30315e2134e19f9a6f58e15867c/src/pdm/utils.py#L361-L374 |
256 | 252 | if getattr(dist, "link_file", None) is not None: |
|
0 commit comments