Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keewenaw
GitHub Repository: keewenaw/ethereum-wallet-cracker
Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/commands/uninstall.py
4804 views
1
import logging
2
from optparse import Values
3
from typing import List
4
5
from pip._vendor.packaging.utils import canonicalize_name
6
7
from pip._internal.cli import cmdoptions
8
from pip._internal.cli.base_command import Command
9
from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root
10
from pip._internal.cli.status_codes import SUCCESS
11
from pip._internal.exceptions import InstallationError
12
from pip._internal.req import parse_requirements
13
from pip._internal.req.constructors import (
14
install_req_from_line,
15
install_req_from_parsed_requirement,
16
)
17
from pip._internal.utils.misc import protect_pip_from_modification_on_windows
18
19
logger = logging.getLogger(__name__)
20
21
22
class UninstallCommand(Command, SessionCommandMixin):
23
"""
24
Uninstall packages.
25
26
pip is able to uninstall most installed packages. Known exceptions are:
27
28
- Pure distutils packages installed with ``python setup.py install``, which
29
leave behind no metadata to determine what files were installed.
30
- Script wrappers installed by ``python setup.py develop``.
31
"""
32
33
usage = """
34
%prog [options] <package> ...
35
%prog [options] -r <requirements file> ..."""
36
37
def add_options(self) -> None:
38
self.cmd_opts.add_option(
39
"-r",
40
"--requirement",
41
dest="requirements",
42
action="append",
43
default=[],
44
metavar="file",
45
help=(
46
"Uninstall all the packages listed in the given requirements "
47
"file. This option can be used multiple times."
48
),
49
)
50
self.cmd_opts.add_option(
51
"-y",
52
"--yes",
53
dest="yes",
54
action="store_true",
55
help="Don't ask for confirmation of uninstall deletions.",
56
)
57
self.cmd_opts.add_option(cmdoptions.root_user_action())
58
self.parser.insert_option_group(0, self.cmd_opts)
59
60
def run(self, options: Values, args: List[str]) -> int:
61
session = self.get_default_session(options)
62
63
reqs_to_uninstall = {}
64
for name in args:
65
req = install_req_from_line(
66
name,
67
isolated=options.isolated_mode,
68
)
69
if req.name:
70
reqs_to_uninstall[canonicalize_name(req.name)] = req
71
else:
72
logger.warning(
73
"Invalid requirement: %r ignored -"
74
" the uninstall command expects named"
75
" requirements.",
76
name,
77
)
78
for filename in options.requirements:
79
for parsed_req in parse_requirements(
80
filename, options=options, session=session
81
):
82
req = install_req_from_parsed_requirement(
83
parsed_req, isolated=options.isolated_mode
84
)
85
if req.name:
86
reqs_to_uninstall[canonicalize_name(req.name)] = req
87
if not reqs_to_uninstall:
88
raise InstallationError(
89
f"You must give at least one requirement to {self.name} (see "
90
f'"pip help {self.name}")'
91
)
92
93
protect_pip_from_modification_on_windows(
94
modifying_pip="pip" in reqs_to_uninstall
95
)
96
97
for req in reqs_to_uninstall.values():
98
uninstall_pathset = req.uninstall(
99
auto_confirm=options.yes,
100
verbose=self.verbosity > 0,
101
)
102
if uninstall_pathset:
103
uninstall_pathset.commit()
104
if options.root_user_action == "warn":
105
warn_if_run_as_root()
106
return SUCCESS
107
108