Skip to content

Commit

Permalink
improved get-poetry.py and added POETRY_HOME support in actual poetry…
Browse files Browse the repository at this point in the history
… application

* fixed prompting even after no-modify-path explicitly given in commandline
* use POETRY_HOME if available instead of XDG_*_HOME variables in nix systems
  • Loading branch information
mohan43u committed Mar 8, 2021
1 parent adc3cde commit 0673c4e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 13 deletions.
4 changes: 3 additions & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ python get-poetry.py --uninstall
POETRY_UNINSTALL=1 python get-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 get-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` to `get-poetry.py`
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/pyproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ poetry install --extras "mysql pgsql"
poetry install -E mysql -E pgsql
```

When installing or specifying Poetry-built packages, the extras defined in this section can be activated
When installing or specifying Poetry-built packages, the extras defined in this section can be activated
as described in [PEP 508](https://www.python.org/dev/peps/pep-0508/#extras).

For example, when installing the package using `pip`, the dependencies required by
For example, when installing the package using `pip`, the dependencies required by
the `databases` extra can be installed as shown below.

```bash
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

0 comments on commit 0673c4e

Please sign in to comment.