1"""Manages cache of a dvc repo.""" 2 3from __future__ import unicode_literals 4 5import os 6 7from dvc.config import Config 8 9 10class Cache(object): 11 """Class that manages cache locations of a dvc repo. 12 13 Args: 14 repo (dvc.repo.Repo): repo instance that this cache belongs to. 15 """ 16 17 CACHE_DIR = "cache" 18 19 def __init__(self, repo): 20 from dvc.remote import Remote 21 22 self.repo = repo 23 24 config = repo.config.config[Config.SECTION_CACHE] 25 local = config.get(Config.SECTION_CACHE_LOCAL) 26 27 if local: 28 name = Config.SECTION_REMOTE_FMT.format(local) 29 sect = repo.config.config[name] 30 else: 31 default_cache_dir = os.path.join(repo.dvc_dir, self.CACHE_DIR) 32 cache_dir = config.get(Config.SECTION_CACHE_DIR, default_cache_dir) 33 cache_type = config.get(Config.SECTION_CACHE_TYPE) 34 protected = config.get(Config.SECTION_CACHE_PROTECTED) 35 36 sect = { 37 Config.PRIVATE_CWD: config.get( 38 Config.PRIVATE_CWD, repo.dvc_dir 39 ), 40 Config.SECTION_REMOTE_URL: cache_dir, 41 Config.SECTION_CACHE_TYPE: cache_type, 42 Config.SECTION_CACHE_PROTECTED: protected, 43 } 44 45 self._local = Remote(repo, sect) 46 47 self._s3 = self._get_remote(config, Config.SECTION_CACHE_S3) 48 self._gs = self._get_remote(config, Config.SECTION_CACHE_GS) 49 self._ssh = self._get_remote(config, Config.SECTION_CACHE_SSH) 50 self._hdfs = self._get_remote(config, Config.SECTION_CACHE_HDFS) 51 self._azure = self._get_remote(config, Config.SECTION_CACHE_AZURE) 52 53 @property 54 def local(self): 55 """Remote instance for local cache.""" 56 return self._local 57 58 @property 59 def s3(self): 60 """Remote instance for AWS S3 cache.""" 61 return self._s3 62 63 @property 64 def gs(self): 65 """Remote instance for Google Cloud Storage cache.""" 66 return self._gs 67 68 @property 69 def ssh(self): 70 """Remote instance for SSH cache.""" 71 return self._ssh 72 73 @property 74 def hdfs(self): 75 """Remote instance for HDFS cache.""" 76 return self._hdfs 77 78 @property 79 def azure(self): 80 """Remote instance for azure cache.""" 81 return self._azure 82 83 def _get_remote(self, config, name): 84 from dvc.remote import Remote 85 86 remote = config.get(name, None) 87 if not remote: 88 return None 89 90 name = Config.SECTION_REMOTE_FMT.format(remote) 91 sect = self.repo.config.config[name] 92 return Remote(self.repo, sect) 93