Skip to content

Commit 267fdf8

Browse files
committed
Rework MacOS and Linux configuration paths, to match documentation
This brings us in line with pre-platformdirs behaviours on these, following the details of the configuration paths, as documented in pip's own documentation.
1 parent 75c11c4 commit 267fdf8

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/pip/_internal/utils/appdirs.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,36 @@ def user_cache_dir(appname: str) -> str:
1717
return _appdirs.user_cache_dir(appname, appauthor=False)
1818

1919

20+
def _macos_user_config_dir(appname: str, roaming: bool = True) -> str:
21+
# Use ~/Application Support/pip, if the directory exists.
22+
path = _appdirs.user_data_dir(appname, appauthor=False, roaming=roaming)
23+
if os.path.isdir(path):
24+
return path
25+
26+
# Use a Linux-like ~/.config/pip, by default.
27+
linux_like_path = "~/.config/"
28+
if appname:
29+
linux_like_path = os.path.join(linux_like_path, appname)
30+
31+
return os.path.expanduser(linux_like_path)
32+
33+
2034
def user_config_dir(appname: str, roaming: bool = True) -> str:
21-
path = _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming)
22-
if sys.platform == "darwin" and not os.path.isdir(path):
23-
path = os.path.expanduser("~/.config/")
24-
if appname:
25-
path = os.path.join(path, appname)
26-
return path
35+
if sys.platform == "darwin":
36+
return _macos_user_config_dir(appname, roaming)
37+
38+
return _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming)
2739

2840

2941
# for the discussion regarding site_config_dir locations
3042
# see <https://github.com/pypa/pip/issues/1733>
3143
def site_config_dirs(appname: str) -> List[str]:
32-
dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True)
3344
if sys.platform == "darwin":
34-
# always look in /Library/Application Support/pip as well
35-
return dirval.split(os.pathsep) + ["/Library/Application Support/pip"]
36-
elif sys.platform == "win32":
45+
return [_appdirs.site_data_dir(appname, appauthor=False, multipath=True)]
46+
47+
dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True)
48+
if sys.platform == "win32":
3749
return [dirval]
38-
else:
39-
# always look in /etc directly as well
40-
return dirval.split(os.pathsep) + ["/etc"]
50+
51+
# Unix-y system. Look in /etc as well.
52+
return ["/etc"] + dirval.split(os.pathsep)

tests/unit/test_appdirs.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ def test_site_config_dirs_osx(self, monkeypatch: pytest.MonkeyPatch) -> None:
9999
monkeypatch.setenv("HOME", "/home/test")
100100

101101
assert appdirs.site_config_dirs("pip") == [
102-
"/Library/Preferences/pip",
103102
"/Library/Application Support/pip",
104103
]
105104

106105
@pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test")
107106
def test_site_config_dirs_linux(self, monkeypatch: pytest.MonkeyPatch) -> None:
108107
monkeypatch.delenv("XDG_CONFIG_DIRS", raising=False)
109108

110-
assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"]
109+
assert appdirs.site_config_dirs("pip") == [
110+
"/etc",
111+
"/etc/xdg/pip",
112+
]
111113

112114
@pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test")
113115
def test_site_config_dirs_linux_override(
@@ -117,10 +119,10 @@ def test_site_config_dirs_linux_override(
117119
monkeypatch.setenv("XDG_CONFIG_DIRS", "/spam:/etc:/etc/xdg")
118120

119121
assert appdirs.site_config_dirs("pip") == [
122+
"/etc",
120123
"/spam/pip",
121124
"/etc/pip",
122125
"/etc/xdg/pip",
123-
"/etc",
124126
]
125127

126128
@pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test")
@@ -129,7 +131,10 @@ def test_site_config_dirs_linux_empty(
129131
) -> None:
130132
monkeypatch.setattr(os, "pathsep", ":")
131133
monkeypatch.setenv("XDG_CONFIG_DIRS", "")
132-
assert appdirs.site_config_dirs("pip") == ["/etc/xdg/pip", "/etc"]
134+
assert appdirs.site_config_dirs("pip") == [
135+
"/etc",
136+
"/etc/xdg/pip",
137+
]
133138

134139

135140
class TestUserConfigDir:

0 commit comments

Comments
 (0)