dvc: introduce dvc cache dir command

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2019-01-04 09:02:37 +02:00
parent d1be895b61
commit db6cd8e1bb
3 changed files with 79 additions and 0 deletions
+41
View File
@@ -21,6 +21,7 @@ from dvc.command.config import CmdConfig
from dvc.command.checkout import CmdCheckout
from dvc.command.remote import CmdRemoteAdd, CmdRemoteRemove
from dvc.command.remote import CmdRemoteModify, CmdRemoteList, CmdRemoteDefault
from dvc.command.cache import CmdCacheDir
from dvc.command.metrics import CmdMetricsShow, CmdMetricsAdd
from dvc.command.metrics import CmdMetricsRemove, CmdMetricsModify
from dvc.command.install import CmdInstall
@@ -783,6 +784,46 @@ def parse_args(argv=None):
help=REMOTE_LIST_HELP)
remote_list_parser.set_defaults(func=CmdRemoteList)
# Cache
CACHE_HELP = 'Manage cache settings.'
cache_parser = subparsers.add_parser(
'cache',
parents=[parent_parser],
description=CACHE_HELP,
help=CACHE_HELP)
cache_subparsers = cache_parser.add_subparsers(
dest='cmd',
help='Use dvc cache CMD --help for '
'command-specific help.')
_fix_subparsers(cache_subparsers)
parent_cache_config_parser = argparse.ArgumentParser(
add_help=False,
parents=[parent_config_parser])
CACHE_DIR_HELP = 'Configure cache directory location.'
cache_dir_parser = cache_subparsers.add_parser(
'dir',
parents=[parent_cache_config_parser],
description=CACHE_DIR_HELP,
help=CACHE_DIR_HELP)
cache_dir_parser.add_argument(
'-u',
'--unset',
default=False,
action='store_true',
help='Unset option.')
cache_dir_parser.add_argument(
'value',
nargs='?',
default=None,
help='Path to cache directory. Relative paths are '
'resolved relative to the current directory and '
'saved to config relative to the config file '
'location.')
cache_dir_parser.set_defaults(func=CmdCacheDir)
# Metrics
METRICS_HELP = 'Get metrics from all branches.'
metrics_parser = subparsers.add_parser(
+16
View File
@@ -0,0 +1,16 @@
import os
from dvc.command.config import CmdConfig
class CmdCacheDir(CmdConfig):
def run(self):
self.args.name = 'cache.dir'
path = self.args.value
if not os.path.isabs(path):
config_dir = os.path.dirname(self.configobj.filename)
path = os.path.relpath(path, config_dir)
self.args.value = path
return super(CmdCacheDir, self).run()
+22
View File
@@ -1,5 +1,6 @@
import os
import shutil
import configobj
from dvc.cache import Cache
from dvc.main import main
@@ -114,3 +115,24 @@ class TestCacheLinkType(TestDvc):
ret = main(['add', self.FOO])
self.assertEqual(ret, 0)
class TestCmdCacheDir(TestDvc):
def test_abs_path(self):
dname = os.path.join(os.path.dirname(self._root_dir), 'dir')
ret = main(['cache', 'dir', dname])
self.assertEqual(ret, 0)
config = configobj.ConfigObj(self.dvc.config.config_file)
self.assertEqual(config['cache']['dir'], dname)
def test_relative_path(self):
dname = os.path.join('..', 'path', 'to', 'dir')
ret = main(['cache', 'dir', dname])
self.assertEqual(ret, 0)
# NOTE: we are in the project's root and config is in .dvc/, so
# dir path written to config should be just one level above.
rel = os.path.join('..', dname)
config = configobj.ConfigObj(self.dvc.config.config_file)
self.assertEqual(config['cache']['dir'], rel)