allura
リビジョン | 830553c95213cf9a9fc87c6ceb037bbad327f599 (tree) |
---|---|
日時 | 2011-04-30 05:11:32 |
作者 | John Hoffmann <jwh@geek...> |
コミッター | John Hoffmann |
[#1692] Merge, conflict in requirements-common.txt
Signed-off-by: John Hoffmann <jwh@geek.net>
@@ -12,6 +12,8 @@ from paste.registry import Registry | ||
12 | 12 | import ming |
13 | 13 | from allura.config.environment import load_environment |
14 | 14 | |
15 | +log = None | |
16 | + | |
15 | 17 | class EmptyClass(object): pass |
16 | 18 | |
17 | 19 | class Command(command.Command): |
@@ -140,14 +140,20 @@ class Repository(Artifact): | ||
140 | 140 | def __init__(self, **kw): |
141 | 141 | if 'name' in kw and 'tool' in kw: |
142 | 142 | if 'fs_path' not in kw: |
143 | - repos_root = tg.config.get('scm.repos.root', '/') | |
144 | - kw['fs_path'] = os.path.join(repos_root, | |
145 | - kw['tool'], | |
146 | - c.project.url()[1:]) | |
143 | + kw['fs_path'] = self.default_fs_path(c.project, kw['tool']) | |
147 | 144 | if 'url_path' not in kw: |
148 | - kw['url_path'] = c.project.url() | |
145 | + kw['url_path'] = self.default_url_path(c.project, kw['tool']) | |
149 | 146 | super(Repository, self).__init__(**kw) |
150 | 147 | |
148 | + @classmethod | |
149 | + def default_fs_path(cls, project, tool): | |
150 | + repos_root = tg.config.get('scm.repos.root', '/') | |
151 | + return os.path.join(repos_root, tool, project.url()[1:]) | |
152 | + | |
153 | + @classmethod | |
154 | + def default_url_path(cls, project, tool): | |
155 | + return project.url() | |
156 | + | |
151 | 157 | def __repr__(self): # pragma no cover |
152 | 158 | return '<%s %s>' % ( |
153 | 159 | self.__class__.__name__, |
@@ -47,7 +47,20 @@ def dump_project(project, dirname): | ||
47 | 47 | c.project = project |
48 | 48 | app_config_ids = [ |
49 | 49 | ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ] |
50 | + visited_collections = {} | |
50 | 51 | for name, cls in MappedClass._registry.iteritems(): |
52 | + cname = cls.__mongometa__.name | |
53 | + sess = cls.__mongometa__.session | |
54 | + if sess is None: | |
55 | + log.info('Skipping %s which has no session', cls) | |
56 | + continue | |
57 | + dbname = sess.impl.db.name | |
58 | + fqname = cname + '/' + dbname | |
59 | + if fqname in visited_collections: | |
60 | + log.info('Skipping %s (already dumped collection %s in %s)', | |
61 | + cls, fqname, visited_collections[fqname]) | |
62 | + continue | |
63 | + visited_collections[fqname] = cls | |
51 | 64 | if 'project_id' in mapper(cls).property_index: |
52 | 65 | # Dump the things directly related to the project |
53 | 66 | oq = cls.query.find(dict(project_id=project._id)) |
@@ -59,8 +72,11 @@ def dump_project(project, dirname): | ||
59 | 72 | continue |
60 | 73 | num_objs = oq.count() |
61 | 74 | if num_objs == 0: continue |
75 | + if not os.path.exists(os.path.join(dirname, dbname)): | |
76 | + os.mkdir(os.path.join(dirname, dbname)) | |
62 | 77 | fname = os.path.join( |
63 | 78 | dirname, |
79 | + dbname, | |
64 | 80 | '%s.bson' % (cls.__mongometa__.name)) |
65 | 81 | log.info('%s: dumping %s objects to %s', |
66 | 82 | name, num_objs, fname) |
@@ -3,6 +3,7 @@ import sys | ||
3 | 3 | import struct |
4 | 4 | import logging |
5 | 5 | |
6 | +from ming.schema import Invalid | |
6 | 7 | from ming.orm import state, session, mapper, MappedClass |
7 | 8 | from ming.orm.base import instrument, DocumentTracker |
8 | 9 |
@@ -36,28 +37,38 @@ def restore_project(dirname, new_shortname, new_unix_group_name): | ||
36 | 37 | project.set_tool_data('sfx', unix_group_name=new_unix_group_name) |
37 | 38 | project.deleted = False |
38 | 39 | c.project = project |
39 | - for name, cls in MappedClass._registry.iteritems(): | |
40 | - if session(cls) is None: continue | |
41 | - m = mapper(cls) | |
42 | - sess = session(cls).impl | |
43 | - fname = os.path.join(dirname, '%s.bson' % (cls.__mongometa__.name)) | |
44 | - if not os.path.exists(fname): continue | |
45 | - if ('project_id' not in m.property_index | |
46 | - and 'app_config_id' not in m.property_index): continue | |
47 | - with open(fname, 'rb') as fp: | |
48 | - num_objects = 0 | |
49 | - while True: | |
50 | - doc = _read_bson(fp) | |
51 | - if doc is None: break | |
52 | - num_objects += 1 | |
53 | - sess.insert(m.doc_cls(doc)) | |
54 | - log.info('%s: loaded %s objects from %s', | |
55 | - name, num_objects, fname) | |
40 | + conn = M.main_doc_session.db.connection | |
41 | + repo_collections = get_repo_collections() | |
42 | + for dbname in os.listdir(dirname): | |
43 | + if dbname.endswith('.bson'): continue | |
44 | + for fname in os.listdir(os.path.join(dirname, dbname)): | |
45 | + cname = os.path.splitext(fname)[0] | |
46 | + collection = conn[dbname][cname] | |
47 | + with open(os.path.join(dirname, dbname, fname), 'rb') as fp: | |
48 | + num_objects = 0 | |
49 | + while True: | |
50 | + doc = _read_bson(fp) | |
51 | + if doc is None: break | |
52 | + if cname in repo_collections: | |
53 | + cls = repo_collections[cname] | |
54 | + doc['fs_path'] = cls.default_fs_path(project, doc['tool']) | |
55 | + doc['url_path'] = cls.default_url_path(project, doc['tool']) | |
56 | + collection.insert(doc) | |
57 | + num_objects += 1 | |
58 | + log.info('%s: loaded %s objects from %s', | |
59 | + dbname, num_objects, fname) | |
56 | 60 | session(project).flush() |
57 | 61 | reindex= ReindexCommand('reindex') |
58 | 62 | reindex.run(['--project', new_shortname]) |
59 | 63 | return 0 |
60 | 64 | |
65 | +def get_repo_collections(): | |
66 | + res = {} | |
67 | + for name, cls in MappedClass._registry.iteritems(): | |
68 | + cname = cls.__mongometa__.name | |
69 | + if issubclass(cls, M.Repository): res[cname] = cls | |
70 | + return res | |
71 | + | |
61 | 72 | def _read_bson(fp): |
62 | 73 | slen = fp.read(4) |
63 | 74 | if not slen: return None |