migrate-data 2.75 KB
Newer Older
1 2
#!/usr/bin/env python

3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (C) 2010-2014 GRNET S.A.
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
17 18 19

from binascii import hexlify

20
from sqlalchemy import Table
21 22 23 24 25
from sqlalchemy.sql import select

from pithos import settings
from pithos.backends.modular import ModularBackend

Antony Chazapis's avatar
Antony Chazapis committed
26
from pithos.tools.lib.hashmap import HashMap
27 28

from migrate import Migration, Cache
29

30 31
import os
    
32
class DataMigration(Migration):
33 34
    def __init__(self, pithosdb, db):
        Migration.__init__(self,  pithosdb)
35
        self.cache = Cache(db)
36
    
37
    def retrieve_files(self):
38
        # Loop for all available files.
39 40 41
        filebody = Table('filebody', self.metadata, autoload=True)
        s = select([filebody.c.storedfilepath])
        rp = self.conn.execute(s)
42 43 44 45
        path = rp.fetchone()
        while path:
            yield path
            path = rp.fetchone()
46
        rp.close()
47 48 49 50
    
    def execute(self):
        blocksize = self.backend.block_size
        blockhash = self.backend.hash_algorithm
51
        
52
        for (path,) in self.retrieve_files():
53
            map = HashMap(blocksize, blockhash)
54 55 56 57 58
            try:
                map.load(open(path))
            except Exception, e:
                print e
                continue
59 60
            hash = hexlify(map.hash())
            
61
            if hash != self.cache.get(path):
62 63 64 65 66 67 68 69 70 71 72 73
                missing = self.backend.blocker.block_ping(map) # XXX Backend hack...
                status = '[>] ' + path
                if missing:
                    status += ' - %d block(s) missing' % len(missing)
                    with open(path) as fp:
                        for h in missing:
                            offset = map.index(h) * blocksize
                            fp.seek(offset)
                            block = fp.read(blocksize)
                            self.backend.put_block(block)
                else:
                    status += ' - no blocks missing'
74
                self.cache.put(path, hash)
75 76 77
            else:
                status = '[-] ' + path
            print status
Antony Chazapis's avatar
Antony Chazapis committed
78

79
if __name__ == "__main__":
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
80
    pithosdb = 'postgresql://gss@127.0.0.1/pithos'
81 82
    db = 'sqlite:///migrate.db'
    
83
    dt = DataMigration(pithosdb, db)
84
    dt.execute()