You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by tv...@apache.org on 2013/06/18 15:31:45 UTC
git commit: [#6366] Add option for reindex tasks to use alternate db
Updated Branches:
refs/heads/tv/6366 [created] e86aea9ed
[#6366] Add option for reindex tasks to use alternate db
Signed-off-by: Tim Van Steenburgh <tv...@gmail.com>
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/e86aea9e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/e86aea9e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/e86aea9e
Branch: refs/heads/tv/6366
Commit: e86aea9ed659644cafbd92165feb71a35834e434
Parents: 7055812
Author: Tim Van Steenburgh <tv...@gmail.com>
Authored: Tue Jun 18 13:24:28 2013 +0000
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Tue Jun 18 13:29:48 2013 +0000
----------------------------------------------------------------------
Allura/allura/command/show_models.py | 30 +++++++++++++++++++++----
Allura/allura/lib/helpers.py | 37 +++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e86aea9e/Allura/allura/command/show_models.py
----------------------------------------------------------------------
diff --git a/Allura/allura/command/show_models.py b/Allura/allura/command/show_models.py
index 29d2091..9872c23 100644
--- a/Allura/allura/command/show_models.py
+++ b/Allura/allura/command/show_models.py
@@ -17,6 +17,7 @@
import sys
from collections import defaultdict
+from contextlib import contextmanager
from itertools import groupby
from pylons import tmpl_context as c, app_globals as g
@@ -27,9 +28,11 @@ from ming.orm.declarative import MappedClass
from allura.tasks.index_tasks import add_artifacts
from allura.lib.exceptions import CompoundError
+from allura.lib import helpers as h
from allura.lib import utils
from . import base
+
class ShowModelsCommand(base.Command):
min_args=1
max_args=1
@@ -44,6 +47,7 @@ class ShowModelsCommand(base.Command):
for line in dump_cls(depth, cls):
print line
+
class ReindexCommand(base.Command):
min_args=1
max_args=1
@@ -72,6 +76,8 @@ class ReindexCommand(base.Command):
help='Override the solr host(s) to post to. Comma-separated list of solr server URLs')
parser.add_option('--max-chunk', dest='max_chunk', type=int, default=100*1000,
help='Max number of artifacts to index in one Solr update command')
+ parser.add_option('--ming-config', dest='ming_config', help='Path (absolute, or relative to '
+ 'Allura root) to .ini file defining ming configuration.')
def command(self):
from allura import model as M
@@ -156,10 +162,11 @@ class ReindexCommand(base.Command):
mongo document is too large.
"""
try:
- add_artifacts.post(chunk,
- update_solr=self.options.solr,
- update_refs=self.options.refs,
- **self.add_artifact_kwargs)
+ with self.ming_config(self.options.ming_config):
+ add_artifacts.post(chunk,
+ update_solr=self.options.solr,
+ update_refs=self.options.refs,
+ **self.add_artifact_kwargs)
except InvalidDocument as e:
# there are many types of InvalidDocument, only recurse if its expected to help
if str(e).startswith('BSON document too large'):
@@ -168,6 +175,21 @@ class ReindexCommand(base.Command):
else:
raise
+ @property
+ def ming_config(self):
+ """Return a contextmanager for the provided ming config, if there is
+ one.
+
+ Otherwise return a no-op contextmanager.
+
+ """
+ def noop_cm(*args):
+ yield
+
+ if self.options.ming_config:
+ return h.ming_config_from_ini
+ return contextmanager(noop_cm)
+
class EnsureIndexCommand(base.Command):
min_args=1
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e86aea9e/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 9f87115..ab506d5 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -33,9 +33,11 @@ from collections import defaultdict
import tg
import genshi.template
import chardet
+import pkg_resources
from formencode.validators import FancyValidator
from dateutil.parser import parse
from bson import ObjectId
+from paste.deploy import appconfig
from pymongo.errors import InvalidId
from contextlib import contextmanager
from pylons import tmpl_context as c, app_globals as g
@@ -791,3 +793,38 @@ def topological_sort(items, partial_order):
# There is a loop in the input.
return None
return sorted
+
+
+@contextmanager
+def ming_config(**conf):
+ """Temporarily swap in a new ming configuration, restoring the previous
+ one when the contextmanager exits.
+
+ :param \*\*conf: keyword arguments defining the new ming configuration
+
+ """
+ import ming
+ from ming.session import Session
+ datastores = Session._datastores
+ try:
+ ming.configure(**conf)
+ yield
+ finally:
+ Session._datastores = datastores
+ for name, session in Session._registry.iteritems():
+ session.bind = datastores.get(name, None)
+ session._name = name
+
+
+@contextmanager
+def ming_config_from_ini(ini_path):
+ """Temporarily swap in a new ming configuration, restoring the previous
+ one when the contextmanager exits.
+
+ :param ini_path: Path to ini file containing the ming configuration
+
+ """
+ root = pkg_resources.get_distribution('allura').location
+ conf = appconfig('config:%s' % os.path.join(root, ini_path))
+ with ming_config(**conf):
+ yield