|
20 | 20 | from pip._vendor import pkg_resources
|
21 | 21 | # NOTE: retrying is not annotated in typeshed as on 2017-07-17, which is
|
22 | 22 | # why we ignore the type on this import.
|
| 23 | +from pip._vendor.packaging.utils import canonicalize_name |
23 | 24 | from pip._vendor.retrying import retry # type: ignore
|
24 | 25 | from pip._vendor.six import PY2, text_type
|
25 | 26 | from pip._vendor.six.moves import input, map, zip_longest
|
@@ -480,6 +481,40 @@ def user_test(d):
|
480 | 481 | ]
|
481 | 482 |
|
482 | 483 |
|
| 484 | +def search_distribution(req_name): |
| 485 | + |
| 486 | + # Canonicalize the name before searching in the list of |
| 487 | + # installed distributions and also while creating the package |
| 488 | + # dictionary to get the Distribution object |
| 489 | + req_name = canonicalize_name(req_name) |
| 490 | + packages = get_installed_distributions(skip=()) |
| 491 | + pkg_dict = {canonicalize_name(p.key): p for p in packages} |
| 492 | + return pkg_dict.get(req_name) |
| 493 | + |
| 494 | + |
| 495 | +def get_distribution(req_name): |
| 496 | + """Given a requirement name, return the installed Distribution object""" |
| 497 | + |
| 498 | + # Search the distribution by looking through the working set |
| 499 | + dist = search_distribution(req_name) |
| 500 | + |
| 501 | + # If distribution could not be found, call working_set.require |
| 502 | + # to update the working set, and try to find the distribution |
| 503 | + # again. |
| 504 | + # This might happen for e.g. when you install a package |
| 505 | + # twice, once using setup.py develop and again using setup.py install. |
| 506 | + # Now when run pip uninstall twice, the package gets removed |
| 507 | + # from the working set in the first uninstall, so we have to populate |
| 508 | + # the working set again so that pip knows about it and the packages |
| 509 | + # gets picked up and is successfully uninstalled the second time too. |
| 510 | + if not dist: |
| 511 | + try: |
| 512 | + pkg_resources.working_set.require(req_name) |
| 513 | + except pkg_resources.DistributionNotFound: |
| 514 | + return None |
| 515 | + return search_distribution(req_name) |
| 516 | + |
| 517 | + |
483 | 518 | def egg_link_path(dist):
|
484 | 519 | # type: (Distribution) -> Optional[str]
|
485 | 520 | """
|
|
0 commit comments