Skip to content
Snippets Groups Projects
  • Vangelis Koukis's avatar
    Switch license to GPLv3 · 02071b96
    Vangelis Koukis authored
    According to the decision of the GRNET Board of Directors,
    switch license to GPLv3.
    
    This commit will be propagated to the release
    and master branches based on git flow, and the next
    release will be licensed as GPLv3.
    02071b96
migrate-data 2.75 KiB
#!/usr/bin/env python

# 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/>.

from binascii import hexlify

from sqlalchemy import Table
from sqlalchemy.sql import select

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

from pithos.tools.lib.hashmap import HashMap

from migrate import Migration, Cache

import os
    
class DataMigration(Migration):
    def __init__(self, pithosdb, db):
        Migration.__init__(self,  pithosdb)
        self.cache = Cache(db)
    
    def retrieve_files(self):
        # Loop for all available files.
        filebody = Table('filebody', self.metadata, autoload=True)
        s = select([filebody.c.storedfilepath])
        rp = self.conn.execute(s)
        path = rp.fetchone()
        while path:
            yield path
            path = rp.fetchone()
        rp.close()
    
    def execute(self):
        blocksize = self.backend.block_size
        blockhash = self.backend.hash_algorithm
        
        for (path,) in self.retrieve_files():
            map = HashMap(blocksize, blockhash)
            try:
                map.load(open(path))
            except Exception, e:
                print e
                continue
            hash = hexlify(map.hash())
            
            if hash != self.cache.get(path):
                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'
                self.cache.put(path, hash)
            else:
                status = '[-] ' + path
            print status

if __name__ == "__main__":
    pithosdb = 'postgresql://gss@127.0.0.1/pithos'
    db = 'sqlite:///migrate.db'
    
    dt = DataMigration(pithosdb, db)
    dt.execute()