Statistics
| Branch: | Tag: | Revision:

root / other / migrate.py @ 17ad5d37

History | View | Annotate | Download (3 kB)

1 98137a34 Sofia Papagiannaki
#!/usr/bin/env python
2 98137a34 Sofia Papagiannaki
3 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
4 2715ade4 Sofia Papagiannaki
#
5 98137a34 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
6 98137a34 Sofia Papagiannaki
# without modification, are permitted provided that the following
7 98137a34 Sofia Papagiannaki
# conditions are met:
8 2715ade4 Sofia Papagiannaki
#
9 98137a34 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
10 98137a34 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
11 98137a34 Sofia Papagiannaki
#      disclaimer.
12 2715ade4 Sofia Papagiannaki
#
13 98137a34 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
14 98137a34 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
15 98137a34 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
16 98137a34 Sofia Papagiannaki
#      provided with the distribution.
17 2715ade4 Sofia Papagiannaki
#
18 98137a34 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 98137a34 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 98137a34 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 98137a34 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 98137a34 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 98137a34 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 98137a34 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 98137a34 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 98137a34 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 98137a34 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 98137a34 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 98137a34 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
30 2715ade4 Sofia Papagiannaki
#
31 98137a34 Sofia Papagiannaki
# The views and conclusions contained in the software and
32 98137a34 Sofia Papagiannaki
# documentation are those of the authors and should not be
33 98137a34 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
34 98137a34 Sofia Papagiannaki
# or implied, of GRNET S.A.
35 98137a34 Sofia Papagiannaki
36 98137a34 Sofia Papagiannaki
from sqlalchemy import create_engine
37 f6c0005f Sofia Papagiannaki
from sqlalchemy import Table, Column, String, MetaData
38 f6c0005f Sofia Papagiannaki
from sqlalchemy.sql import select
39 98137a34 Sofia Papagiannaki
40 2db16f05 Sofia Papagiannaki
from django.conf import settings
41 2db16f05 Sofia Papagiannaki
42 98137a34 Sofia Papagiannaki
from pithos.backends.modular import ModularBackend
43 98137a34 Sofia Papagiannaki
44 2715ade4 Sofia Papagiannaki
45 98137a34 Sofia Papagiannaki
class Migration(object):
46 98137a34 Sofia Papagiannaki
    def __init__(self, db):
47 98137a34 Sofia Papagiannaki
        self.engine = create_engine(db)
48 98137a34 Sofia Papagiannaki
        self.metadata = MetaData(self.engine)
49 98137a34 Sofia Papagiannaki
        #self.engine.echo = True
50 98137a34 Sofia Papagiannaki
        self.conn = self.engine.connect()
51 2715ade4 Sofia Papagiannaki
52 2db16f05 Sofia Papagiannaki
        options = getattr(settings, 'BACKEND', None)[1]
53 2db16f05 Sofia Papagiannaki
        self.backend = ModularBackend(*options)
54 2715ade4 Sofia Papagiannaki
55 98137a34 Sofia Papagiannaki
    def execute(self):
56 f6c0005f Sofia Papagiannaki
        pass
57 f6c0005f Sofia Papagiannaki
58 2715ade4 Sofia Papagiannaki
59 f6c0005f Sofia Papagiannaki
class Cache():
60 f6c0005f Sofia Papagiannaki
    def __init__(self, db):
61 f6c0005f Sofia Papagiannaki
        self.engine = create_engine(db)
62 f6c0005f Sofia Papagiannaki
        metadata = MetaData(self.engine)
63 2715ade4 Sofia Papagiannaki
64 2715ade4 Sofia Papagiannaki
        columns = []
65 f6c0005f Sofia Papagiannaki
        columns.append(Column('path', String(2048), primary_key=True))
66 f6c0005f Sofia Papagiannaki
        columns.append(Column('hash', String(255)))
67 f6c0005f Sofia Papagiannaki
        self.files = Table('files', metadata, *columns)
68 f6c0005f Sofia Papagiannaki
        self.conn = self.engine.connect()
69 f6c0005f Sofia Papagiannaki
        self.engine.echo = True
70 f6c0005f Sofia Papagiannaki
        metadata.create_all(self.engine)
71 2715ade4 Sofia Papagiannaki
72 f6c0005f Sofia Papagiannaki
    def put(self, path, hash):
73 f6c0005f Sofia Papagiannaki
        # Insert or replace.
74 2715ade4 Sofia Papagiannaki
        s = self.files.delete().where(self.files.c.path == path)
75 f6c0005f Sofia Papagiannaki
        r = self.conn.execute(s)
76 f6c0005f Sofia Papagiannaki
        r.close()
77 f6c0005f Sofia Papagiannaki
        s = self.files.insert()
78 f6c0005f Sofia Papagiannaki
        r = self.conn.execute(s, {'path': path, 'hash': hash})
79 f6c0005f Sofia Papagiannaki
        r.close()
80 2715ade4 Sofia Papagiannaki
81 f6c0005f Sofia Papagiannaki
    def get(self, path):
82 f6c0005f Sofia Papagiannaki
        s = select([self.files.c.hash], self.files.c.path == path)
83 f6c0005f Sofia Papagiannaki
        r = self.conn.execute(s)
84 f6c0005f Sofia Papagiannaki
        l = r.fetchone()
85 f6c0005f Sofia Papagiannaki
        r.close()
86 f6c0005f Sofia Papagiannaki
        if not l:
87 f6c0005f Sofia Papagiannaki
            return l
88 2e662088 Antony Chazapis
        return l[0]