mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-25 23:09:49 +01:00
Setting default values is very untypical for all other applications and the info config has is not enough to make a decision on the default cache dir, especially when dealing with gitfs. Fixes #8705
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
import os
|
|
|
|
from dvc.fs import GitFileSystem, Schemes
|
|
from dvc_data.hashfile.db import get_odb
|
|
|
|
|
|
def _get_odb(repo, settings, fs=None):
|
|
from dvc.fs import get_cloud_fs
|
|
|
|
if not settings:
|
|
return None
|
|
|
|
cls, config, fs_path = get_cloud_fs(repo, **settings)
|
|
fs = fs or cls(**config)
|
|
return get_odb(fs, fs_path, state=repo.state, **config)
|
|
|
|
|
|
class CacheManager:
|
|
CACHE_DIR = "cache"
|
|
CLOUD_SCHEMES = [
|
|
Schemes.S3,
|
|
Schemes.GS,
|
|
Schemes.SSH,
|
|
Schemes.HDFS,
|
|
Schemes.WEBHDFS,
|
|
]
|
|
|
|
def __init__(self, repo):
|
|
self._repo = repo
|
|
self.config = config = repo.config["cache"]
|
|
self._odb = {}
|
|
|
|
default = None
|
|
if repo and repo.dvc_dir:
|
|
if isinstance(repo.fs, GitFileSystem):
|
|
relparts = ()
|
|
if repo.root_dir != "/":
|
|
# subrepo
|
|
relparts = repo.fs.path.relparts(repo.root_dir, "/")
|
|
dvc_dir = os.path.join(
|
|
repo.scm.root_dir,
|
|
*relparts,
|
|
repo.DVC_DIR,
|
|
)
|
|
if os.path.exists(dvc_dir):
|
|
default = os.path.join(dvc_dir, self.CACHE_DIR)
|
|
else:
|
|
default = repo.fs.path.join(repo.dvc_dir, self.CACHE_DIR)
|
|
|
|
local = config.get("local")
|
|
|
|
if local:
|
|
settings = {"name": local}
|
|
elif "dir" not in config and not default:
|
|
settings = None
|
|
else:
|
|
from dvc.config_schema import LOCAL_COMMON
|
|
|
|
url = config.get("dir") or default
|
|
settings = {"url": url}
|
|
for opt in LOCAL_COMMON:
|
|
if opt in config:
|
|
settings[str(opt)] = config.get(opt)
|
|
|
|
kwargs = {}
|
|
if not isinstance(repo.fs, GitFileSystem):
|
|
kwargs["fs"] = repo.fs
|
|
|
|
odb = _get_odb(repo, settings, **kwargs)
|
|
self._odb["repo"] = odb
|
|
self._odb[Schemes.LOCAL] = odb
|
|
|
|
def _init_odb(self, schemes):
|
|
for scheme in schemes:
|
|
remote = self.config.get(scheme)
|
|
settings = {"name": remote} if remote else None
|
|
self._odb[scheme] = _get_odb(self._repo, settings)
|
|
|
|
def __getattr__(self, name):
|
|
if name not in self._odb and name in self.CLOUD_SCHEMES:
|
|
self._init_odb([name])
|
|
|
|
try:
|
|
return self._odb[name]
|
|
except KeyError as exc:
|
|
raise AttributeError from exc
|
|
|
|
def by_scheme(self):
|
|
self._init_odb(self.CLOUD_SCHEMES)
|
|
yield from self._odb.items()
|