diff --git a/dvc/cli.py b/dvc/cli.py index 6b69d7ba8..605fa8957 100644 --- a/dvc/cli.py +++ b/dvc/cli.py @@ -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( diff --git a/dvc/command/cache.py b/dvc/command/cache.py new file mode 100644 index 000000000..599a1ed96 --- /dev/null +++ b/dvc/command/cache.py @@ -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() diff --git a/tests/test_cache.py b/tests/test_cache.py index a7a33a520..099fdd609 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -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)