|
4 | 4 | import logging
|
5 | 5 | import os
|
6 | 6 | import re
|
| 7 | +from typing import Container, Optional, List, cast |
7 | 8 |
|
8 | 9 | from packaging.utils import canonicalize_name
|
9 | 10 | from packaging.markers import Marker
|
|
13 | 14 | except ImportError: # pragma: no cover
|
14 | 15 | from pip._internal.download import PipSession
|
15 | 16 | from pip._internal.req.req_file import parse_requirements
|
| 17 | +from pip._internal.utils.compat import stdlib_pkgs |
| 18 | +from pip._internal.metadata import get_default_environment, get_environment |
| 19 | +from pip._internal.metadata.pkg_resources import Distribution as _Dist |
| 20 | +from pip._vendor.pkg_resources import Distribution |
16 | 21 |
|
17 | 22 | log = logging.getLogger(__name__)
|
18 | 23 |
|
19 | 24 |
|
| 25 | +# get_installed_distributions was removed in pip 21.3. |
| 26 | +# This is a copy from pip. |
| 27 | +# See |
| 28 | +# https://github.com/pypa/pip/commit/d051a00fc57037104fca85ad8ebf2cdbd1e32d24#diff-058e40cb3a9ea705f655937e48f3a053f5dc7c500b7f1b2aae76e9bd673faf64. |
| 29 | +# |
| 30 | +# This is mocked in all tests (unfortunately) and so we do not cover this |
| 31 | +# function. |
| 32 | +def get_installed_distributions( |
| 33 | + local_only: bool = True, |
| 34 | + skip: Container[str] = stdlib_pkgs, |
| 35 | + include_editables: bool = True, |
| 36 | + editables_only: bool = False, |
| 37 | + user_only: bool = False, |
| 38 | + paths: Optional[List[str]] = None, |
| 39 | + ) -> List[Distribution]: # pragma: no cover |
| 40 | + """Return a list of installed Distribution objects. |
| 41 | +
|
| 42 | + Left for compatibility until direct pkg_resources uses are refactored out. |
| 43 | + """ |
| 44 | + if paths is None: |
| 45 | + env = get_default_environment() |
| 46 | + else: |
| 47 | + env = get_environment(paths) |
| 48 | + |
| 49 | + dists = env.iter_installed_distributions( |
| 50 | + local_only=local_only, |
| 51 | + skip=skip, |
| 52 | + include_editables=include_editables, |
| 53 | + editables_only=editables_only, |
| 54 | + user_only=user_only, |
| 55 | + ) |
| 56 | + return [cast(_Dist, dist)._dist for dist in dists] |
| 57 | + |
| 58 | + |
20 | 59 | class FoundModule:
|
21 | 60 | def __init__(self, modname, filename, locations=None):
|
22 | 61 | self.modname = modname
|
|
0 commit comments