|
a/scripts/restore_project.py |
|
b/scripts/restore_project.py |
1 |
import os
|
1 |
import os
|
2 |
import sys
|
2 |
import sys
|
3 |
import struct
|
3 |
import struct
|
4 |
import logging
|
4 |
import logging
|
5 |
|
5 |
|
|
|
6 |
from ming.schema import Invalid
|
6 |
from ming.orm import state, session, mapper, MappedClass
|
7 |
from ming.orm import state, session, mapper, MappedClass
|
7 |
from ming.orm.base import instrument, DocumentTracker
|
8 |
from ming.orm.base import instrument, DocumentTracker
|
8 |
|
9 |
|
9 |
from pylons import c
|
10 |
from pylons import c
|
10 |
from bson import BSON
|
11 |
from bson import BSON
|
|
... |
|
... |
34 |
return 2
|
35 |
return 2
|
35 |
project.shortname = new_shortname
|
36 |
project.shortname = new_shortname
|
36 |
project.set_tool_data('sfx', unix_group_name=new_unix_group_name)
|
37 |
project.set_tool_data('sfx', unix_group_name=new_unix_group_name)
|
37 |
project.deleted = False
|
38 |
project.deleted = False
|
38 |
c.project = project
|
39 |
c.project = project
|
39 |
for name, cls in MappedClass._registry.iteritems():
|
40 |
conn = M.main_doc_session.db.connection
|
40 |
if session(cls) is None: continue
|
41 |
repo_collections = get_repo_collections()
|
41 |
m = mapper(cls)
|
42 |
for dbname in os.listdir(dirname):
|
42 |
sess = session(cls).impl
|
43 |
if dbname.endswith('.bson'): continue
|
43 |
fname = os.path.join(dirname, '%s.bson' % (cls.__mongometa__.name))
|
44 |
for fname in os.listdir(os.path.join(dirname, dbname)):
|
44 |
if not os.path.exists(fname): continue
|
45 |
cname = os.path.splitext(fname)[0]
|
45 |
if ('project_id' not in m.property_index
|
46 |
collection = conn[dbname][cname]
|
46 |
and 'app_config_id' not in m.property_index): continue
|
47 |
with open(os.path.join(dirname, dbname, fname), 'rb') as fp:
|
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
|
48 |
num_objects = 0
|
53 |
sess.insert(m.doc_cls(doc))
|
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
|
54 |
log.info('%s: loaded %s objects from %s',
|
58 |
log.info('%s: loaded %s objects from %s',
|
55 |
name, num_objects, fname)
|
59 |
dbname, num_objects, fname)
|
56 |
session(project).flush()
|
60 |
session(project).flush()
|
57 |
reindex= ReindexCommand('reindex')
|
61 |
reindex= ReindexCommand('reindex')
|
58 |
reindex.run(['--project', new_shortname])
|
62 |
reindex.run(['--project', new_shortname])
|
59 |
return 0
|
63 |
return 0
|
|
|
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
|
60 |
|
71 |
|
61 |
def _read_bson(fp):
|
72 |
def _read_bson(fp):
|
62 |
slen = fp.read(4)
|
73 |
slen = fp.read(4)
|
63 |
if not slen: return None
|
74 |
if not slen: return None
|
64 |
bson = BSON(fp.read(struct.unpack('!l', slen)[0]))
|
75 |
bson = BSON(fp.read(struct.unpack('!l', slen)[0]))
|