1
1
import abc
2
2
3
+ from pip ._internal .utils .misc import stdlib_pkgs # TODO: Move definition here.
3
4
from pip ._internal .utils .typing import MYPY_CHECK_RUNNING
4
5
5
6
if MYPY_CHECK_RUNNING :
6
- from typing import List , Optional
7
+ from typing import Container , Iterator , List , Optional
7
8
8
9
9
10
class BaseDistribution (metadata = abc .ABCMeta ):
11
+ @property
12
+ def canonical_name (self ):
13
+ # type: () -> str
14
+ raise NotImplementedError ()
15
+
10
16
@property
11
17
def installer (self ):
12
18
# type: () -> str
13
19
raise NotImplementedError ()
14
20
21
+ @property
22
+ def editable (self ):
23
+ # type: () -> bool
24
+ raise NotImplementedError ()
25
+
26
+ @property
27
+ def local (self ):
28
+ # type: () -> bool
29
+ raise NotImplementedError ()
30
+
31
+ @property
32
+ def in_usersite (self ):
33
+ # type: () -> bool
34
+ raise NotImplementedError ()
35
+
15
36
16
37
class BaseEnvironment (metadata = abc .ABCMeta ):
17
38
"""An environment containing distributions to introspect.
@@ -29,3 +50,27 @@ def from_paths(cls, paths):
29
50
def get_distribution (self , name ):
30
51
# type: (str) -> Optional[BaseDistribution]
31
52
raise NotImplementedError ()
53
+
54
+ def iter_distributions (self ):
55
+ # type: () -> Iterator[BaseDistribution]
56
+ raise NotImplementedError ()
57
+
58
+ def iter_installed_distributions (
59
+ self ,
60
+ local_only = True , # type: bool
61
+ skip = stdlib_pkgs , # type: Container[str]
62
+ include_editables = True , # type: bool
63
+ editables_only = False , # type: bool
64
+ user_only = False , # type: bool
65
+ ):
66
+ # type: (...) -> Iterator[BaseDistribution]
67
+ it = self .iter_distributions ()
68
+ if local_only :
69
+ it = (d for d in it if d .local )
70
+ if not include_editables :
71
+ it = (d for d in it if not d .editable )
72
+ if editables_only :
73
+ it = (d for d in it if d .editable )
74
+ if user_only :
75
+ it = (d for d in it if d .in_usersite )
76
+ return (d for d in it if d .canonical_name not in skip )
0 commit comments