Skip to content

Commit 05a953a

Browse files
committed
Factor out filename matching logic
1 parent c25cdda commit 05a953a

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

pip/commands/cache.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,40 @@ def run(self, options, args):
6262
method = getattr(self, "action_%s" % args[0])
6363
return method(options, args[1:])
6464

65-
def get_cache_location(self, cache_root, cache_type):
65+
@staticmethod
66+
def get_cache_location(cache_root, cache_type):
6667
location = cache_root
6768
suffix = {"wheel": "wheels", "http": "http"}
6869
if cache_type != "all":
6970
location = os.path.join(location, suffix[cache_type])
7071
return location
7172

73+
@staticmethod
74+
def wheels_matching(cache_location, pattern):
75+
"""Returns a list of absolute filenames of wheels with filenames
76+
matching `pattern`. A pattern may be:
77+
* the name of a package
78+
* a shell glob expression matching the basename of the wheel
79+
* an exact basename
80+
"""
81+
shell_metachars = '*?'
82+
if (any(m in pattern for m in shell_metachars) or
83+
pattern.endswith(".whl")):
84+
matches = find_files(cache_location, pattern)
85+
matches = fnmatch.filter(matches, "*.whl")
86+
else:
87+
wheels = find_files(cache_location, "*.whl")
88+
pkgname = safe_name(pattern).lower()
89+
matches = []
90+
for filename in wheels:
91+
try:
92+
wheel = Wheel(basename(filename))
93+
except InvalidWheelFilename:
94+
continue
95+
if wheel.name.lower() == pkgname:
96+
matches.append(filename)
97+
return matches
98+
7299
def action_info(self, options, args):
73100
caches = ["http", "wheel"] if options.type == "all" else [options.type]
74101
result = []
@@ -108,26 +135,10 @@ def action_rm(self, options, args):
108135
"Must specify the filename of (a) wheel(s) to remove.")
109136
cache_location = self.get_cache_location(options.cache_dir, "wheel")
110137
value = SUCCESS
111-
shell_metachars = '*?'
112-
for target in args:
113-
if (any(m in target for m in shell_metachars) or
114-
target.endswith(".whl")):
115-
matches = find_files(cache_location, target)
116-
matches = fnmatch.filter(matches, "*.whl")
117-
else:
118-
wheels = find_files(cache_location, "*.whl")
119-
pkgname = safe_name(target).lower()
120-
matches = []
121-
for filename in wheels:
122-
try:
123-
wheel = Wheel(basename(filename))
124-
except InvalidWheelFilename:
125-
continue
126-
if wheel.name.lower() == pkgname:
127-
matches.append(filename)
128-
138+
for pattern in args:
139+
matches = self.wheels_matching(cache_location, pattern)
129140
if not matches:
130-
logger.info("No match found for %s" % target)
141+
logger.info("No match found for %s" % pattern)
131142
continue
132143
for match in matches:
133144
try:

0 commit comments

Comments
 (0)