Skip to content

Commit

Permalink
Make use of unified cache directory
Browse files Browse the repository at this point in the history
  • Loading branch information
tjelvar-olsson committed Apr 25, 2019
1 parent 302c29c commit a157f0f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 75 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Added
Changed
^^^^^^^

- ``dtool config cache`` now works with one unified cache directory for all
storage brokers


Deprecated
^^^^^^^^^^
Expand Down
62 changes: 9 additions & 53 deletions dtool_config/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,65 +85,21 @@ def secret_access_key(ecs_secret_access_key):
))


@config.group()
def cache():
"""Configure ECS S3 object storage."""


@cache.command()
@click.argument(
"scheme",
type=click.Choice(dtool_config.utils.CACHE_KEYS.keys())
)
def get(scheme):
"""Print the cache directory of the specific storage scheme."""
click.secho(dtool_config.utils.get_cache(
CONFIG_PATH,
scheme
))


@cache.command()
@click.argument(
"scheme",
type=click.Choice(dtool_config.utils.CACHE_KEYS.keys())
)
@click.argument(
"cache_directory_path",
type=click.Path(exists=True, file_okay=False)
)
def set(scheme, cache_directory_path):
"""Configure the cache directory of the specific storage scheme."""
click.secho(dtool_config.utils.set_cache(
CONFIG_PATH,
scheme,
cache_directory_path
))


@cache.command()
def ls():
"""List the storage scheme cache directories."""
for scheme in dtool_config.utils.CACHE_KEYS.keys():
line = "{}\t{}".format(
scheme,
dtool_config.utils.get_cache(CONFIG_PATH, scheme)
)
click.secho(line)


@config.command()
@click.argument(
"cache_directory_path",
required=False,
type=click.Path(exists=True, file_okay=False)
)
@cache.command()
def set_all(cache_directory_path):
"""Configure the cache directory for all storage schemes."""
for scheme in dtool_config.utils.CACHE_KEYS.keys():
click.secho(scheme + "\t", nl=False)
def cache(cache_directory_path):
"""Display / set / update the dtool cache directory."""
if not cache_directory_path:
click.secho(dtool_config.utils.get_cache(
CONFIG_PATH,
))
else:
click.secho(dtool_config.utils.set_cache(
CONFIG_PATH,
scheme,
cache_directory_path
))

Expand Down
29 changes: 11 additions & 18 deletions dtool_config/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Helper functions for getting and setting config values."""

import os

from dtoolcore.utils import (
_get_config_dict_from_file,
get_config_value_from_file,
Expand All @@ -15,14 +17,6 @@

AZURE_KEY_PREFIX = "DTOOL_AZURE_ACCOUNT_KEY_"

CACHE_KEYS = {
"azure": "DTOOL_AZURE_CACHE_DIRECTORY",
"ecs": "DTOOL_ECS_CACHE_DIRECTORY",
"http": "DTOOL_HTTP_CACHE_DIRECTORY",
"irods": "DTOOL_IRODS_CACHE_DIRECTORY",
"s3": "DTOOL_S3_CACHE_DIRECTORY",
}


def get_username(config_fpath):
"""Return the user name.
Expand Down Expand Up @@ -130,30 +124,29 @@ def set_ecs_secret_access_key(config_fpath, ecs_secret_access_key):
)


def get_cache(config_fpath, storage_scheme):
"""Return the cache directory for the specified storage broker.
def get_cache(config_fpath):
"""Return the cache directory specified in the dtool config file.
:param config_fpath: path to the dtool config file
:param storage_scheme: storage scheme (as in URI scheme)
:returns: the ECS secret access key or an empty string
:returns: the path to the dtool cache directory
"""

return get_config_value_from_file(
CACHE_KEYS[storage_scheme],
"DTOOL_CACHE_DIRECTORY",
config_fpath,
""
)


def set_cache(config_fpath, storage_scheme, cache_dir):
"""Write the ECS access key id to the dtool config file.
def set_cache(config_fpath, cache_dir):
"""Write the cache directory to the dtool config file.
:param config_fpath: path to the dtool config file
:param storage_scheme: storage scheme (as in URI scheme)
:param cache_dir: ECS cache direcotory
:param cache_dir: the path to the dtool cache direcotory
"""
cache_dir = os.path.abspath(cache_dir)
return write_config_value_to_file(
CACHE_KEYS[storage_scheme],
"DTOOL_CACHE_DIRECTORY",
cache_dir,
config_fpath
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def test_set_get_cache(tmp_dir_fixture): # NOQA

config_fpath = os.path.join(tmp_dir_fixture, "dtool.json")

assert dtool_config.utils.get_cache(config_fpath, "ecs") == ""
assert dtool_config.utils.get_cache(config_fpath) == ""

ecs_cache_dir = os.path.join(tmp_dir_fixture, "ecs")
dtool_config.utils.set_cache(config_fpath, "ecs", ecs_cache_dir)
assert dtool_config.utils.get_cache(config_fpath, "ecs") == ecs_cache_dir
cache_dir = os.path.join(tmp_dir_fixture, "dtool_cache")
dtool_config.utils.set_cache(config_fpath, cache_dir)
assert dtool_config.utils.get_cache(config_fpath) == cache_dir


def test_set_get_azure_container(tmp_dir_fixture): # NOQA
Expand Down

0 comments on commit a157f0f

Please sign in to comment.