def _clone(url=None, rev=None, rev_lock=None, cache_dir=None): import git from dvc.repo import Repo _path = tempfile.mkdtemp("dvc-repo") try: repo = git.Repo.clone_from(url, _path, no_single_branch=True) except git.exc.GitCommandError as exc: raise CloneError(url, _path, exc) try: revision = rev_lock or rev if revision: try: repo.git.checkout(revision) except git.exc.GitCommandError as exc: raise RevError(url, revision, exc) finally: repo.close() if cache_dir: repo = Repo(_path) cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) repo.scm.git.close() return Repo(_path)
def _install_to(self, tmp_dir, cache_dir): import git try: git.Repo.clone_from( self.url, tmp_dir, depth=1, no_single_branch=True ) except git.exc.GitCommandError as exc: raise InstallError(self.url, tmp_dir, exc) if self.rev: try: repo = git.Repo(tmp_dir) repo.git.checkout(self.rev) repo.close() except git.exc.GitCommandError as exc: raise RevError(self.url, self.rev, exc) if cache_dir: from dvc.repo import Repo repo = Repo(tmp_dir) cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) repo.scm.git.close()
def _external_repo(url=None, rev=None, cache_dir=None): from dvc.config import Config from dvc.cache import CacheConfig from dvc.repo import Repo key = (url, rev, cache_dir) if key in REPO_CACHE: return REPO_CACHE[key] new_path = tempfile.mkdtemp("dvc-erepo") # Copy and adjust existing clone if (url, None, None) in REPO_CACHE: old_path = REPO_CACHE[url, None, None] # This one unlike shutil.copytree() works with an existing dir copy_tree(old_path, new_path) else: # Create a new clone _clone_repo(url, new_path) # Save clean clone dir so that we will have access to a default branch clean_clone_path = tempfile.mkdtemp("dvc-erepo") copy_tree(new_path, clean_clone_path) REPO_CACHE[url, None, None] = clean_clone_path # Adjust new clone/copy to fit rev and cache_dir # Checkout needs to be done first because current branch might not be # DVC repository if rev is not None: _git_checkout(new_path, rev) repo = Repo(new_path) try: # check if the URL is local and no default remote is present # add default remote pointing to the original repo's cache location if os.path.isdir(url): rconfig = RemoteConfig(repo.config) if not _default_remote_set(rconfig): original_repo = Repo(url) try: rconfig.add( "auto-generated-upstream", original_repo.cache.local.cache_dir, default=True, level=Config.LEVEL_LOCAL, ) finally: original_repo.close() if cache_dir is not None: cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) finally: # Need to close/reopen repo to force config reread repo.close() REPO_CACHE[key] = new_path return new_path
class CmdCacheDir(CmdConfig): def __init__(self, args): super().__init__(args) self.cache_config = CacheConfig(self.config) def run(self): self.cache_config.set_dir(self.args.value, level=self.args.level) return 0
def _clone(cache_dir=None, **kwargs): from dvc.repo import Repo _path = tempfile.mkdtemp("dvc-repo") _clone_git_repo(_path, **kwargs) if cache_dir: repo = Repo(_path) cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) repo.scm.close() return Repo(_path)
def _clone(cache_dir=None, url=None, rev=None, rev_lock=None): from dvc.repo import Repo _path = tempfile.mkdtemp("dvc-repo") repo = Repo.clone(url, _path, rev=(rev_lock or rev)) try: if cache_dir: cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) finally: repo.close() return Repo(_path)
def _external_repo(url=None, rev=None, cache_dir=None): from dvc.config import Config from dvc.cache import CacheConfig from dvc.repo import Repo key = (url, rev, cache_dir) if key in REPO_CACHE: return REPO_CACHE[key] new_path = tempfile.mkdtemp("dvc-erepo") # Copy and adjust existing clone if (url, None, None) in REPO_CACHE: old_path = REPO_CACHE[url, None, None] # This one unlike shutil.copytree() works with an existing dir copy_tree(old_path, new_path) else: # Create a new clone _clone_repo(url, new_path) # Save clean clone dir so that we will have access to a default branch clean_clone_path = tempfile.mkdtemp("dvc-erepo") copy_tree(new_path, clean_clone_path) REPO_CACHE[url, None, None] = clean_clone_path # Adjust new clone/copy to fit rev and cache_dir # Checkout needs to be done first because current branch might not be # DVC repository if rev is not None: _git_checkout(new_path, rev) repo = Repo(new_path) try: if cache_dir is not None: cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) finally: # Need to close/reopen repo to force config reread repo.close() REPO_CACHE[key] = new_path return new_path
def install(self, cache_dir=None, force=False): import git if self.installed: if not force: logger.info("Skipping installing '{}'('{}') as it is already " "installed.".format(self.name, self.url)) return self.uninstall() git.Repo.clone_from(self.url, self.path, depth=1, no_single_branch=True) if self.version: self.repo.scm.checkout(self.version) if cache_dir: cache_config = CacheConfig(self.repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL)
def _external_repo(url=None, rev=None, cache_dir=None): from dvc.config import Config from dvc.cache import CacheConfig from dvc.repo import Repo key = (url, rev, cache_dir) if key in REPO_CACHE: return REPO_CACHE[key] new_path = cached_clone(url, rev=rev) repo = Repo(new_path) try: # check if the URL is local and no default remote is present # add default remote pointing to the original repo's cache location if os.path.isdir(url): rconfig = RemoteConfig(repo.config) if not _default_remote_set(rconfig): original_repo = Repo(url) try: rconfig.add( "auto-generated-upstream", original_repo.cache.local.cache_dir, default=True, level=Config.LEVEL_LOCAL, ) finally: original_repo.close() if cache_dir is not None: cache_config = CacheConfig(repo.config) cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL) finally: # Need to close/reopen repo to force config reread repo.close() REPO_CACHE[key] = new_path return new_path