Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/carquinyol/migration.py
blob: f66dc2f2190129e967b0d5e0f70ec092b45a5e01 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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 time
import traceback
import uuid

import cjson

from carquinyol import layoutmanager, metadatastore

DATE_FORMAT = '%Y-%m-%dT%H:%M:%S'

mstore = metadatastore.MetadataStore()


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

    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):
        tree_id, ext = os.path.splitext(f)
        if ext != '.metadata':
            continue

        logging.debug('Migrating entry %r' % tree_id)
        version_id = str(uuid.uuid4())
        try:
            _migrate_metadata_0(root_path, old_root_path, tree_id, version_id)
            _migrate_file_0(root_path, old_root_path, tree_id, version_id)
            _migrate_preview_0(root_path, old_root_path, tree_id, version_id)
        except Exception:
            logging.error('Error while migrating entry %r: %s\n' % \
                          (tree_id, traceback.format_exc()))

    # 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_0(root_path, old_root_path, tree_id, version_id):
    dir_path = layoutmanager.get_instance().get_entry_path(tree_id, version_id)
    metadata_path = layoutmanager.get_instance().get_metadata_path(tree_id, version_id)

    old_metadata_path = os.path.join(old_root_path, tree_id + '.metadata')
    metadata = cjson.decode(open(old_metadata_path, 'r').read())

    metadata['tree_id'] = tree_id
    metadata['version_id'] = version_id
    metadata['parent_id'] = ''

    if 'timestamp' not in metadata and 'mtime' in metadata:
        metadata['ctime'] = \
                time.mktime(time.strptime(metadata['mtime'], DATE_FORMAT))
    elif 'timestamp' in metadata :
        metadata['ctime'] = metadata.pop('timestamp')
    else :
        metadata['ctime'] = os.stat(old_metadata_path).st_ctime

    try:
        mstore.store(tree_id, version_id, metadata)

    except:
        logging.error(
                'Error while migrating property entry %s: %s\n' % \
                (tree_id, traceback.format_exc()))

def _migrate_file_0(root_path, old_root_path, tree_id, version_id):
    if os.path.exists(os.path.join(old_root_path, tree_id)):
        new_data_path = layoutmanager.get_instance().get_data_path(tree_id, version_id)
        os.rename(os.path.join(old_root_path, tree_id),
                  new_data_path)

def _migrate_preview_0(root_path, old_root_path, tree_id, version_id):
    metadata_path = layoutmanager.get_instance().get_metadata_path(tree_id, version_id)
    os.rename(os.path.join(old_root_path, 'preview', tree_id),
              os.path.join(metadata_path, 'preview'))


def migrate_from_1():
    logging.info('Migrating datastore from version 1 to version 2')

    root_path = layoutmanager.get_instance().get_root_path()
    checksum_path = layoutmanager.get_instance().get_checksums_dir()

    version_ids = {}
    for hash02 in os.listdir(root_path):
        if len(hash02) != 2 :
            continue

        for tree_id in os.listdir(os.path.join(root_path, hash02)) :
            if (len(tree_id) != 36) :
                continue

            logging.debug('Migrating entry %r' % tree_id)

            version_id = str(uuid.uuid4())
            version_ids[tree_id] = version_id
            try:
                new_path = layoutmanager.get_instance().get_entry_path(tree_id, version_id)
                new_metadata_path = layoutmanager.get_instance().get_metadata_path(tree_id, version_id)
                os.rename(os.path.join(root_path, hash02, tree_id),
                          new_path)
                metadata = mstore.retrieve(tree_id, version_id)
                metadata['tree_id'] = tree_id
                metadata['version_id'] = version_id
                metadata['parent_id'] = ''
                metadata['ctime'] = metadata.get('timestamp', os.stat(new_path).st_ctime)
                metadata.pop('uid')
                mstore.store(tree_id, version_id, metadata)

            except Exception:
                logging.error('Error while migrating entry %r: %s\n' % \
                              (tree_id, traceback.format_exc()))

    for checksum in os.listdir(checksum_path) :
        entries_path = os.path.join(checksum_path, checksum)
        for tree_id in os.listdir(entries_path) :
            if len(tree_id) != 36 :
                continue

            try :
                os.rename(os.path.join(entries_path, tree_id),
                          layoutmanager.get_instance().get_entry_path(tree_id, version_ids[tree_id]))

            except Exception:
                logging.error('Error while migrating checksum entry %r / %r: %s\n' % \
                              (checksum, tree_id, traceback.format_exc()))

    logging.info('Migration finished')