Skip to content

Commit

Permalink
seemed to have missed some commits
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed May 20, 2015
1 parent 2baa1b5 commit a7e094f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
5 changes: 5 additions & 0 deletions elasticgit/istorage.py
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions elasticgit/search.py
Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions elasticgit/storage/local.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
12 changes: 8 additions & 4 deletions elasticgit/storage/remote.py
Expand Up @@ -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)
Expand All @@ -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
Expand Down
65 changes: 49 additions & 16 deletions elasticgit/workspace.py
@@ -1,13 +1,14 @@
import os
import warnings
from urlparse import urljoin

from unidecode import unidecode

from git import Repo

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
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -246,15 +244,15 @@ 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):
"""
Check if the index is ready
: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):
"""
Expand Down Expand Up @@ -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):
"""
Expand All @@ -305,15 +303,15 @@ 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):
"""
Get a mapping from Elasticsearch for a 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):
"""
Expand All @@ -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):

"""
Expand Down Expand Up @@ -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

0 comments on commit a7e094f

Please sign in to comment.