Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/olpc/datastore/migration.py
blob: 8846cf16b9a5782ff38572bba828a53ea6f32fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2008, One Laptop Per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""Transform one DataStore directory in a newer format.
""" 

import os
import logging
import shutil

import cjson

from olpc.datastore import layoutmanager

def migrate_from_0():
    logging.info('Migrating datastore from version 0 to version 1')

    root_path = layoutmanager.get_instance().get_root_path()
    old_root_path = os.path.join(root_path, 'store')
    if not os.path.exists(old_root_path):
        return

    for f in os.listdir(old_root_path):
        uid, ext = os.path.splitext(f)
        if ext != '.metadata':
            continue

        logging.debug('Migrating entry %r' % uid)
        try:
            _migrate_metadata(root_path, old_root_path, uid)
            _migrate_file(root_path, old_root_path, uid)
            _migrate_preview(root_path, old_root_path, uid)
        except Exception:
            #logging.warning('Failed to migrate entry %r:%s\n' %(uid, 
            #    ''.join(traceback.format_exception(*sys.exc_info()))))
            #
            # In production, we may choose to ignore errors when failing to
            # migrate some entries. But for now, raise them.
            raise

    # Just be paranoid, it's cheap.
    if old_root_path.endswith('datastore/store'):
        shutil.rmtree(old_root_path)

    logging.info('Migration finished')

def _migrate_metadata(root_path, old_root_path, uid):
    dir_path = layoutmanager.get_instance().get_entry_path(uid)
    metadata_path = os.path.join(dir_path, 'metadata')
    os.makedirs(metadata_path)

    old_metadata_path = os.path.join(old_root_path, uid + '.metadata')
    metadata = cjson.decode(open(old_metadata_path, 'r').read())
    if 'uid' not in metadata:
        metadata['uid'] = uid
    for key, value in metadata.items():
        f = open(os.path.join(metadata_path, key), 'w')
        try:
            if isinstance(value, unicode):
                value = value.encode('utf-8')
            if not isinstance(value, basestring):
                value = str(value)
            f.write(value)
        finally:
            f.close()

def _migrate_file(root_path, old_root_path, uid):
    if os.path.exists(os.path.join(old_root_path, uid)):
        dir_path = layoutmanager.get_instance().get_entry_path(uid)
        os.rename(os.path.join(old_root_path, uid),
                  os.path.join(dir_path, 'data'))

def _migrate_preview(root_path, old_root_path, uid):
    dir_path = layoutmanager.get_instance().get_entry_path(uid)
    metadata_path = os.path.join(dir_path, 'metadata')
    os.rename(os.path.join(old_root_path, 'preview', uid),
              os.path.join(metadata_path, 'preview'))