Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved get-poetry.py and added POETRY_HOME support to poetry command #3550

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ python install-poetry.py --uninstall
POETRY_UNINSTALL=1 python install-poetry.py
```

By default, Poetry is installed into the user's platform-specific home directory.
If you wish to change this, you may define the `POETRY_HOME` environment variable:
By default, Poetry is installed into the user's platform-specific home directory. If you wish to change this, you may define the `POETRY_HOME` environment variable. In Linux, this variable can also be used with `poetry` command to substitute `XDG_CACHE_HOME`, `XDG_CONFIG_HOME` and `XDG_DATA_HOME` variables.

```bash
POETRY_HOME=/etc/poetry python install-poetry.py
POETRY_HOME=/tmp/poetry poetry config --list
POETRY_HOME=/tmp/poetry poetry env info
```

If you want to install prerelease versions, you can do so by passing `--preview` option to `install-poetry.py`
Expand Down
2 changes: 1 addition & 1 deletion get-poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _compare_versions(x, y):
return version, current_version

def customize_install(self):
if not self._accept_all:
if not self._accept_all and self._modify_path:
print("Before we start, please answer the following questions.")
print("You may simply press the Enter key to leave unchanged.")

Expand Down
40 changes: 31 additions & 9 deletions poetry/utils/appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def user_cache_dir(appname: str) -> str:

Typical user cache directories are:
macOS: ~/Library/Caches/<AppName>
Unix: ~/.cache/<AppName> (XDG default)
Unix: ${POETRY_HOME}/.cache/<AppName> or
${XDG_CACHE_HOME}/<AppName> or
~/.cache/<AppName>
Windows: C:\Users\<username>\AppData\Local\<AppName>\Cache

On Windows the only suggestion in the MSDN docs is that local settings go
Expand All @@ -64,7 +66,11 @@ def user_cache_dir(appname: str) -> str:
path = os.path.join(path, appname)
else:
# Get the base path
path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache"))
path = os.getenv("POETRY_HOME")
if path is not None:
path = os.path.join(path, ".cache")
else:
path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache"))

# Add our app name to it
path = os.path.join(path, appname)
Expand All @@ -87,8 +93,9 @@ def user_data_dir(appname: str, roaming: bool = False) -> str:

Typical user data directories are:
macOS: ~/Library/Application Support/<AppName>
Unix: ~/.local/share/<AppName> # or in
$XDG_DATA_HOME, if defined
Unix: ${POETRY_HOME}/.local/share/<AppName> or
${XDG_DATA_HOME}/<AppName> or
~/.local/share/<AppName>
Win XP (not roaming): C:\Documents and Settings\<username>\ ...
...Application Data\<AppName>
Win XP (roaming): C:\Documents and Settings\<username>\Local ...
Expand All @@ -105,9 +112,15 @@ def user_data_dir(appname: str, roaming: bool = False) -> str:
elif sys.platform == "darwin":
path = os.path.join(expanduser("~/Library/Application Support/"), appname)
else:
path = os.path.join(
os.getenv("XDG_DATA_HOME", expanduser("~/.local/share")), appname
)
# Get the base path
path = os.getenv("POETRY_HOME")
if path is not None:
path = os.path.join(path, ".local/share")
else:
path = os.getenv("XDG_DATA_HOME", expanduser("~/.local/share"))

# Add our app name to it
path = os.path.join(path, appname)

return path

Expand All @@ -126,7 +139,9 @@ def user_config_dir(appname: str, roaming: bool = True) -> str:

Typical user data directories are:
macOS: same as user_data_dir
Unix: ~/.config/<AppName>
Unix: ${POETRY_HOME}/.config/<AppName> or
${XDG_CONFIG_HOME}/<AppName> or
~/.config/<AppName>
Win *: same as user_data_dir

For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
Expand All @@ -137,7 +152,14 @@ def user_config_dir(appname: str, roaming: bool = True) -> str:
elif sys.platform == "darwin":
path = user_data_dir(appname)
else:
path = os.getenv("XDG_CONFIG_HOME", expanduser("~/.config"))
# Get the base path
path = os.getenv("POETRY_HOME")
if path is not None:
path = os.path.join(path, ".config")
else:
path = os.getenv("XDG_CONFIG_HOME", expanduser("~/.config"))

# Add our app name to it
path = os.path.join(path, appname)

return path
Expand Down
69 changes: 69 additions & 0 deletions tests/utils/test_appdirs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from os import environ
from os import path
from sys import platform

from poetry.utils.appdirs import WINDOWS
from poetry.utils.appdirs import expanduser
from poetry.utils.appdirs import user_cache_dir
from poetry.utils.appdirs import user_config_dir
from poetry.utils.appdirs import user_data_dir


def test_user_cache_dir():
if WINDOWS or (platform == "darwin"):
return

environ["POETRY_HOME"] = "/poetry"
environ["XDG_CACHE_HOME"] = "/xdg/.cache"
appname = "pypoetry"
default_user_cache_dir = path.join(expanduser("~/.cache"), appname)

assert user_cache_dir(appname) == path.join(
environ["POETRY_HOME"], ".cache", appname
)

del environ["POETRY_HOME"]
assert user_cache_dir(appname) == path.join(environ["XDG_CACHE_HOME"], appname)

del environ["XDG_CACHE_HOME"]
assert user_cache_dir(appname) == default_user_cache_dir


def test_user_config_dir():
if WINDOWS or (platform == "darwin"):
return

environ["POETRY_HOME"] = "/poetry"
environ["XDG_CONFIG_HOME"] = "/xdg/.config"
appname = "pypoetry"
default_user_config_dir = path.join(expanduser("~/.config"), appname)

assert user_config_dir(appname) == path.join(
environ["POETRY_HOME"], ".config", appname
)

del environ["POETRY_HOME"]
assert user_config_dir(appname) == path.join(environ["XDG_CONFIG_HOME"], appname)

del environ["XDG_CONFIG_HOME"]
assert user_config_dir(appname) == default_user_config_dir


def test_user_data_dir():
if WINDOWS or (platform == "darwin"):
return

environ["POETRY_HOME"] = "/poetry"
environ["XDG_DATA_HOME"] = "/xdg/.local/share"
appname = "pypoetry"
default_user_data_dir = path.join(expanduser("~/.local/share"), appname)

assert user_data_dir(appname) == path.join(
environ["POETRY_HOME"], ".local/share", appname
)

del environ["POETRY_HOME"]
assert user_data_dir(appname) == path.join(environ["XDG_DATA_HOME"], appname)

del environ["XDG_DATA_HOME"]
assert user_data_dir(appname) == default_user_data_dir