Commit 54701a1d by Arun Babu Neelicattu Committed by GitHub

Add cache list command (#1187)

* Add poetry.locations.REPOSITORY_CACHE_DIR

The repository cache directory is used in multiple places in the
codebase. This change ensures that the value is reused.

* Add cache list command

This introduces a new cache sub-command that lists all available
caches.

Relates-to: #1162
parent 9e3f6064
...@@ -452,3 +452,15 @@ The `env` command regroups sub commands to interact with the virtualenvs ...@@ -452,3 +452,15 @@ The `env` command regroups sub commands to interact with the virtualenvs
associated with a specific project. associated with a specific project.
See [Managing environments](./managing-environments.md) for more information about these commands. See [Managing environments](./managing-environments.md) for more information about these commands.
## cache
The `cache` command regroups sub commands to interact with Poetry's cache.
### cache list
The `cache list` command lists Poetry's available caches.
```bash
poetry cache list
```
from poetry.console.commands.cache.list import CacheListCommand
from ..command import Command from ..command import Command
from .clear import CacheClearCommand from .clear import CacheClearCommand
...@@ -7,7 +9,7 @@ class CacheCommand(Command): ...@@ -7,7 +9,7 @@ class CacheCommand(Command):
name = "cache" name = "cache"
description = "Interact with Poetry's cache" description = "Interact with Poetry's cache"
commands = [CacheClearCommand()] commands = [CacheClearCommand(), CacheListCommand()]
def handle(self): def handle(self):
return self.call("help", self._config.name) return self.call("help", self._config.name)
...@@ -16,19 +16,17 @@ class CacheClearCommand(Command): ...@@ -16,19 +16,17 @@ class CacheClearCommand(Command):
def handle(self): def handle(self):
from cachy import CacheManager from cachy import CacheManager
from poetry.locations import CACHE_DIR from poetry.locations import REPOSITORY_CACHE_DIR
from poetry.utils._compat import Path
cache = self.argument("cache") cache = self.argument("cache")
parts = cache.split(":") parts = cache.split(":")
root = parts[0] root = parts[0]
base_cache = Path(CACHE_DIR) / "cache" / "repositories" cache_dir = REPOSITORY_CACHE_DIR / root
cache_dir = base_cache / root
try: try:
cache_dir.relative_to(base_cache) cache_dir.relative_to(REPOSITORY_CACHE_DIR)
except ValueError: except ValueError:
raise ValueError("{} is not a valid repository cache".format(root)) raise ValueError("{} is not a valid repository cache".format(root))
......
import os
from ..command import Command
class CacheListCommand(Command):
name = "list"
description = "List Poetry's caches."
def handle(self):
from poetry.locations import REPOSITORY_CACHE_DIR
if os.path.exists(str(REPOSITORY_CACHE_DIR)):
caches = list(sorted(REPOSITORY_CACHE_DIR.iterdir()))
if caches:
for cache in caches:
self.line("<info>{}</>".format(cache.name))
return 0
self.line("<warning>No caches found</>")
from .utils._compat import Path
from .utils.appdirs import user_cache_dir from .utils.appdirs import user_cache_dir
from .utils.appdirs import user_config_dir from .utils.appdirs import user_config_dir
CACHE_DIR = user_cache_dir("pypoetry") CACHE_DIR = user_cache_dir("pypoetry")
CONFIG_DIR = user_config_dir("pypoetry") CONFIG_DIR = user_config_dir("pypoetry")
REPOSITORY_CACHE_DIR = Path(CACHE_DIR) / "cache" / "repositories"
...@@ -15,7 +15,7 @@ from cachy import CacheManager ...@@ -15,7 +15,7 @@ from cachy import CacheManager
import poetry.packages import poetry.packages
from poetry.locations import CACHE_DIR from poetry.locations import REPOSITORY_CACHE_DIR
from poetry.packages import Package from poetry.packages import Package
from poetry.packages import dependency_from_pep_508 from poetry.packages import dependency_from_pep_508
from poetry.packages.utils.link import Link from poetry.packages.utils.link import Link
...@@ -174,7 +174,7 @@ class LegacyRepository(PyPiRepository): ...@@ -174,7 +174,7 @@ class LegacyRepository(PyPiRepository):
self._client_cert = client_cert self._client_cert = client_cert
self._cert = cert self._cert = cert
self._inspector = Inspector() self._inspector = Inspector()
self._cache_dir = Path(CACHE_DIR) / "cache" / "repositories" / name self._cache_dir = REPOSITORY_CACHE_DIR / name
self._cache = CacheManager( self._cache = CacheManager(
{ {
"default": "releases", "default": "releases",
......
...@@ -15,7 +15,7 @@ from requests import get ...@@ -15,7 +15,7 @@ from requests import get
from requests import session from requests import session
from requests.exceptions import TooManyRedirects from requests.exceptions import TooManyRedirects
from poetry.locations import CACHE_DIR from poetry.locations import REPOSITORY_CACHE_DIR
from poetry.packages import Package from poetry.packages import Package
from poetry.packages import dependency_from_pep_508 from poetry.packages import dependency_from_pep_508
from poetry.packages.utils.link import Link from poetry.packages.utils.link import Link
...@@ -55,7 +55,7 @@ class PyPiRepository(Repository): ...@@ -55,7 +55,7 @@ class PyPiRepository(Repository):
self._disable_cache = disable_cache self._disable_cache = disable_cache
self._fallback = fallback self._fallback = fallback
release_cache_dir = Path(CACHE_DIR) / "cache" / "repositories" / "pypi" release_cache_dir = REPOSITORY_CACHE_DIR / "pypi"
self._cache = CacheManager( self._cache = CacheManager(
{ {
"default": "releases", "default": "releases",
......
import uuid
import pytest
from cleo.testers import CommandTester
@pytest.fixture
def repository_cache_dir(monkeypatch, tmpdir):
import poetry.locations
from poetry.utils._compat import Path
path = Path(str(tmpdir))
monkeypatch.setattr(poetry.locations, "REPOSITORY_CACHE_DIR", path)
return path
@pytest.fixture
def repository_one():
return "01_{}".format(uuid.uuid4())
@pytest.fixture
def repository_two():
return "02_{}".format(uuid.uuid4())
@pytest.fixture
def mock_caches(repository_cache_dir, repository_one, repository_two):
(repository_cache_dir / repository_one).mkdir()
(repository_cache_dir / repository_two).mkdir()
def test_cache_list(app, mock_caches, repository_one, repository_two):
command = app.find("cache list")
tester = CommandTester(command)
tester.execute()
expected = """\
{}
{}
""".format(
repository_one, repository_two
)
assert expected == tester.io.fetch_output()
def test_cache_list_empty(app, repository_cache_dir):
command = app.find("cache list")
tester = CommandTester(command)
tester.execute()
expected = """\
No caches found
"""
assert expected == tester.io.fetch_output()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment