Skip to content

Commit

Permalink
Merge pull request #10585 from pradyunsg/fix-config-paths
Browse files Browse the repository at this point in the history
Rework MacOS configuration paths, to match documentation
  • Loading branch information
pradyunsg committed Oct 22, 2021
1 parent 8c1f333 commit 457564c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
7 changes: 3 additions & 4 deletions docs/html/topics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions news/10585.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Restore compatibility of where configuration files are loaded from on MacOS (back to ``Library/Application Support/pip``, instead of ``Preferences/pip``).
38 changes: 25 additions & 13 deletions src/pip/_internal/utils/appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/pypa/pip/issues/1733>
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"]
11 changes: 8 additions & 3 deletions tests/unit/test_appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,17 @@ 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",
]

@pytest.mark.skipif(sys.platform != "linux", reason="Linux-only test")
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(
Expand All @@ -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:
Expand Down

0 comments on commit 457564c

Please sign in to comment.