From a7e094f55d0669b81b6c6671a272c5b5afd23af3 Mon Sep 17 00:00:00 2001 From: Simon de Haan Date: Wed, 20 May 2015 11:15:02 +0200 Subject: [PATCH] seemed to have missed some commits --- elasticgit/istorage.py | 5 +++ elasticgit/search.py | 3 +- elasticgit/storage/local.py | 6 ++-- elasticgit/storage/remote.py | 12 ++++--- elasticgit/workspace.py | 65 +++++++++++++++++++++++++++--------- 5 files changed, 67 insertions(+), 24 deletions(-) diff --git a/elasticgit/istorage.py b/elasticgit/istorage.py index d118f98..7668067 100644 --- a/elasticgit/istorage.py +++ b/elasticgit/istorage.py @@ -3,6 +3,11 @@ class IStorageManager(Interface): + def active_branch(self): + """ + Return the name of the currently active branch + """ + def write_config(section, data): """ Write a config block for a git repository. diff --git a/elasticgit/search.py b/elasticgit/search.py index f4065cc..1625193 100644 --- a/elasticgit/search.py +++ b/elasticgit/search.py @@ -10,8 +10,7 @@ class ModelMappingType(MappingType, Indexable): @classmethod def get_index(cls): im = cls.im - repo = cls.sm.repo - return im.index_name(repo.active_branch.name) + return im.index_name(cls.sm.active_branch()) @classmethod def get_mapping_type_name(cls): diff --git a/elasticgit/storage/local.py b/elasticgit/storage/local.py index 835a414..7b1b0ac 100644 --- a/elasticgit/storage/local.py +++ b/elasticgit/storage/local.py @@ -37,6 +37,9 @@ def __init__(self, repo): self.workdir = self.repo.working_dir self.serializer = self.serializer_class() + def active_branch(self): + return self.repo.active_branch.name + def git_path(self, model_class, *args): """ Return the path of a model_class when layed out in the git @@ -154,8 +157,7 @@ def get_data(self, repo_path): :returns: str """ - current_branch = self.repo.active_branch.name - return self.repo.git.show('%s:%s' % (current_branch, repo_path)) + return self.repo.git.show('%s:%s' % (self.active_branch(), repo_path)) def get(self, model_class, uuid): """ diff --git a/elasticgit/storage/remote.py b/elasticgit/storage/remote.py index 7656fc7..4b8cdf2 100644 --- a/elasticgit/storage/remote.py +++ b/elasticgit/storage/remote.py @@ -22,7 +22,7 @@ def __init__(self, repo_url): self.repo_url = repo_url parse_result = urlparse(self.repo_url) self.scheme = parse_result.scheme - self.host = parse_result.netloc + self.netloc = parse_result.netloc self.port = parse_result.port self.dir_name = os.path.dirname(parse_result.path) basename = os.path.basename(parse_result.path) @@ -34,13 +34,17 @@ def mk_request(self, *args, **kwargs): """ return requests.request(*args, **kwargs) + def active_branch(self): + response = self.mk_request('GET', self.url()) + response.raise_for_status() + return response.json()['branch'] + def url(self, *parts): path = [self.repo_name] path.extend(parts) - return '%s://%s%s%s/%s.%s' % ( + return '%s://%s%s/%s.%s' % ( self.scheme, - self.host, - ':%s' % (self.port,) if self.port else '', + self.netloc, self.dir_name, '/'.join(path), self.suffix diff --git a/elasticgit/workspace.py b/elasticgit/workspace.py index 59928d6..15c9a3b 100644 --- a/elasticgit/workspace.py +++ b/elasticgit/workspace.py @@ -1,5 +1,6 @@ import os import warnings +from urlparse import urljoin from unidecode import unidecode @@ -7,7 +8,7 @@ from elasticutils import get_es, S as SBase, Q, F -from elasticgit.storage import StorageManager +from elasticgit.storage import StorageManager, RemoteStorageManager from elasticgit.search import ESManager import logging @@ -68,8 +69,8 @@ def setup(self, name, email): 'email': email, }) - if not self.im.index_exists(self.repo.active_branch.name): - self.im.create_index(self.repo.active_branch.name) + if not self.im.index_exists(self.sm.active_branch()): + self.im.create_index(self.sm.active_branch()) def exists(self): """ @@ -79,8 +80,7 @@ def exists(self): :returns: bool """ if self.sm.storage_exists(): - branch = self.sm.repo.active_branch - return self.im.index_exists(branch.name) + return self.im.index_exists(self.sm.active_branch()) return False @@ -90,9 +90,8 @@ def destroy(self): Guaranteed to remove things completely, use with caution. """ if self.sm.storage_exists(): - branch = self.sm.repo.active_branch - if self.im.index_exists(branch.name): - self.im.destroy_index(branch.name) + if self.im.index_exists(self.sm.active_branch()): + self.im.destroy_index(self.sm.active_branch()) self.sm.destroy_storage() def save(self, model, message, author=None, committer=None): @@ -223,9 +222,8 @@ def reindex_iter(self, model_class, refresh_index=True): been indexed. Defaults to ``True`` """ - branch = self.repo.active_branch - if not self.im.index_exists(branch.name): - self.im.create_index(branch.name) + if not self.im.index_exists(self.sm.active_branch()): + self.im.create_index(self.sm.active_branch()) iterator = self.sm.iterate(model_class) for model in iterator: yield self.im.index(model) @@ -246,7 +244,7 @@ def refresh_index(self): Manually refresh the Elasticsearch index. In production this is not necessary but it is useful when running tests. """ - self.im.refresh_indices(self.repo.active_branch.name) + self.im.refresh_indices(self.sm.active_branch()) def index_ready(self): """ @@ -254,7 +252,7 @@ def index_ready(self): :returns: bool """ - return self.im.index_ready(self.repo.active_branch.name) + return self.im.index_ready(self.sm.active_branch()) def sync(self, model_class, refresh_index=True): """ @@ -292,7 +290,7 @@ def setup_mapping(self, model_class): :param elasticgit.models.Model model_class: :returns: dict, the decoded dictionary from Elasticsearch """ - return self.im.setup_mapping(self.repo.active_branch.name, model_class) + return self.im.setup_mapping(self.sm.active_branch(), model_class) def setup_custom_mapping(self, model_class, mapping): """ @@ -305,7 +303,7 @@ def setup_custom_mapping(self, model_class, mapping): """ return self.im.setup_custom_mapping( - self.repo.active_branch.name, model_class, mapping) + self.sm.active_branch(), model_class, mapping) def get_mapping(self, model_class): """ @@ -313,7 +311,7 @@ def get_mapping(self, model_class): :param elasticgit.models.Model model_class: :returns: dict """ - return self.im.get_mapping(self.repo.active_branch.name, model_class) + return self.im.get_mapping(self.sm.active_branch(), model_class) def S(self, model_class): """ @@ -331,6 +329,40 @@ def S(self, model_class): self.im.get_mapping_type(model_class)).es(**self.es_settings) +class RemoteWorkspace(Workspace): + """ + A workspace that connects to a unicore.distribute server hosted + somewhere on the network. + + This is a read only version of the :py:class:`Workspace` + """ + def __init__(self, url, es=None): + """ + :param str url: + The URL of the unicore.distribute server. + :param es dict: + The parameters for connecting to Elasticsearch to. If not specified + then the default unicore.distribute ES proxy would be used. + This defaults to ``/esapi`` on the host of the ``url`` parameter + provided. + """ + self.es_settings = es or {'urls': urljoin(url, '/esapi')} + self.sm = RemoteStorageManager(url) + self.im = ESManager( + self.sm, + es=get_es(**self.es_settings), + index_prefix=self.sm.repo_name) + + def pull(self, branch_name='master', remote_name='origin'): + # TOOD: In the local storage we're diffing the changes pulled in + # So that we can re-index those, unicore.distribute doesn't + # expose that diff yet and so we cannot yet reindex. + import warnings + warnings.warn('Pulling without updating the index!') + self.sm.pull(branch_name=branch_name, + remote_name=remote_name) + + class EG(object): """ @@ -384,5 +416,6 @@ def init_repo(cls, workdir, bare=False): def clone_repo(cls, repo_url, workdir): return Repo.clone_from(repo_url, workdir) + Q F