Switch to unified view

a/scripts/backup_project.py b/scripts/backup_project.py
...
...
4
import logging
4
import logging
5
5
6
from pylons import c
6
from pylons import c
7
from bson import BSON
7
from bson import BSON
8
8
9
from ming.orm import Mapper, state, mapper
9
from ming.orm import Mapper, state
10
10
11
from allura import model as M
11
from allura import model as M
12
12
13
log = logging.getLogger(__name__)
13
log = logging.getLogger(__name__)
14
14
...
...
48
    app_config_ids = [
48
    app_config_ids = [
49
        ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ]
49
        ac._id for ac in M.AppConfig.query.find(dict(project_id=c.project._id)) ]
50
    visited_collections = {}
50
    visited_collections = {}
51
    for m in Mapper.all_mappers():
51
    for m in Mapper.all_mappers():
52
        cls = m.mapped_class
52
        cls = m.mapped_class
53
        cname = cls.name
53
        cname = cls.__module__ + '.' + cls.__name__
54
        sess = m.session
54
        sess = m.session
55
        if sess is None:
55
        if sess is None:
56
            log.info('Skipping %s which has no session', cls)
56
            log.info('Skipping %s which has no session', cls)
57
            continue
57
            continue
58
        dbname = sess.impl.db.name
58
        dbname = sess.impl.db.name
...
...
60
        if fqname in visited_collections:
60
        if fqname in visited_collections:
61
            log.info('Skipping %s (already dumped collection %s in %s)',
61
            log.info('Skipping %s (already dumped collection %s in %s)',
62
                     cls, fqname, visited_collections[fqname])
62
                     cls, fqname, visited_collections[fqname])
63
            continue
63
            continue
64
        visited_collections[fqname] = cls
64
        visited_collections[fqname] = cls
65
        if 'project_id' in mapper(cls).property_index:
65
        if 'project_id' in m.property_index:
66
            # Dump the things directly related to the project
66
            # Dump the things directly related to the project
67
            oq = cls.query.find(dict(project_id=project._id))
67
            oq = cls.query.find(dict(project_id=project._id))
68
        elif 'app_config_id' in mapper(cls).property_index:
68
        elif 'app_config_id' in m.property_index:
69
            # ... and the things related to its apps
69
            # ... and the things related to its apps
70
            oq = cls.query.find(dict(app_config_id={'$in':app_config_ids}))
70
            oq = cls.query.find(dict(app_config_id={'$in':app_config_ids}))
71
        else:
71
        else:
72
            # Don't dump other things
72
            # Don't dump other things
73
            continue
73
            continue
...
...
78
        fname = os.path.join(
78
        fname = os.path.join(
79
            dirname,
79
            dirname,
80
            dbname,
80
            dbname,
81
            '%s.bson' % (cls.__mongometa__.name))
81
            '%s.bson' % (cls.__mongometa__.name))
82
        log.info('%s: dumping %s objects to %s',
82
        log.info('%s: dumping %s objects to %s',
83
                 name, num_objs, fname)
83
                 cname, num_objs, fname)
84
        with open(os.path.join(dirname, fname), 'w') as fp:
84
        with open(os.path.join(dirname, fname), 'w') as fp:
85
            for obj in oq.ming_cursor: _write_bson(fp, obj)
85
            for obj in oq.ming_cursor: _write_bson(fp, obj)
86
86
87
if __name__ == '__main__':
87
if __name__ == '__main__':
88
    sys.exit(main())
88
    sys.exit(main())