Skip to content

Commit a7e094f

Browse files
committedMay 20, 2015
seemed to have missed some commits
1 parent 2baa1b5 commit a7e094f

File tree

5 files changed

+67
-24
lines changed

5 files changed

+67
-24
lines changed
 

‎elasticgit/istorage.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
class IStorageManager(Interface):
55

6+
def active_branch(self):
7+
"""
8+
Return the name of the currently active branch
9+
"""
10+
611
def write_config(section, data):
712
"""
813
Write a config block for a git repository.

‎elasticgit/search.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class ModelMappingType(MappingType, Indexable):
1010
@classmethod
1111
def get_index(cls):
1212
im = cls.im
13-
repo = cls.sm.repo
14-
return im.index_name(repo.active_branch.name)
13+
return im.index_name(cls.sm.active_branch())
1514

1615
@classmethod
1716
def get_mapping_type_name(cls):

‎elasticgit/storage/local.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def __init__(self, repo):
3737
self.workdir = self.repo.working_dir
3838
self.serializer = self.serializer_class()
3939

40+
def active_branch(self):
41+
return self.repo.active_branch.name
42+
4043
def git_path(self, model_class, *args):
4144
"""
4245
Return the path of a model_class when layed out in the git
@@ -154,8 +157,7 @@ def get_data(self, repo_path):
154157
:returns:
155158
str
156159
"""
157-
current_branch = self.repo.active_branch.name
158-
return self.repo.git.show('%s:%s' % (current_branch, repo_path))
160+
return self.repo.git.show('%s:%s' % (self.active_branch(), repo_path))
159161

160162
def get(self, model_class, uuid):
161163
"""

‎elasticgit/storage/remote.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, repo_url):
2222
self.repo_url = repo_url
2323
parse_result = urlparse(self.repo_url)
2424
self.scheme = parse_result.scheme
25-
self.host = parse_result.netloc
25+
self.netloc = parse_result.netloc
2626
self.port = parse_result.port
2727
self.dir_name = os.path.dirname(parse_result.path)
2828
basename = os.path.basename(parse_result.path)
@@ -34,13 +34,17 @@ def mk_request(self, *args, **kwargs):
3434
"""
3535
return requests.request(*args, **kwargs)
3636

37+
def active_branch(self):
38+
response = self.mk_request('GET', self.url())
39+
response.raise_for_status()
40+
return response.json()['branch']
41+
3742
def url(self, *parts):
3843
path = [self.repo_name]
3944
path.extend(parts)
40-
return '%s://%s%s%s/%s.%s' % (
45+
return '%s://%s%s/%s.%s' % (
4146
self.scheme,
42-
self.host,
43-
':%s' % (self.port,) if self.port else '',
47+
self.netloc,
4448
self.dir_name,
4549
'/'.join(path),
4650
self.suffix

‎elasticgit/workspace.py

+49-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import os
22
import warnings
3+
from urlparse import urljoin
34

45
from unidecode import unidecode
56

67
from git import Repo
78

89
from elasticutils import get_es, S as SBase, Q, F
910

10-
from elasticgit.storage import StorageManager
11+
from elasticgit.storage import StorageManager, RemoteStorageManager
1112
from elasticgit.search import ESManager
1213

1314
import logging
@@ -68,8 +69,8 @@ def setup(self, name, email):
6869
'email': email,
6970
})
7071

71-
if not self.im.index_exists(self.repo.active_branch.name):
72-
self.im.create_index(self.repo.active_branch.name)
72+
if not self.im.index_exists(self.sm.active_branch()):
73+
self.im.create_index(self.sm.active_branch())
7374

7475
def exists(self):
7576
"""
@@ -79,8 +80,7 @@ def exists(self):
7980
:returns: bool
8081
"""
8182
if self.sm.storage_exists():
82-
branch = self.sm.repo.active_branch
83-
return self.im.index_exists(branch.name)
83+
return self.im.index_exists(self.sm.active_branch())
8484

8585
return False
8686

@@ -90,9 +90,8 @@ def destroy(self):
9090
Guaranteed to remove things completely, use with caution.
9191
"""
9292
if self.sm.storage_exists():
93-
branch = self.sm.repo.active_branch
94-
if self.im.index_exists(branch.name):
95-
self.im.destroy_index(branch.name)
93+
if self.im.index_exists(self.sm.active_branch()):
94+
self.im.destroy_index(self.sm.active_branch())
9695
self.sm.destroy_storage()
9796

