Skip to content

Commit 1ee270a

Browse files
committed
Check that the cache is writable in _main()
This avoid code duplication (for the wheel and http cache) and repeated warnings.
1 parent 865539b commit 1ee270a

File tree

6 files changed

+18
-38
lines changed

6 files changed

+18
-38
lines changed

news/7488.bugfix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Effectively disable the wheel cache when it is not writable, as is the
2+
case with the http cache.

src/pip/_internal/cli/base_command.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
UninstallationError,
3232
)
3333
from pip._internal.utils.deprecation import deprecated
34+
from pip._internal.utils.filesystem import check_path_owner
3435
from pip._internal.utils.logging import BrokenStdoutLoggingError, setup_logging
3536
from pip._internal.utils.misc import get_prog
3637
from pip._internal.utils.temp_dir import global_tempdir_manager
@@ -168,6 +169,18 @@ def _main(self, args):
168169
)
169170
sys.exit(VIRTUALENV_NOT_FOUND)
170171

172+
if options.cache_dir:
173+
if not check_path_owner(options.cache_dir):
174+
logger.warning(
175+
"The directory '%s' or its parent directory is not owned "
176+
"or is not writable by the current user. The cache "
177+
"has been disabled. Check the permissions and owner of "
178+
"that directory. If executing pip with sudo, you may want "
179+
"sudo's -H flag.",
180+
options.cache_dir,
181+
)
182+
options.cache_dir = None
183+
171184
try:
172185
status = self.run(options, args)
173186
# FIXME: all commands should return an exit status

src/pip/_internal/commands/download.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pip._internal.cli.req_command import RequirementCommand
1212
from pip._internal.req import RequirementSet
1313
from pip._internal.req.req_tracker import get_requirement_tracker
14-
from pip._internal.utils.filesystem import check_path_owner
1514
from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
1615
from pip._internal.utils.temp_dir import TempDirectory
1716

@@ -99,16 +98,6 @@ def run(self, options, args):
9998
target_python=target_python,
10099
)
101100
build_delete = (not (options.no_clean or options.build_dir))
102-
if options.cache_dir and not check_path_owner(options.cache_dir):
103-
logger.warning(
104-
"The directory '%s' or its parent directory is not owned "
105-
"by the current user and caching wheels has been "
106-
"disabled. check the permissions and owner of that "
107-
"directory. If executing pip with sudo, you may want "
108-
"sudo's -H flag.",
109-
options.cache_dir,
110-
)
111-
options.cache_dir = None
112101

113102
with get_requirement_tracker() as req_tracker, TempDirectory(
114103
options.build_dir, delete=build_delete, kind="download"

src/pip/_internal/commands/install.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from pip._internal.req.req_tracker import get_requirement_tracker
3535
from pip._internal.utils.deprecation import deprecated
3636
from pip._internal.utils.distutils_args import parse_distutils_args
37-
from pip._internal.utils.filesystem import check_path_owner, test_writable_dir
37+
from pip._internal.utils.filesystem import test_writable_dir
3838
from pip._internal.utils.misc import (
3939
ensure_dir,
4040
get_installed_version,
@@ -330,17 +330,6 @@ def run(self, options, args):
330330
build_delete = (not (options.no_clean or options.build_dir))
331331
wheel_cache = WheelCache(options.cache_dir, options.format_control)
332332

333-
if options.cache_dir and not check_path_owner(options.cache_dir):
334-
logger.warning(
335-
"The directory '%s' or its parent directory is not owned "
336-
"by the current user and caching wheels has been "
337-
"disabled. check the permissions and owner of that "
338-
"directory. If executing pip with sudo, you may want "
339-
"sudo's -H flag.",
340-
options.cache_dir,
341-
)
342-
options.cache_dir = None
343-
344333
with get_requirement_tracker() as req_tracker, TempDirectory(
345334
options.build_dir, delete=build_delete, kind="install"
346335
) as directory:

src/pip/_internal/network/session.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from pip._internal.network.cache import SafeFileCache
2828
# Import ssl from compat so the initial import occurs in only one place.
2929
from pip._internal.utils.compat import has_tls, ipaddress
30-
from pip._internal.utils.filesystem import check_path_owner
3130
from pip._internal.utils.glibc import libc_ver
3231
from pip._internal.utils.misc import (
3332
build_url_from_netloc,
@@ -264,19 +263,6 @@ def __init__(self, *args, **kwargs):
264263
backoff_factor=0.25,
265264
)
266265

267-
# Check to ensure that the directory containing our cache directory
268-
# is owned by the user current executing pip. If it does not exist
269-
# we will check the parent directory until we find one that does exist.
270-
if cache and not check_path_owner(cache):
271-
logger.warning(
272-
"The directory '%s' or its parent directory is not owned by "
273-
"the current user and the cache has been disabled. Please "
274-
"check the permissions and owner of that directory. If "
275-
"executing pip with sudo, you may want sudo's -H flag.",
276-
cache,
277-
)
278-
cache = None
279-
280266
# We want to _only_ cache responses on securely fetched origins. We do
281267
# this because we can't validate the response of an insecurely fetched
282268
# origin, and we don't want someone to be able to poison the cache and

tests/functional/test_wheel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def test_pip_wheel_builds_when_no_binary_set(script, data):
120120
assert "Building wheel for simple" in str(res), str(res)
121121

122122

123+
@pytest.mark.skipif("sys.platform == 'win32'")
123124
def test_pip_wheel_readonly_cache(script, data, tmpdir):
124125
cache_dir = tmpdir / "cache"
125126
cache_dir.mkdir()
@@ -133,7 +134,7 @@ def test_pip_wheel_readonly_cache(script, data, tmpdir):
133134
allow_stderr_warning=True,
134135
)
135136
assert res.returncode == 0
136-
assert "caching wheels has been disabled" in str(res), str(res)
137+
assert "The cache has been disabled." in str(res), str(res)
137138

138139

139140
def test_pip_wheel_builds_editable_deps(script, data):

0 commit comments

Comments
 (0)