From 294b259579a4555c93690d6b0b496d3fee23a4df Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 4 Jan 2019 10:51:52 +0200 Subject: [PATCH] cache: support relative paths Fixes #1284 Signed-off-by: Ruslan Kuprieiev --- dvc/cache.py | 7 ++----- dvc/config.py | 33 +++++++++++++++++++++++++++------ tests/test_cache.py | 11 ++++++++++- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/dvc/cache.py b/dvc/cache.py index c492e32cc..89ca47eac 100644 --- a/dvc/cache.py +++ b/dvc/cache.py @@ -18,14 +18,11 @@ class Cache(object): name = Config.SECTION_REMOTE_FMT.format(local) sect = project.config._config[name] else: - cache_dir = config.get(Config.SECTION_CACHE_DIR, self.CACHE_DIR) + default_cache_dir = os.path.join(project.dvc_dir, self.CACHE_DIR) + cache_dir = config.get(Config.SECTION_CACHE_DIR, default_cache_dir) cache_type = config.get(Config.SECTION_CACHE_TYPE) protected = config.get(Config.SECTION_CACHE_PROTECTED) - if not os.path.isabs(cache_dir): - cache_dir = os.path.join(project.dvc_dir, cache_dir) - cache_dir = os.path.abspath(os.path.realpath(cache_dir)) - sect = { Config.SECTION_REMOTE_URL: cache_dir, Config.SECTION_CACHE_TYPE: cache_type, diff --git a/dvc/config.py b/dvc/config.py index bdc6c08ac..25f5228c9 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -253,19 +253,39 @@ class Config(object): self._config = None + def _load_config(self, path): + config = configobj.ConfigObj(path) + config = self._lower(config) + self._resolve_paths(config, path) + return config + + def _resolve_paths(self, config, fname): + cache = config.get(self.SECTION_CACHE) + if cache is None: + return + + cache_dir = cache.get(self.SECTION_CACHE_DIR) + if cache_dir is None: + return + + if os.path.isabs(cache_dir): + return + + config_dir = os.path.dirname(fname) + cache_dir = os.path.abspath(os.path.join(config_dir, cache_dir)) + cache[self.SECTION_CACHE_DIR] = cache_dir + def load(self, validate=True): self._load() try: - self._config = configobj.ConfigObj(self.system_config_file) - user = configobj.ConfigObj(self.global_config_file) - config = configobj.ConfigObj(self.config_file) - local = configobj.ConfigObj(self.config_local_file) + self._config = self._load_config(self.system_config_file) + user = self._load_config(self.global_config_file) + config = self._load_config(self.config_file) + local = self._load_config(self.config_local_file) # NOTE: schema doesn't support ConfigObj.Section validation, so we # need to convert our config to dict before passing it to - self._config = self._lower(self._config) for c in [user, config, local]: - c = self._lower(c) self._config = self._merge(self._config, c) if validate: @@ -275,6 +295,7 @@ class Config(object): self._config = configobj.ConfigObj(self._config, write_empty_values=True) self._config.filename = self.config_file + self._resolve_paths(self._config, self.config_file) except Exception as ex: raise ConfigError(ex) diff --git a/tests/test_cache.py b/tests/test_cache.py index 099fdd609..7c39e35a8 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -127,7 +127,8 @@ class TestCmdCacheDir(TestDvc): self.assertEqual(config['cache']['dir'], dname) def test_relative_path(self): - dname = os.path.join('..', 'path', 'to', 'dir') + tmpdir = self.mkdtemp() + dname = os.path.relpath(tmpdir) ret = main(['cache', 'dir', dname]) self.assertEqual(ret, 0) @@ -136,3 +137,11 @@ class TestCmdCacheDir(TestDvc): rel = os.path.join('..', dname) config = configobj.ConfigObj(self.dvc.config.config_file) self.assertEqual(config['cache']['dir'], rel) + + ret = main(['add', self.FOO]) + self.assertEqual(ret, 0) + + subdirs = os.listdir(tmpdir) + self.assertEqual(len(subdirs), 1) + files = os.listdir(os.path.join(tmpdir, subdirs[0])) + self.assertEqual(len(files), 1)