18
18
from itertools import filterfalse , tee , zip_longest
19
19
20
20
from pip ._vendor import pkg_resources
21
- from pip ._vendor .packaging .utils import canonicalize_name
22
21
23
22
# NOTE: retrying is not annotated in typeshed as on 2017-07-17, which is
24
23
# why we ignore the type on this import.
@@ -402,86 +401,20 @@ def get_installed_distributions(
402
401
paths = None # type: Optional[List[str]]
403
402
):
404
403
# type: (...) -> List[Distribution]
405
- """
406
- Return a list of installed Distribution objects.
407
-
408
- If ``local_only`` is True (default), only return installations
409
- local to the current virtualenv, if in a virtualenv.
410
-
411
- ``skip`` argument is an iterable of lower-case project names to
412
- ignore; defaults to stdlib_pkgs
413
-
414
- If ``include_editables`` is False, don't report editables.
415
-
416
- If ``editables_only`` is True , only report editables.
417
-
418
- If ``user_only`` is True , only report installations in the user
419
- site directory.
420
-
421
- If ``paths`` is set, only report the distributions present at the
422
- specified list of locations.
423
- """
424
- if paths :
425
- working_set = pkg_resources .WorkingSet (paths )
426
- else :
427
- working_set = pkg_resources .working_set
428
-
429
- if local_only :
430
- local_test = dist_is_local
431
- else :
432
- def local_test (d ):
433
- return True
434
-
435
- if include_editables :
436
- def editable_test (d ):
437
- return True
438
- else :
439
- def editable_test (d ):
440
- return not dist_is_editable (d )
441
-
442
- if editables_only :
443
- def editables_only_test (d ):
444
- return dist_is_editable (d )
445
- else :
446
- def editables_only_test (d ):
447
- return True
448
-
449
- if user_only :
450
- user_test = dist_in_usersite
451
- else :
452
- def user_test (d ):
453
- return True
454
-
455
- return [d for d in working_set
456
- if local_test (d ) and
457
- d .key not in skip and
458
- editable_test (d ) and
459
- editables_only_test (d ) and
460
- user_test (d )
461
- ]
462
-
463
-
464
- def _search_distribution (req_name ):
465
- # type: (str) -> Optional[Distribution]
466
- """Find a distribution matching the ``req_name`` in the environment.
467
-
468
- This searches from *all* distributions available in the environment, to
469
- match the behavior of ``pkg_resources.get_distribution()``.
470
- """
471
- # Canonicalize the name before searching in the list of
472
- # installed distributions and also while creating the package
473
- # dictionary to get the Distribution object
474
- req_name = canonicalize_name (req_name )
475
- packages = get_installed_distributions (
476
- local_only = False ,
477
- skip = (),
478
- include_editables = True ,
479
- editables_only = False ,
480
- user_only = False ,
481
- paths = None ,
404
+ """Return a list of installed Distribution objects.
405
+
406
+ Left for compatibility until direct pkg_resources uses are refactored out.
407
+ """
408
+ from pip ._internal .metadata import get_environment
409
+ from pip ._internal .metadata .pkg_resources import Distribution as _Dist
410
+ dists = get_environment (paths ).iter_installed_distributions (
411
+ local_only = local_only ,
412
+ skip = skip ,
413
+ include_editables = include_editables ,
414
+ editables_only = editables_only ,
415
+ user_only = user_only ,
482
416
)
483
- pkg_dict = {canonicalize_name (p .key ): p for p in packages }
484
- return pkg_dict .get (req_name )
417
+ return [cast (_Dist , dist )._dist for dist in dists ]
485
418
486
419
487
420
def get_distribution (req_name ):
@@ -490,26 +423,15 @@ def get_distribution(req_name):
490
423
491
424
This searches from *all* distributions available in the environment, to
492
425
match the behavior of ``pkg_resources.get_distribution()``.
493
- """
494
426
495
- # Search the distribution by looking through the working set
496
- dist = _search_distribution (req_name )
497
-
498
- # If distribution could not be found, call working_set.require
499
- # to update the working set, and try to find the distribution
500
- # again.
501
- # This might happen for e.g. when you install a package
502
- # twice, once using setup.py develop and again using setup.py install.
503
- # Now when run pip uninstall twice, the package gets removed
504
- # from the working set in the first uninstall, so we have to populate
505
- # the working set again so that pip knows about it and the packages
506
- # gets picked up and is successfully uninstalled the second time too.
507
- if not dist :
508
- try :
509
- pkg_resources .working_set .require (req_name )
510
- except pkg_resources .DistributionNotFound :
511
- return None
512
- return _search_distribution (req_name )
427
+ Left for compatibility until direct pkg_resources uses are refactored out.
428
+ """
429
+ from pip ._internal .metadata import get_environment
430
+ from pip ._internal .metadata .pkg_resources import Distribution as _Dist
431
+ dist = get_environment ().get_distribution (req_name )
432
+ if dist is None :
433
+ return None
434
+ return cast (_Dist , dist )._dist
513
435
514
436
515
437
def egg_link_path (dist ):
0 commit comments