migrate.py 2.17 KB
Newer Older
1 2
#!/usr/bin/env python

3
# Copyright (C) 2010-2014 GRNET S.A.
4
#
5 6 7 8
# 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.
9
#
10 11 12 13
# 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.
14
#
15 16
# 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

from sqlalchemy import create_engine
19 20
from sqlalchemy import Table, Column, String, MetaData
from sqlalchemy.sql import select
21

22 23
from django.conf import settings

24 25
from pithos.backends.modular import ModularBackend

26

27 28 29 30 31 32
class Migration(object):
    def __init__(self, db):
        self.engine = create_engine(db)
        self.metadata = MetaData(self.engine)
        #self.engine.echo = True
        self.conn = self.engine.connect()
33

34 35
        options = getattr(settings, 'BACKEND', None)[1]
        self.backend = ModularBackend(*options)
36

37
    def execute(self):
38 39
        pass

40

41 42 43 44
class Cache():
    def __init__(self, db):
        self.engine = create_engine(db)
        metadata = MetaData(self.engine)
45 46

        columns = []
47 48 49 50 51 52
        columns.append(Column('path', String(2048), primary_key=True))
        columns.append(Column('hash', String(255)))
        self.files = Table('files', metadata, *columns)
        self.conn = self.engine.connect()
        self.engine.echo = True
        metadata.create_all(self.engine)
53

54 55
    def put(self, path, hash):
        # Insert or replace.
56
        s = self.files.delete().where(self.files.c.path == path)
57 58 59 60 61
        r = self.conn.execute(s)
        r.close()
        s = self.files.insert()
        r = self.conn.execute(s, {'path': path, 'hash': hash})
        r.close()
62

63 64 65 66 67 68 69
    def get(self, path):
        s = select([self.files.c.hash], self.files.c.path == path)
        r = self.conn.execute(s)
        l = r.fetchone()
        r.close()
        if not l:
            return l
Antony Chazapis's avatar
Antony Chazapis committed
70
        return l[0]