From 457564cf38ad5da75672d4de119e4c5ae19fbd56 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Mon, 18 Oct 2021 07:37:43 +0100 Subject: [PATCH] Merge pull request #10585 from pradyunsg/fix-config-paths Rework MacOS configuration paths, to match documentation --- docs/html/topics/configuration.md | 7 +++--- news/10585.bugfix.rst | 1 + src/pip/_internal/utils/appdirs.py | 38 ++++++++++++++++++++---------- tests/unit/test_appdirs.py | 11 ++++++--- 4 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 news/10585.bugfix.rst diff --git a/docs/html/topics/configuration.md b/docs/html/topics/configuration.md index eb392ecd493..2d63ac1413f 100644 --- a/docs/html/topics/configuration.md +++ b/docs/html/topics/configuration.md @@ -29,11 +29,10 @@ complexity for backwards compatibility reasons. ```{tab} Unix Global -: {file}`/etc/pip.conf` +: In a "pip" subdirectory of any of the paths set in the environment variable + `XDG_CONFIG_DIRS` (if it exists), for example {file}`/etc/xdg/pip/pip.conf`. - Alternatively, it may be in a "pip" subdirectory of any of the paths set - in the environment variable `XDG_CONFIG_DIRS` (if it exists), for - example {file}`/etc/xdg/pip/pip.conf`. + This will be followed by loading {file}`/etc/pip.conf`. User : {file}`$HOME/.config/pip/pip.conf`, which respects the `XDG_CONFIG_HOME` environment variable. diff --git a/news/10585.bugfix.rst b/news/10585.bugfix.rst new file mode 100644 index 00000000000..9e78a4609cf --- /dev/null +++ b/news/10585.bugfix.rst @@ -0,0 +1 @@ +Restore compatibility of where configuration files are loaded from on MacOS (back to ``Library/Application Support/pip``, instead of ``Preferences/pip``). diff --git a/src/pip/_internal/utils/appdirs.py b/src/pip/_internal/utils/appdirs.py index 5e334126b61..16933bf8afe 100644 --- a/src/pip/_internal/utils/appdirs.py +++ b/src/pip/_internal/utils/appdirs.py @@ -17,24 +17,36 @@ def user_cache_dir(appname: str) -> str: return _appdirs.user_cache_dir(appname, appauthor=False) +def _macos_user_config_dir(appname: str, roaming: bool = True) -> str: + # Use ~/Application Support/pip, if the directory exists. + path = _appdirs.user_data_dir(appname, appauthor=False, roaming=roaming) + if os.path.isdir(path): + return path + + # Use a Linux-like ~/.config/pip, by default. + linux_like_path = "~/.config/" + if appname: + linux_like_path = os.path.join(linux_like_path, appname) + + return os.path.expanduser(linux_like_path) + + def user_config_dir(appname: str, roaming: bool = True) -> str: - path = _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming) - if sys.platform == "darwin" and not os.path.isdir(path): - path = os.path.expanduser("~/.config/") - if appname: - path = os.path.join(path, appname) - return path + if sys.platform == "darwin": + return _macos_user_config_dir(appname, roaming) + + return _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming) # for the discussion regarding site_config_dir locations # see def site_config_dirs(appname: str) -> List[str]: - dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True) if sys.platform == "darwin": - # always look in /Library/Application Support/pip as well - return dirval.split(os.pathsep) + ["/Library/Application Support/pip"] - elif sys.platform == "win32": + return [_appdirs.site_data_dir(appname, appauthor=False, multipath=True)] + + dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True) + if sys.platform == "win32": return [dirval] - else: - # always look in /etc directly as well - return dirval.split(os.pathsep) + ["/etc"] + + # Unix-y system. Look in /etc as well. + return dirval.split(os.pathsep) + ["/etc"] diff --git a/tests/unit/test_appdirs.py b/tests/unit/test_appdirs.py index e59931b6652..cdb15e7a1a3 100644 --- a/tests/unit/test_appdirs.py +++ b/tests/unit/test_appdirs.py @@ -99,7 +99,6 @@ def test_site_config_dirs_osx(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("HOME", "/home/test") assert appdirs.site_config_dirs("pip") == [ - "/Library/Preferences/pip", "/Library/Application Support/pip", ] @@ -107,7 +106,10 @@ def test_site_config_dirs_osx(self, monkeypatch: pytest.MonkeyPatch) -> None: def test_site_config_dirs_linux(self, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("XDG_CONFIG_DIRS", raising=False) - assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"] + assert appdirs.site_config_dirs("pip") == [ + "/etc/xdg/pip", + "/etc", + ] @pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test") def test_site_config_dirs_linux_override( @@ -129,7 +131,10 @@ def test_site_config_dirs_linux_empty( ) -> None: monkeypatch.setattr(os, "pathsep", ":") monkeypatch.setenv("XDG_CONFIG_DIRS", "") - assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"] + assert appdirs.site_config_dirs("pip") == [ + "/etc/xdg/pip", + "/etc", + ] class TestUserConfigDir: