summaryrefslogtreecommitdiff
path: root/morphlib/remoterepocache.py
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2016-03-02 17:11:34 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2016-03-16 19:11:21 +0000
commitd58d8e8f7a4ec03ff14021a4515c8283dad52573 (patch)
tree5ece2d0524e4423bb953e6140831c9fde93b7219 /morphlib/remoterepocache.py
parent014a029ade9a045a839ca86c35690b218098ea33 (diff)
downloadmorph-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/remoterepocache.py')
-rw-r--r--morphlib/remoterepocache.py105
1 files changed, 0 insertions, 105 deletions
diff --git a/morphlib/remoterepocache.py b/morphlib/remoterepocache.py
deleted file mode 100644
index 4a6d9fe9..00000000
--- a/morphlib/remoterepocache.py
+++ /dev/null
@@ -1,105 +0,0 @@
-# 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
-# the Free Software Foundation; version 2 of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program. If not, see <http://www.gnu.org/licenses/>.
-
-
-import cliapp
-import json
-import logging
-import urllib2
-import urlparse
-import urllib
-
-
-class ResolveRefError(cliapp.AppException):
-
- def __init__(self, repo_name, ref):
- cliapp.AppException.__init__(
- self, 'Failed to resolve ref %s for repo %s' %
- (ref, repo_name))
-
-
-class CatFileError(cliapp.AppException):
-
- def __init__(self, repo_name, ref, filename):
- cliapp.AppException.__init__(
- self, 'Failed to cat file %s in ref %s of repo %s' %
- (filename, ref, repo_name))
-
-class LsTreeError(cliapp.AppException):
-
- def __init__(self, repo_name, ref):
- cliapp.AppException.__init__(
- self, 'Failed to list tree in ref %s of repo %s' %
- (ref, repo_name))
-
-
-class RemoteRepoCache(object):
-
- def __init__(self, server_url, resolver):
- self.server_url = server_url
- self._resolver = resolver
-
- def resolve_ref(self, repo_name, ref):
- repo_url = self._resolver.pull_url(repo_name)
- try:
- return self._resolve_ref_for_repo_url(repo_url, ref)
- except BaseException as e:
- logging.error('Caught exception: %s' % str(e))
- raise ResolveRefError(repo_name, ref)
-
- def cat_file(self, repo_name, ref, filename):
- repo_url = self._resolver.pull_url(repo_name)
- try:
- return self._cat_file_for_repo_url(repo_url, ref, filename)
- except urllib2.HTTPError as e:
- logging.error('Caught exception: %s' % str(e))
- if e.code == 404:
- raise CatFileError(repo_name, ref, filename)
- raise # pragma: no cover
-
- def ls_tree(self, repo_name, ref):
- repo_url = self._resolver.pull_url(repo_name)
- try:
- info = json.loads(self._ls_tree_for_repo_url(repo_url, ref))
- return info['tree'].keys()
- except BaseException as e:
- logging.error('Caught exception: %s' % str(e))
- raise LsTreeError(repo_name, ref)
-
- def _resolve_ref_for_repo_url(self, repo_url, ref): # pragma: no cover
- data = self._make_request(
- 'sha1s?repo=%s&ref=%s' % self._quote_strings(repo_url, ref))
- info = json.loads(data)
- return info['sha1'], info['tree']
-
- def _cat_file_for_repo_url(self, repo_url, ref,
- filename): # pragma: no cover
- return self._make_request(
- 'files?repo=%s&ref=%s&filename=%s'
- % self._quote_strings(repo_url, ref, filename))
-
- def _ls_tree_for_repo_url(self, repo_url, ref): # pragma: no cover
- return self._make_request(
- 'trees?repo=%s&ref=%s' % self._quote_strings(repo_url, ref))
-
- def _quote_strings(self, *args): # pragma: no cover
- return tuple(urllib.quote(string) for string in args)
-
- def _make_request(self, path): # pragma: no cover
- server_url = self.server_url
- if not server_url.endswith('/'):
- server_url += '/'
- url = urlparse.urljoin(server_url, '/1.0/%s' % path)
- handle = urllib2.urlopen(url)
- return handle.read()