diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2016-03-02 17:11:34 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2016-03-16 19:11:21 +0000 |
commit | d58d8e8f7a4ec03ff14021a4515c8283dad52573 (patch) | |
tree | 5ece2d0524e4423bb953e6140831c9fde93b7219 /morphlib/util.py | |
parent | 014a029ade9a045a839ca86c35690b218098ea33 (diff) | |
download | morph-d58d8e8f7a4ec03ff14021a4515c8283dad52573.tar.gz |
Unify local and remote repo cache modules
There's not really any reason you'd want to use the RemoteRepoCache
class except as a workaround for the slow speed of some LocalRepoCache
operations, so I can't see this ruining anyone's day.
The main reason for doing this is so we can simply the sourceresolver
code. One reason that the sourceresolver class is so hopelessly
complicated is that it right now has to use two incompatible interfaces
for Git repo caches.
I've taken the opportunity to detangle the RepoCache class from the
App class. Now all of the configuration for the RepoCache class is
passed into the constructor explicitly. This makes the class usable from
outside Morph:
resolver = morphlib.repoaliasresolver.RepoAliasResolver(aliases=[])
repo_cache = morphlib.repocache.RepoCache('/src/cache/gits', resolver)
Change-Id: I596c81d7645b67504c88e555172a8c238f4f8a66
Diffstat (limited to 'morphlib/util.py')
-rw-r--r-- | morphlib/util.py | 54 |
1 files changed, 21 insertions, 33 deletions
diff --git a/morphlib/util.py b/morphlib/util.py index 3b3e4d2b..ba170952 100644 --- a/morphlib/util.py +++ b/morphlib/util.py @@ -102,21 +102,16 @@ def make_concurrency(cores=None): return min(n, 20) -def create_cachedir(settings): # pragma: no cover - '''Return cache directory, creating it if necessary.''' - - cachedir = settings['cachedir'] +def ensure_directory_exists(path): # pragma: no cover # Don't check the folder exists and handle the exception that happens in # this case to avoid errors if the folder is created by something else # just after the check. try: - os.mkdir(cachedir) + os.makedirs(path) except OSError as e: if e.errno != errno.EEXIST: raise - return cachedir - def get_artifact_cache_server(settings): # pragma: no cover if settings['artifact-cache-server']: @@ -141,17 +136,8 @@ def new_artifact_caches(settings): # pragma: no cover ''' - cachedir = create_cachedir(settings) - artifact_cachedir = os.path.join(cachedir, 'artifacts') - # Don't check the folder exists and handle the exception that happens in - # this case to avoid errors if the folder is created by something else - # just after the check. - try: - os.mkdir(artifact_cachedir) - except OSError as e: - if e.errno != errno.EEXIST: - raise - + artifact_cachedir = os.path.join(settings['cachedir'], 'artifacts') + ensure_directory_exists(artifact_cachedir) lac = morphlib.localartifactcache.LocalArtifactCache( fs.osfs.OSFS(artifact_cachedir)) @@ -222,24 +208,26 @@ def combine_aliases(app): # pragma: no cover return alias_map.values() -def new_repo_caches(app): # pragma: no cover - '''Create new objects for local, remote git repository caches.''' - aliases = app.settings['repo-alias'] - cachedir = create_cachedir(app.settings) - gits_dir = os.path.join(cachedir, 'gits') - tarball_base_url = app.settings['tarball-server'] - repo_resolver = morphlib.repoaliasresolver.RepoAliasResolver(aliases) - lrc = morphlib.localrepocache.LocalRepoCache( - app, gits_dir, repo_resolver, tarball_base_url=tarball_base_url) +def new_repo_cache(app): # pragma: no cover + '''Create a RepoCache instance using settings from app.settings.''' - url = get_git_resolve_cache_server(app.settings) - if url: - rrc = morphlib.remoterepocache.RemoteRepoCache(url, repo_resolver) - else: - rrc = None + gits_dir = os.path.join(app.settings['cachedir'], 'gits') + tarball_base_url = app.settings['tarball-server'] + git_resolve_cache_url = get_git_resolve_cache_server(app.settings) + aliases = app.settings['repo-alias'] + repo_alias_resolver = morphlib.repoaliasresolver.RepoAliasResolver(aliases) + + return morphlib.repocache.RepoCache( + gits_dir, repo_alias_resolver, + tarball_base_url=tarball_base_url, + git_resolve_cache_url=git_resolve_cache_url, + update_gits=(not app.settings['no-git-update']), + runcmd_cb=app.runcmd, + status_cb=app.status, + verbose=app.settings['verbose'], + debug=app.settings['debug']) - return lrc, rrc def env_variable_is_password(key): # pragma: no cover return 'PASSWORD' in key |