59 |
59 |
60 if TYPE_CHECKING: |
60 if TYPE_CHECKING: |
61 from typing import Iterator, Optional, Sequence |
61 from typing import Iterator, Optional, Sequence |
62 |
62 |
63 |
63 |
64 def get_installed_distributions(local_only=True, user_only=False): |
64 def get_installed_distributions(): |
65 """ |
65 """ |
66 Function to get the installed packages via pip. |
66 Function to get the installed packages via pip. |
67 |
67 |
68 Note: importlib_metadata.distributions() does not respect |
68 @return list of installed distributions |
69 'local_only' and 'user_only' keyword parameters. |
69 @rtype list |
70 |
|
71 @param local_only DESCRIPTION (defaults to True) |
|
72 @type TYPE (optional) |
|
73 @param user_only DESCRIPTION (defaults to False) |
|
74 @type TYPE (optional) |
|
75 @return DESCRIPTION |
|
76 @rtype TYPE |
|
77 """ |
70 """ |
78 try: |
71 try: |
79 from pip._internal.metadata import get_environment |
72 from importlib import metadata as importlib_metadata |
|
73 return importlib_metadata.distributions() |
80 except ImportError: |
74 except ImportError: |
81 # For backward compatibility with pip version 20.3.4 |
75 try: |
82 from pip._internal.utils import misc |
76 from pip._internal.metadata import get_environment |
83 return misc.get_installed_distributions( |
77 except ImportError: |
84 local_only=local_only, |
78 # For backward compatibility with pip version 20.3.4 |
85 user_only=user_only |
79 from pip._internal.utils import misc |
86 ) |
80 return misc.get_installed_distributions( |
87 else: |
81 local_only=True, |
88 from pip._internal.utils.compat import stdlib_pkgs |
82 user_only=False |
89 dists = get_environment(None).iter_installed_distributions( |
83 ) |
90 local_only=local_only, |
84 else: |
91 user_only=user_only, |
85 from pip._internal.utils.compat import stdlib_pkgs |
92 skip=stdlib_pkgs, |
86 dists = get_environment(None).iter_installed_distributions( |
93 include_editables=True, |
87 local_only=True, |
94 editables_only=False, |
88 user_only=False, |
95 ) |
89 skip=stdlib_pkgs, |
96 return [d._dist for d in dists] |
90 include_editables=True, |
|
91 editables_only=False, |
|
92 ) |
|
93 return [d._dist for d in dists] |
97 |
94 |
98 |
95 |
99 __pkgname__ = "pip-licenses" |
96 __pkgname__ = "pip-licenses" |
100 __version__ = "4.1.0" |
97 __version__ = "4.1.0" |
101 __author__ = "raimon" |
98 __author__ = "raimon" |
231 else: |
228 else: |
232 pkg_info[k] = filter_string(cast(str, pkg_info[k])) |
229 pkg_info[k] = filter_string(cast(str, pkg_info[k])) |
233 |
230 |
234 return pkg_info |
231 return pkg_info |
235 |
232 |
236 pkgs = get_installed_distributions( |
233 pkgs = get_installed_distributions() |
237 local_only=args.local_only, |
|
238 user_only=args.user_only, |
|
239 ) |
|
240 ignore_pkgs_as_lower = [pkg.lower() for pkg in args.ignore_packages] |
234 ignore_pkgs_as_lower = [pkg.lower() for pkg in args.ignore_packages] |
241 pkgs_as_lower = [pkg.lower() for pkg in args.packages] |
235 pkgs_as_lower = [pkg.lower() for pkg in args.packages] |
242 |
236 |
243 fail_on_licenses = set() |
237 fail_on_licenses = set() |
244 if args.fail_on: |
238 if args.fail_on: |