mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
cache: support relative paths
Fixes #1284 Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
+2
-5
@@ -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,
|
||||
|
||||
+27
-6
@@ -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)
|
||||
|
||||
|
||||
+10
-1
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user