From 1d7e39b955f4da58230f9c5a06ff1b05f8bc020b Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Fri, 20 Feb 2015 11:13:24 +0000 Subject: sourceresolver: Factor out 'cache repo locally' code into a function Also, move the repo.update() call into the 'fetch from tarball' code path in the localrepocache module -- there's no need to update straight after doing a `git clone`, but we do need to do one if we got the repo from a `git archive` tarball that may be out of date. --- morphlib/localrepocache.py | 6 ++++-- morphlib/localrepocache_tests.py | 8 ++++++-- morphlib/sourceresolver.py | 44 +++++++++++++++++----------------------- morphlib/sourceresolver_tests.py | 3 +-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/morphlib/localrepocache.py b/morphlib/localrepocache.py index 9bccb20b..39fbd200 100644 --- a/morphlib/localrepocache.py +++ b/morphlib/localrepocache.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2014 Codethink Limited +# Copyright (C) 2012-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -204,7 +204,9 @@ class LocalRepoCache(object): if self._tarball_base_url: ok, error = self._clone_with_tarball(repourl, path) if ok: - return self.get_repo(reponame) + repo = self.get_repo(reponame) + repo.update() + return repo else: errors.append(error) self._app.status( diff --git a/morphlib/localrepocache_tests.py b/morphlib/localrepocache_tests.py index ab6e71fd..aeb32961 100644 --- a/morphlib/localrepocache_tests.py +++ b/morphlib/localrepocache_tests.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2014 Codethink Limited +# Copyright (C) 2012-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -139,7 +139,11 @@ class LocalRepoCacheTests(unittest.TestCase): self.lrc._fetch = lambda url, path: self.fetched.append(url) self.unpacked_tar = "" self.mkdir_path = "" - self.lrc.cache_repo(self.repourl) + + with morphlib.gitdir_tests.monkeypatch( + morphlib.cachedrepo.CachedRepo, 'update', lambda self: None): + self.lrc.cache_repo(self.repourl) + self.assertEqual(self.fetched, [self.tarball_url]) self.assertFalse(self.lrc.fs.exists(self.cache_path + '.tar')) self.assertEqual(self.remotes['origin']['url'], self.repourl) diff --git a/morphlib/sourceresolver.py b/morphlib/sourceresolver.py index 2d2d02f1..e84a6cd2 100644 --- a/morphlib/sourceresolver.py +++ b/morphlib/sourceresolver.py @@ -90,13 +90,6 @@ class MorphologyNotFoundError(SourceResolverError): # pragma: no cover self, "Couldn't find morphology: %s" % filename) -class NotcachedError(SourceResolverError): - def __init__(self, repo_name): - SourceResolverError.__init__( - self, "Repository %s is not cached locally and there is no " - "remote cache specified" % repo_name) - - class SourceResolver(object): '''Provides a way of resolving the set of sources for a given system. @@ -155,6 +148,18 @@ class SourceResolver(object): self._definitions_checkout_dir = None + def cache_repo_locally(self, reponame): + if self.update: + self.status(msg='Caching git repository %(reponame)s', + reponame=reponame) + repo = self.lrc.cache_repo(reponame) + else: # pragma: no cover + # This is likely to raise a morphlib.localrepocache.NotCached + # exception, because the caller should have checked if the + # localrepocache already had the repo. But we may as well try. + repo = self.lrc.get_repo(reponame) + return repo + def _resolve_ref(self, reponame, ref): # pragma: no cover '''Resolves commit and tree sha1s of the ref in a repo and returns it. @@ -195,16 +200,9 @@ class SourceResolver(object): chatty=True) except BaseException, e: logging.warning('Caught (and ignored) exception: %s' % str(e)) + if absref is None: - if self.update: - self.status(msg='Caching git repository %(reponame)s', - reponame=reponame) - repo = self.lrc.cache_repo(reponame) - repo.update() - else: - # This is likely to raise an exception, because if the local - # repo cache had the repo we'd have already resolved the ref. - repo = self.lrc.get_repo(reponame) + repo = self.cache_repo_locally(reponame) absref = repo.resolve_ref_to_commit(ref) tree = repo.resolve_ref_to_tree(absref) @@ -306,15 +304,10 @@ class SourceResolver(object): file_list = self.rrc.ls_tree(reponame, sha1) except morphlib.remoterepocache.LsTreeError: pass + if not file_list: - if self.update: - self.status(msg='Caching git repository %(reponame)s', - reponame=reponame) - repo = self.lrc.cache_repo(reponame) - repo.update() - file_list = repo.list_files(ref=sha1, recurse=False) - else: # pragma: no cover - raise NotcachedError(reponame) + repo = self.cache_repo_locally(reponame) + file_list = repo.list_files(ref=sha1, recurse=False) buildsystem = morphlib.buildsystem.detect_build_system(file_list) @@ -453,7 +446,8 @@ class SourceResolver(object): try: definitions_cached_repo = self.lrc.get_repo(definitions_repo) except morphlib.localrepocache.NotCached: - definitions_cached_repo = self.lrc.cache_repo(definitions_repo) + definitions_cached_repo = self.cache_repo_locally( + definitions_repo) definitions_cached_repo.extract_commit( definitions_absref, self._definitions_checkout_dir) diff --git a/morphlib/sourceresolver_tests.py b/morphlib/sourceresolver_tests.py index 75025a9a..638f593f 100644 --- a/morphlib/sourceresolver_tests.py +++ b/morphlib/sourceresolver_tests.py @@ -22,8 +22,7 @@ import unittest import morphlib from morphlib.sourceresolver import (SourceResolver, PickleCacheManager, - MorphologyNotFoundError, - NotcachedError) + MorphologyNotFoundError) from morphlib.remoterepocache import CatFileError, LsTreeError -- cgit v1.2.1