9897
def save(self, model, message, author=None, committer=None):
@@ -223,9 +222,8 @@ def reindex_iter(self, model_class, refresh_index=True):
223222
been indexed. Defaults to ``True``
224223
225224
"""
226-
branch = self.repo.active_branch
227-
if not self.im.index_exists(branch.name):
228-
self.im.create_index(branch.name)
225+
if not self.im.index_exists(self.sm.active_branch()):
226+
self.im.create_index(self.sm.active_branch())
229227
iterator = self.sm.iterate(model_class)
230228
for model in iterator:
231229
yield self.im.index(model)
@@ -246,15 +244,15 @@ def refresh_index(self):
246244
Manually refresh the Elasticsearch index. In production this is
247245
not necessary but it is useful when running tests.
248246
"""
249-
self.im.refresh_indices(self.repo.active_branch.name)
247+
self.im.refresh_indices(self.sm.active_branch())
250248

251249
def index_ready(self):
252250
"""
253251
Check if the index is ready
254252
255253
:returns: bool
256254
"""
257-
return self.im.index_ready(self.repo.active_branch.name)
255+
return self.im.index_ready(self.sm.active_branch())
258256

259257
def sync(self, model_class, refresh_index=True):
260258
"""
@@ -292,7 +290,7 @@ def setup_mapping(self, model_class):
292290
:param elasticgit.models.Model model_class:
293291
:returns: dict, the decoded dictionary from Elasticsearch
294292
"""
295-
return self.im.setup_mapping(self.repo.active_branch.name, model_class)
293+
return self.im.setup_mapping(self.sm.active_branch(), model_class)
296294

297295
def setup_custom_mapping(self, model_class, mapping):
298296
"""
@@ -305,15 +303,15 @@ def setup_custom_mapping(self, model_class, mapping):
305303
"""
306304

307305
return self.im.setup_custom_mapping(
308-
self.repo.active_branch.name, model_class, mapping)
306+
self.sm.active_branch(), model_class, mapping)
309307

310308
def get_mapping(self, model_class):
311309
"""
312310
Get a mapping from Elasticsearch for a model_class
313311
:param elasticgit.models.Model model_class:
314312
:returns: dict
315313
"""
316-
return self.im.get_mapping(self.repo.active_branch.name, model_class)
314+
return self.im.get_mapping(self.sm.active_branch(), model_class)
317315

318316
def S(self, model_class):
319317
"""
@@ -331,6 +329,40 @@ def S(self, model_class):
331329
self.im.get_mapping_type(model_class)).es(**self.es_settings)
332330

333331

332+
class RemoteWorkspace(Workspace):
333+
"""
334+
A workspace that connects to a unicore.distribute server hosted
335+
somewhere on the network.
336+
337+
This is a read only version of the :py:class:`Workspace`
338+
"""
339+
def __init__(self, url, es=None):
340+
"""
341+
:param str url:
342+
The URL of the unicore.distribute server.
343+
:param es dict:
344+
The parameters for connecting to Elasticsearch to. If not specified
345+
then the default unicore.distribute ES proxy would be used.
346+
This defaults to ``/esapi`` on the host of the ``url`` parameter
347+
provided.
348+
"""
349+
self.es_settings = es or {'urls': urljoin(url, '/esapi')}
350+
self.sm = RemoteStorageManager(url)
351+
self.im = ESManager(
352+
self.sm,
353+
es=get_es(**self.es_settings),
354+
index_prefix=self.sm.repo_name)
355+
356+
def pull(self, branch_name='master', remote_name='origin'):
357+
# TOOD: In the local storage we're diffing the changes pulled in
358+
# So that we can re-index those, unicore.distribute doesn't
359+
# expose that diff yet and so we cannot yet reindex.
360+
import warnings
361+
warnings.warn('Pulling without updating the index!')
362+
self.sm.pull(branch_name=branch_name,
363+
remote_name=remote_name)
364+
365+
334366
class EG(object):
335367

336368
"""
@@ -384,5 +416,6 @@ def init_repo(cls, workdir, bare=False):
384416
def clone_repo(cls, repo_url, workdir):
385417
return Repo.clone_from(repo_url, workdir)
386418

419+
387420
Q
388421
F

0 commit comments

Comments
 (0)
Please sign in to comment.