From 6ca58a901f8350f0902a6c2a6b3e23aa62ba84c3 Mon Sep 17 00:00:00 2001 From: Axel H Date: Sun, 26 Jun 2022 19:42:52 +0200 Subject: [PATCH] fix(config): use platform standard directories for all PDM directories Fixes #1150 --- docs/docs/configuration.md | 4 ++-- docs/docs/usage/project.md | 16 +++++++++++----- news/1161.fix.md | 1 + pdm/cli/completions/pdm.ps1 | 9 +++++---- pdm/project/config.py | 2 +- pdm/project/core.py | 3 ++- 6 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 news/1161.fix.md diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 2d67a4d371..77d05bb898 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -8,8 +8,8 @@ The following configuration items can be retrieved and modified by [`pdm config` | `cache_dir` | The root directory of cached files | The default cache location on OS | No | | | `check_update` | Check if there is any newer version available | True | No | | | `global_project.fallback` | Use the global project implicitly if no local project is found | `False` | No | | -| `global_project.path` | The path to the global project | `~/.pdm/global-project` | No | | -| `global_project.user_site` | Whether to install to user site | `False` | No | | +| `global_project.path` | The path to the global project | `/global-project` | No | | +| `global_project.user_site` | Whether to install to user site | `False` | No | | | `install.cache` | Enable caching of wheel installations | False | Yes | | | `install.cache_method` | Specify how to create links to the caches(`symlink` or `pth`) | `symlink` | Yes | | | `install.parallel` | Whether to perform installation and uninstallation in parallel | `True` | Yes | `PDM_PARALLEL_INSTALL` | diff --git a/docs/docs/usage/project.md b/docs/docs/usage/project.md index a8d6265a37..9acc565e1f 100644 --- a/docs/docs/usage/project.md +++ b/docs/docs/usage/project.md @@ -91,9 +91,15 @@ Any local configurations will be stored in `.pdm.toml` under the project root di The configuration files are searched in the following order: 1. `/.pdm.toml` - The project configuration -2. `~/.pdm/config.toml` - The home configuration +2. `/config.toml` - The home configuration -If `-g/--global` option is used, the first item will be replaced by `~/.pdm/global-project/.pdm.toml`. +where `` is: + +- `$XDG_CONFIG_HOME/pdm` (`~/.config/pdm` in most cases) on Linux as defined by [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) +- `~/Library/Preferences/pdm` on MacOS as defined by [Appli File System Basics](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html) +- `%USERPROFILE%\AppData\Local\pdm` on Windows as defined in [Known folders](https://docs.microsoft.com/en-us/windows/win32/shell/known-folders) + +If `-g/--global` option is used, the first item will be replaced by `/global-project/.pdm.toml`. You can find all available configuration items in [Configuration Page](../configuration.md). @@ -122,7 +128,7 @@ See all supported options by typing `pdm publish --help`. ### Configure the repository secrets for upload -When using the [`pdm publish`](cli_reference.md#exec-0--publish) command, it reads the repository secrets from the *global* config file(`~/.pdm/config.toml`). The content of the config is as follows: +When using the [`pdm publish`](cli_reference.md#exec-0--publish) command, it reads the repository secrets from the *global* config file(`/config.toml`). The content of the config is as follows: ```toml [repository.pypi] @@ -169,7 +175,7 @@ The caches are located under `$(pdm config cache_dir)/packages`. One can view th Sometimes users may want to keep track of the dependencies of global Python interpreter as well. It is easy to do so with PDM, via `-g/--global` option which is supported by most subcommands. -If the option is passed, `~/.pdm/global-project` will be used as the project directory, which is +If the option is passed, `/global-project` will be used as the project directory, which is almost the same as normal project except that `pyproject.toml` will be created automatically for you and it doesn't support build features. The idea is taken from Haskell's [stack](https://docs.haskellstack.org). @@ -177,7 +183,7 @@ However, unlike `stack`, by default, PDM won't use global project automatically Users should pass `-g/--global` explicitly to activate it, since it is not very pleasing if packages go to a wrong place. But PDM also leave the decision to users, just set the config `global_project.fallback` to `true`. -If you want global project to track another project file other than `~/.pdm/global-project`, you can provide the +If you want global project to track another project file other than `/global-project`, you can provide the project path via `-p/--project ` option. !!! attention "CAUTION" diff --git a/news/1161.fix.md b/news/1161.fix.md new file mode 100644 index 0000000000..8aa1991392 --- /dev/null +++ b/news/1161.fix.md @@ -0,0 +1 @@ +Ensure all app/user path related are computed according to platform standards. diff --git a/pdm/cli/completions/pdm.ps1 b/pdm/cli/completions/pdm.ps1 index 59093bef5d..44b90bd8b7 100644 --- a/pdm/cli/completions/pdm.ps1 +++ b/pdm/cli/completions/pdm.ps1 @@ -6,6 +6,7 @@ if ((Test-Path Function:\TabExpansion) -and -not (Test-Path Function:\_pdm_compl $PDM_PYTHON = "%{python_executable}" $PDM_PIP_INDEX = (& $PDM_PYTHON -m pdm config pypi.url).Trim() +$CONFIG_DIR = "$env:LOCALAPPDATA\pdm" class Option { [string[]] $Opts @@ -110,14 +111,14 @@ function getSections() { } function _fetchPackageListFromPyPI() { - if (-not (Test-Path -Path "~/.pdm")) { - mkdir "~/.pdm" + if (-not (Test-Path -Path $CONFIG_DIR)) { + mkdir $CONFIG_DIR } - (Invoke-WebRequest $PDM_PIP_INDEX).Links | ForEach-Object { $_.innerText } | Out-File -FilePath "~/.pdm/.pypiPackages" + (Invoke-WebRequest $PDM_PIP_INDEX).Links | ForEach-Object { $_.innerText } | Out-File -FilePath "$CONFIG_DIR\.pypiPackages" } function getPyPIPackages() { - # $cacheFile = "~/.pdm/.pypiPackages" + # $cacheFile = "$CONFIG_DIR\.pypiPackages" # if (-not (Test-Path -Path $cacheFile) -or (Get-Item $cacheFile).LastWriteTime -lt (Get-Date).AddDays(-28)) { # _fetchPackageListFromPyPI # } diff --git a/pdm/project/config.py b/pdm/project/config.py index 0795ea8a83..3185e93076 100644 --- a/pdm/project/config.py +++ b/pdm/project/config.py @@ -130,7 +130,7 @@ class Config(MutableMapping[str, str]): ), "global_project.path": ConfigItem( "The path to the global project", - os.path.expanduser("~/.pdm/global-project"), + platformdirs.user_config_path("pdm") / "global-project", True, ), "global_project.user_site": ConfigItem( diff --git a/pdm/project/core.py b/pdm/project/core.py index 88c101f61b..e67c525ce1 100644 --- a/pdm/project/core.py +++ b/pdm/project/core.py @@ -10,6 +10,7 @@ from typing import TYPE_CHECKING, Any, Iterable, Type, cast from urllib.parse import urlparse +import platformdirs import tomlkit from findpython import Finder @@ -78,7 +79,7 @@ def __init__( self.core = core if global_config is None: - global_config = Path.home() / ".pdm/config.toml" + global_config = platformdirs.user_config_path("pdm") / "config.toml" self.global_config = Config(Path(global_config), is_global=True) global_project = Path(self.global_config["global_project.path"])