Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/pip/_internal/commands/cache.py
811 views
1
from __future__ import absolute_import
2
3
import logging
4
import os
5
import textwrap
6
7
import pip._internal.utils.filesystem as filesystem
8
from pip._internal.cli.base_command import Command
9
from pip._internal.cli.status_codes import ERROR, SUCCESS
10
from pip._internal.exceptions import CommandError, PipError
11
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
12
13
if MYPY_CHECK_RUNNING:
14
from optparse import Values
15
from typing import Any, List
16
17
18
logger = logging.getLogger(__name__)
19
20
21
class CacheCommand(Command):
22
"""
23
Inspect and manage pip's wheel cache.
24
25
Subcommands:
26
27
dir: Show the cache directory.
28
info: Show information about the cache.
29
list: List filenames of packages stored in the cache.
30
remove: Remove one or more package from the cache.
31
purge: Remove all items from the cache.
32
33
<pattern> can be a glob expression or a package name.
34
"""
35
36
ignore_require_venv = True
37
usage = """
38
%prog dir
39
%prog info
40
%prog list [<pattern>]
41
%prog remove <pattern>
42
%prog purge
43
"""
44
45
def run(self, options, args):
46
# type: (Values, List[Any]) -> int
47
handlers = {
48
"dir": self.get_cache_dir,
49
"info": self.get_cache_info,
50
"list": self.list_cache_items,
51
"remove": self.remove_cache_items,
52
"purge": self.purge_cache,
53
}
54
55
if not options.cache_dir:
56
logger.error("pip cache commands can not "
57
"function since cache is disabled.")
58
return ERROR
59
60
# Determine action
61
if not args or args[0] not in handlers:
62
logger.error("Need an action ({}) to perform.".format(
63
", ".join(sorted(handlers)))
64
)
65
return ERROR
66
67
action = args[0]
68
69
# Error handling happens here, not in the action-handlers.
70
try:
71
handlers[action](options, args[1:])
72
except PipError as e:
73
logger.error(e.args[0])
74
return ERROR
75
76
return SUCCESS
77
78
def get_cache_dir(self, options, args):
79
# type: (Values, List[Any]) -> None
80
if args:
81
raise CommandError('Too many arguments')
82
83
logger.info(options.cache_dir)
84
85
def get_cache_info(self, options, args):
86
# type: (Values, List[Any]) -> None
87
if args:
88
raise CommandError('Too many arguments')
89
90
num_packages = len(self._find_wheels(options, '*'))
91
92
cache_location = self._wheels_cache_dir(options)
93
cache_size = filesystem.format_directory_size(cache_location)
94
95
message = textwrap.dedent("""
96
Location: {location}
97
Size: {size}
98
Number of wheels: {package_count}
99
""").format(
100
location=cache_location,
101
package_count=num_packages,
102
size=cache_size,
103
).strip()
104
105
logger.info(message)
106
107
def list_cache_items(self, options, args):
108
# type: (Values, List[Any]) -> None
109
if len(args) > 1:
110
raise CommandError('Too many arguments')
111
112
if args:
113
pattern = args[0]
114
else:
115
pattern = '*'
116
117
files = self._find_wheels(options, pattern)
118
119
if not files:
120
logger.info('Nothing cached.')
121
return
122
123
results = []
124
for filename in files:
125
wheel = os.path.basename(filename)
126
size = filesystem.format_file_size(filename)
127
results.append(' - {} ({})'.format(wheel, size))
128
logger.info('Cache contents:\n')
129
logger.info('\n'.join(sorted(results)))
130
131
def remove_cache_items(self, options, args):
132
# type: (Values, List[Any]) -> None
133
if len(args) > 1:
134
raise CommandError('Too many arguments')
135
136
if not args:
137
raise CommandError('Please provide a pattern')
138
139
files = self._find_wheels(options, args[0])
140
if not files:
141
raise CommandError('No matching packages')
142
143
for filename in files:
144
os.unlink(filename)
145
logger.debug('Removed %s', filename)
146
logger.info('Files removed: %s', len(files))
147
148
def purge_cache(self, options, args):
149
# type: (Values, List[Any]) -> None
150
if args:
151
raise CommandError('Too many arguments')
152
153
return self.remove_cache_items(options, ['*'])
154
155
def _wheels_cache_dir(self, options):
156
# type: (Values) -> str
157
return os.path.join(options.cache_dir, 'wheels')
158
159
def _find_wheels(self, options, pattern):
160
# type: (Values, str) -> List[str]
161
wheel_dir = self._wheels_cache_dir(options)
162
163
# The wheel filename format, as specified in PEP 427, is:
164
# {distribution}-{version}(-{build})?-{python}-{abi}-{platform}.whl
165
#
166
# Additionally, non-alphanumeric values in the distribution are
167
# normalized to underscores (_), meaning hyphens can never occur
168
# before `-{version}`.
169
#
170
# Given that information:
171
# - If the pattern we're given contains a hyphen (-), the user is
172
# providing at least the version. Thus, we can just append `*.whl`
173
# to match the rest of it.
174
# - If the pattern we're given doesn't contain a hyphen (-), the
175
# user is only providing the name. Thus, we append `-*.whl` to
176
# match the hyphen before the version, followed by anything else.
177
#
178
# PEP 427: https://www.python.org/dev/peps/pep-0427/
179
pattern = pattern + ("*.whl" if "-" in pattern else "-*.whl")
180
181
return filesystem.find_files(wheel_dir, pattern)
182
183