Statistics
| Branch: | Tag: | Revision:

root / other / migrate-db @ 17ad5d37

History | View | Annotate | Download (13.2 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 98137a34 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 98137a34 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 98137a34 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 98137a34 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 98137a34 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 Table
37 93297831 Sofia Papagiannaki
from sqlalchemy.sql import select, and_
38 98137a34 Sofia Papagiannaki
39 98137a34 Sofia Papagiannaki
from binascii import hexlify
40 98137a34 Sofia Papagiannaki
41 98137a34 Sofia Papagiannaki
from pithos.backends.lib.hashfiler import Blocker
42 2db16f05 Sofia Papagiannaki
from pithos.backends.lib.sqlalchemy import Node
43 98137a34 Sofia Papagiannaki
44 98137a34 Sofia Papagiannaki
from django.conf import settings
45 98137a34 Sofia Papagiannaki
46 2db16f05 Sofia Papagiannaki
from pithos.backends.modular import CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED
47 5fdaf331 Sofia Papagiannaki
from pithos.backends.lib.sqlalchemy.node import Node, ROOTNODE
48 98137a34 Sofia Papagiannaki
49 6e147ecc Antony Chazapis
from pithos.tools.lib.transfer import upload
50 6e147ecc Antony Chazapis
from pithos.tools.lib.hashmap import HashMap
51 6e147ecc Antony Chazapis
from pithos.tools.lib.client import Fault
52 5a96180b Antony Chazapis
53 5a96180b Antony Chazapis
from migrate import Migration, Cache
54 5a96180b Antony Chazapis
55 f6c0005f Sofia Papagiannaki
from calendar import timegm
56 5fdaf331 Sofia Papagiannaki
from decimal import Decimal
57 5fdaf331 Sofia Papagiannaki
from collections import defaultdict
58 98137a34 Sofia Papagiannaki
59 98137a34 Sofia Papagiannaki
import json
60 98137a34 Sofia Papagiannaki
import os
61 98137a34 Sofia Papagiannaki
import sys
62 98137a34 Sofia Papagiannaki
import hashlib
63 2db16f05 Sofia Papagiannaki
import mimetypes
64 5fdaf331 Sofia Papagiannaki
import time
65 5fdaf331 Sofia Papagiannaki
import datetime
66 5fdaf331 Sofia Papagiannaki
67 5fdaf331 Sofia Papagiannaki
(ID, CREATIONDATE, MODIFICATIONDATE, DELETED, ICON, NAME, VERSION, CREATEDBY_ID, MODIFIEDBY_ID, OWNER_ID, PARENT_ID, READFORALL, SHARED, USER) = range(14)
68 98137a34 Sofia Papagiannaki
69 2db16f05 Sofia Papagiannaki
class ObjectMigration(Migration):
70 f6c0005f Sofia Papagiannaki
    def __init__(self, old_db, db, f):
71 2db16f05 Sofia Papagiannaki
        Migration.__init__(self, old_db)
72 f6c0005f Sofia Papagiannaki
        self.cache = Cache(db)
73 98137a34 Sofia Papagiannaki
    
74 5fdaf331 Sofia Papagiannaki
    def create_node(self, username, container, object):
75 5fdaf331 Sofia Papagiannaki
        node = self.backend.node.node_lookup(object)
76 5fdaf331 Sofia Papagiannaki
        if not node:
77 5fdaf331 Sofia Papagiannaki
            parent_path = '%s/%s' %(username, container)
78 5fdaf331 Sofia Papagiannaki
            parent_node = self.backend.node.node_lookup(parent_path)
79 5fdaf331 Sofia Papagiannaki
            if not parent_node:
80 5fdaf331 Sofia Papagiannaki
                raise Exception('Missing node')
81 5fdaf331 Sofia Papagiannaki
            node = self.backend.node.node_create(parent_node, object)
82 5fdaf331 Sofia Papagiannaki
        return node
83 2db16f05 Sofia Papagiannaki
    
84 f6c0005f Sofia Papagiannaki
    def create_history(self, header_id, node_id, deleted=False):
85 2db16f05 Sofia Papagiannaki
        i = 0
86 f6c0005f Sofia Papagiannaki
        map = HashMap(self.backend.block_size, self.backend.hash_algorithm)
87 5fdaf331 Sofia Papagiannaki
        v = []
88 5fdaf331 Sofia Papagiannaki
        stored_versions = self.backend.node.node_get_versions(node_id, ['mtime'])
89 5fdaf331 Sofia Papagiannaki
        stored_versions_mtime = [datetime.datetime.utcfromtimestamp(elem[0]) for elem in stored_versions]
90 f6c0005f Sofia Papagiannaki
        for t, rowcount  in self.retrieve_node_versions(header_id):
91 5fdaf331 Sofia Papagiannaki
            size, modyfied_by, filepath, mimetype, mdate = t
92 5fdaf331 Sofia Papagiannaki
            if mdate in stored_versions_mtime:
93 5fdaf331 Sofia Papagiannaki
                continue
94 f6c0005f Sofia Papagiannaki
            cluster = CLUSTER_HISTORY if i < rowcount - 1 else CLUSTER_NORMAL
95 2db16f05 Sofia Papagiannaki
            cluster = cluster if not deleted else CLUSTER_DELETED
96 f6c0005f Sofia Papagiannaki
            hash = self.cache.get(filepath)
97 f6c0005f Sofia Papagiannaki
            if hash == None:
98 5fdaf331 Sofia Papagiannaki
                raise Exception("Missing hash")
99 5fdaf331 Sofia Papagiannaki
            args = node_id, hash, size, modyfied_by, cluster, mimetype, mdate
100 5fdaf331 Sofia Papagiannaki
            v.append(self.create_version(*args))
101 2db16f05 Sofia Papagiannaki
            i += 1
102 5fdaf331 Sofia Papagiannaki
        return v
103 5fdaf331 Sofia Papagiannaki
    
104 5fdaf331 Sofia Papagiannaki
    def create_version(self, node_id, hash, size, modyfied_by, cluster, mimetype, mdate):
105 5fdaf331 Sofia Papagiannaki
        args = (node_id, hash, size, None, modyfied_by, cluster)
106 5fdaf331 Sofia Papagiannaki
        serial = self.backend.node.version_create(*args)[0]
107 5fdaf331 Sofia Papagiannaki
        meta = {'hash':hash,
108 5fdaf331 Sofia Papagiannaki
                'content-type':mimetype}
109 5fdaf331 Sofia Papagiannaki
        self.backend.node.attribute_set(serial, ((k, v) for k, v in meta.iteritems()))
110 5fdaf331 Sofia Papagiannaki
        timestamp = timegm(mdate.timetuple())
111 5fdaf331 Sofia Papagiannaki
        microseconds = mdate.time().microsecond
112 5fdaf331 Sofia Papagiannaki
        values = timestamp, microseconds, serial
113 5fdaf331 Sofia Papagiannaki
        f.write('update versions set mtime=\'%10d.%6d\' where serial=%s;' %values)
114 5fdaf331 Sofia Papagiannaki
        return serial
115 5fdaf331 Sofia Papagiannaki
    
116 5fdaf331 Sofia Papagiannaki
    def create_tags(self, header_id, node_id, vserials):
117 5fdaf331 Sofia Papagiannaki
        tags = self.retrieve_tags(header_id)
118 5fdaf331 Sofia Papagiannaki
        if not tags:
119 5fdaf331 Sofia Papagiannaki
            return
120 5fdaf331 Sofia Papagiannaki
        for v in vserials:
121 317c3efc Sofia Papagiannaki
            self.backend.node.attribute_set(v, (('X-Object-Meta-Tag', tags),))
122 2db16f05 Sofia Papagiannaki
    
123 93297831 Sofia Papagiannaki
    def create_permissions(self, fid, path, owner, is_folder=True):
124 93297831 Sofia Papagiannaki
        fpath, fpermissions = self.backend.permissions.access_inherit(path)
125 93297831 Sofia Papagiannaki
        permissions = self.retrieve_permissions(fid, is_folder)
126 93297831 Sofia Papagiannaki
        if not fpermissions:
127 5fdaf331 Sofia Papagiannaki
            keys = ('read', 'write')
128 5fdaf331 Sofia Papagiannaki
            for k in keys:
129 5fdaf331 Sofia Papagiannaki
                if owner in permissions[k]:
130 5fdaf331 Sofia Papagiannaki
                    permissions[k].remove(owner)
131 5fdaf331 Sofia Papagiannaki
            self.backend.permissions.access_set(path, permissions)
132 93297831 Sofia Papagiannaki
        else:
133 93297831 Sofia Papagiannaki
            keys = ('read', 'write')
134 93297831 Sofia Papagiannaki
            common_p = {}
135 93297831 Sofia Papagiannaki
            for k in keys:
136 93297831 Sofia Papagiannaki
                if owner in permissions[k]:
137 93297831 Sofia Papagiannaki
                    permissions[k].remove(owner)
138 93297831 Sofia Papagiannaki
                common = set(fpermissions[k]).intersection(set(permissions[k]))
139 93297831 Sofia Papagiannaki
                common_p[k] = list(common)
140 93297831 Sofia Papagiannaki
            #keep only the common permissions
141 93297831 Sofia Papagiannaki
            #trade off for securing access only to explicitly authorized users
142 93297831 Sofia Papagiannaki
            self.backend.permissions.access_set(fpath, common_p)
143 f6c0005f Sofia Papagiannaki
    
144 98137a34 Sofia Papagiannaki
    def create_objects(self):
145 5fdaf331 Sofia Papagiannaki
        for t in self.retrieve_current_nodes():
146 5fdaf331 Sofia Papagiannaki
            username, headerid, folderid, filename, deleted, filepath, mimetype, public, owner_id = t
147 5fdaf331 Sofia Papagiannaki
            containers = ['pithos', 'trash']
148 5fdaf331 Sofia Papagiannaki
            
149 5fdaf331 Sofia Papagiannaki
            for c in containers:
150 5fdaf331 Sofia Papagiannaki
                #create container if it does not exist
151 5fdaf331 Sofia Papagiannaki
                try:
152 5fdaf331 Sofia Papagiannaki
                    self.backend._lookup_container(username, c)
153 5fdaf331 Sofia Papagiannaki
                except NameError, e:
154 5fdaf331 Sofia Papagiannaki
                    self.backend.put_container(username, username, c) 
155 f6c0005f Sofia Papagiannaki
            
156 5fdaf331 Sofia Papagiannaki
            container = 'pithos' if not deleted else 'trash'
157 5fdaf331 Sofia Papagiannaki
            path = self.build_path(folderid)
158 f6c0005f Sofia Papagiannaki
            #create node
159 317c3efc Sofia Papagiannaki
            object = '%s/%s' %(username, container)
160 317c3efc Sofia Papagiannaki
            object = '%s/%s/%s' %(object, path, filename) if path else '%s/%s' %(object, filename)
161 317c3efc Sofia Papagiannaki
            args = username, container, object
162 5fdaf331 Sofia Papagiannaki
            nodeid = self.create_node(*args)
163 5fdaf331 Sofia Papagiannaki
            #create node history 
164 5fdaf331 Sofia Papagiannaki
            vserials = self.create_history(headerid, nodeid, deleted)
165 5fdaf331 Sofia Papagiannaki
            #set object tags
166 5fdaf331 Sofia Papagiannaki
            self.create_tags(headerid, nodeid, vserials)
167 5fdaf331 Sofia Papagiannaki
            #set object's publicity
168 5fdaf331 Sofia Papagiannaki
            if public:
169 5fdaf331 Sofia Papagiannaki
                self.backend.permissions.public_set(object)
170 5fdaf331 Sofia Papagiannaki
            #set object's permissions
171 93297831 Sofia Papagiannaki
            self.create_permissions(headerid, object, username, is_folder=False)
172 f6c0005f Sofia Papagiannaki
    
173 5fdaf331 Sofia Papagiannaki
    def build_path(self, child_id):
174 5fdaf331 Sofia Papagiannaki
        folder = Table('folder', self.metadata, autoload=True)
175 5fdaf331 Sofia Papagiannaki
        user = Table('gss_user', self.metadata, autoload=True)
176 5fdaf331 Sofia Papagiannaki
        j = folder.join(user, folder.c.owner_id == user.c.id)
177 5fdaf331 Sofia Papagiannaki
        s = select([folder, user.c.username], from_obj=j)
178 5fdaf331 Sofia Papagiannaki
        s = s.where(folder.c.id == child_id)
179 5fdaf331 Sofia Papagiannaki
        s.order_by(folder.c.modificationdate)
180 f6c0005f Sofia Papagiannaki
        rp = self.conn.execute(s)
181 5fdaf331 Sofia Papagiannaki
        t = rp.fetchone()
182 5fdaf331 Sofia Papagiannaki
        md5 = hashlib.md5()
183 5fdaf331 Sofia Papagiannaki
        hash = md5.hexdigest().lower()
184 5fdaf331 Sofia Papagiannaki
        size = 0
185 5fdaf331 Sofia Papagiannaki
        if not t[PARENT_ID]:
186 f6c0005f Sofia Papagiannaki
            return ''
187 f6c0005f Sofia Papagiannaki
        else:
188 5fdaf331 Sofia Papagiannaki
            container_path = t[USER]
189 5fdaf331 Sofia Papagiannaki
            container_path += '/trash' if t[DELETED] else '/pithos'
190 5fdaf331 Sofia Papagiannaki
            parent_node = self.backend.node.node_lookup(container_path)
191 5fdaf331 Sofia Papagiannaki
            if not parent_node:
192 5fdaf331 Sofia Papagiannaki
                raise Exception('Missing node:', container_path)
193 5fdaf331 Sofia Papagiannaki
            parent_path = self.build_path(t[PARENT_ID])
194 5fdaf331 Sofia Papagiannaki
            path = '%s/%s/%s' %(container_path, parent_path, t[NAME]) if parent_path else '%s/%s' %(container_path, t[NAME])
195 5fdaf331 Sofia Papagiannaki
            node = self.backend.node.node_lookup(path)
196 5fdaf331 Sofia Papagiannaki
            if not node:
197 5fdaf331 Sofia Papagiannaki
                node = self.backend.node.node_create(parent_node, path)
198 5fdaf331 Sofia Papagiannaki
                if not node:
199 5fdaf331 Sofia Papagiannaki
                    raise Exception('Unable to create node:', path)
200 5fdaf331 Sofia Papagiannaki
                
201 5fdaf331 Sofia Papagiannaki
                #create versions
202 5fdaf331 Sofia Papagiannaki
                v = self.create_version(node, hash, size, t[USER], CLUSTER_NORMAL, 'application/directory', t[CREATIONDATE])
203 5fdaf331 Sofia Papagiannaki
                if t[CREATIONDATE] != t[MODIFICATIONDATE]:
204 5fdaf331 Sofia Papagiannaki
                    self.backend.node.version_recluster(v, CLUSTER_HISTORY)
205 5fdaf331 Sofia Papagiannaki
                    self.create_version(node, hash, size, t[USER], CLUSTER_NORMAL, 'application/directory', t[MODIFICATIONDATE])
206 5fdaf331 Sofia Papagiannaki
                
207 5fdaf331 Sofia Papagiannaki
                #set permissions
208 93297831 Sofia Papagiannaki
                self.create_permissions(t[ID], path, t[USER], is_folder=True)
209 5fdaf331 Sofia Papagiannaki
            return '%s/%s' %(parent_path, t[NAME]) if parent_path else t[NAME]
210 f6c0005f Sofia Papagiannaki
    
211 f6c0005f Sofia Papagiannaki
    def retrieve_current_nodes(self):
212 98137a34 Sofia Papagiannaki
        fileheader = Table('fileheader', self.metadata, autoload=True)
213 98137a34 Sofia Papagiannaki
        filebody = Table('filebody', self.metadata, autoload=True)
214 98137a34 Sofia Papagiannaki
        folder = Table('folder', self.metadata, autoload=True)
215 98137a34 Sofia Papagiannaki
        gss_user = Table('gss_user', self.metadata, autoload=True)
216 2db16f05 Sofia Papagiannaki
        j = filebody.join(fileheader, filebody.c.id == fileheader.c.currentbody_id)
217 98137a34 Sofia Papagiannaki
        j = j.join(folder, fileheader.c.folder_id == folder.c.id)
218 98137a34 Sofia Papagiannaki
        j = j.join(gss_user, fileheader.c.owner_id == gss_user.c.id)
219 2db16f05 Sofia Papagiannaki
        s = select([gss_user.c.username,  fileheader.c.id, fileheader.c.folder_id,
220 5fdaf331 Sofia Papagiannaki
                    fileheader.c.name,  fileheader.c.deleted,
221 5fdaf331 Sofia Papagiannaki
                    filebody.c.storedfilepath, filebody.c.mimetype,
222 5fdaf331 Sofia Papagiannaki
                    fileheader.c.readforall, fileheader.c.owner_id], from_obj=j)
223 98137a34 Sofia Papagiannaki
        rp = self.conn.execute(s)
224 f6c0005f Sofia Papagiannaki
        object = rp.fetchone()
225 f6c0005f Sofia Papagiannaki
        while object:
226 f6c0005f Sofia Papagiannaki
            yield object
227 f6c0005f Sofia Papagiannaki
            object = rp.fetchone()
228 f6c0005f Sofia Papagiannaki
        rp.close()
229 98137a34 Sofia Papagiannaki
    
230 f6c0005f Sofia Papagiannaki
    def retrieve_node_versions(self, header_id):
231 f6c0005f Sofia Papagiannaki
        filebody = Table('filebody', self.metadata, autoload=True)
232 f6c0005f Sofia Papagiannaki
        gss_user = Table('gss_user', self.metadata, autoload=True)
233 f6c0005f Sofia Papagiannaki
        j = filebody.join(gss_user, filebody.c.modifiedby_id == gss_user.c.id)
234 f6c0005f Sofia Papagiannaki
        s = select([filebody.c.filesize, gss_user.c.username,
235 f6c0005f Sofia Papagiannaki
                    filebody.c.storedfilepath, filebody.c.mimetype,
236 f6c0005f Sofia Papagiannaki
                    filebody.c.modificationdate], from_obj=j)
237 f6c0005f Sofia Papagiannaki
        s = s.where(filebody.c.header_id == header_id)
238 f6c0005f Sofia Papagiannaki
        s = s.order_by(filebody.c.version)
239 f6c0005f Sofia Papagiannaki
        rp = self.conn.execute(s)
240 f6c0005f Sofia Papagiannaki
        version = rp.fetchone()
241 f6c0005f Sofia Papagiannaki
        while version:
242 f6c0005f Sofia Papagiannaki
            yield version, rp.rowcount
243 f6c0005f Sofia Papagiannaki
            version = rp.fetchone()
244 f6c0005f Sofia Papagiannaki
        rp.close()
245 98137a34 Sofia Papagiannaki
    
246 5fdaf331 Sofia Papagiannaki
    def retrieve_tags(self, header_id):
247 f6c0005f Sofia Papagiannaki
        filetag = Table('filetag', self.metadata, autoload=True)
248 5fdaf331 Sofia Papagiannaki
        s = select([filetag.c.tag], filetag.c.fileid == header_id)
249 f6c0005f Sofia Papagiannaki
        rp = self.conn.execute(s)
250 5fdaf331 Sofia Papagiannaki
        tags = rp.fetchall() if rp.returns_rows else []
251 5fdaf331 Sofia Papagiannaki
        tags = [elem[0] for elem in tags]
252 f6c0005f Sofia Papagiannaki
        rp.close()
253 5fdaf331 Sofia Papagiannaki
        return ','.join(tags) if tags else ''
254 98137a34 Sofia Papagiannaki
    
255 5fdaf331 Sofia Papagiannaki
    def retrieve_permissions(self, id, is_folder=True):
256 5fdaf331 Sofia Papagiannaki
        permissions = {}
257 5fdaf331 Sofia Papagiannaki
        if is_folder:
258 5fdaf331 Sofia Papagiannaki
            ftable = Table('folder_permission', self.metadata, autoload=True)
259 5fdaf331 Sofia Papagiannaki
        else:
260 5fdaf331 Sofia Papagiannaki
            ftable = Table('fileheader_permission', self.metadata, autoload=True)
261 5fdaf331 Sofia Papagiannaki
        permission = Table('permission', self.metadata, autoload=True)
262 5fdaf331 Sofia Papagiannaki
        group = Table('gss_group', self.metadata, autoload=True)
263 5fdaf331 Sofia Papagiannaki
        user = Table('gss_user', self.metadata, autoload=True)
264 5fdaf331 Sofia Papagiannaki
        j = ftable.join(permission, ftable.c.permissions_id == permission.c.id)
265 5fdaf331 Sofia Papagiannaki
        j1 = j.join(group, group.c.id == permission.c.group_id)
266 5fdaf331 Sofia Papagiannaki
        j2 = j.join(user, user.c.id == permission.c.user_id)
267 5fdaf331 Sofia Papagiannaki
        
268 5fdaf331 Sofia Papagiannaki
        permissions = defaultdict(list)
269 5fdaf331 Sofia Papagiannaki
        
270 93297831 Sofia Papagiannaki
        def _get_permissions(self, action='read', get_groups=True):
271 93297831 Sofia Papagiannaki
            if get_groups:
272 93297831 Sofia Papagiannaki
                col, j = group.c.name, j1
273 93297831 Sofia Papagiannaki
                cond2 = permission.c.group_id != None
274 93297831 Sofia Papagiannaki
            else:
275 93297831 Sofia Papagiannaki
                col, j = user.c.username, j2
276 93297831 Sofia Papagiannaki
                cond2 = permission.c.user_id != None
277 93297831 Sofia Papagiannaki
            s = select([col], from_obj=j)
278 93297831 Sofia Papagiannaki
            if is_folder:
279 93297831 Sofia Papagiannaki
                s = s.where(ftable.c.folder_id == id)
280 93297831 Sofia Papagiannaki
            else:
281 93297831 Sofia Papagiannaki
                s = s.where(ftable.c.fileheader_id == id)
282 93297831 Sofia Papagiannaki
            if action == 'read':
283 93297831 Sofia Papagiannaki
                cond1 = permission.c.read == True
284 93297831 Sofia Papagiannaki
            else:
285 93297831 Sofia Papagiannaki
                cond1 = permission.c.write == True
286 93297831 Sofia Papagiannaki
            s = s.where(and_(cond1, cond2))
287 93297831 Sofia Papagiannaki
            print '>', s, s.compile().params
288 93297831 Sofia Papagiannaki
            rp = self.conn.execute(s)
289 93297831 Sofia Papagiannaki
            p = permissions[action].extend([e[0] for e in rp.fetchall()])
290 93297831 Sofia Papagiannaki
            rp.close()
291 93297831 Sofia Papagiannaki
            return p
292 5fdaf331 Sofia Papagiannaki
        
293 93297831 Sofia Papagiannaki
        #get object read groups
294 93297831 Sofia Papagiannaki
        _get_permissions(self, action='read', get_groups=True)
295 5fdaf331 Sofia Papagiannaki
        
296 93297831 Sofia Papagiannaki
        #get object read users
297 93297831 Sofia Papagiannaki
        _get_permissions(self, action='read', get_groups=False)
298 93297831 Sofia Papagiannaki
        
299 93297831 Sofia Papagiannaki
        #get object write groups
300 93297831 Sofia Papagiannaki
        _get_permissions(self, action='write', get_groups=True)
301 93297831 Sofia Papagiannaki
        
302 93297831 Sofia Papagiannaki
        #get object write groups
303 93297831 Sofia Papagiannaki
        _get_permissions(self, action='write', get_groups=False)
304 5fdaf331 Sofia Papagiannaki
        
305 5fdaf331 Sofia Papagiannaki
        return permissions
306 5fdaf331 Sofia Papagiannaki
    
307 98137a34 Sofia Papagiannaki
if __name__ == "__main__":
308 2db16f05 Sofia Papagiannaki
    old_db = ''
309 f6c0005f Sofia Papagiannaki
    db = ''
310 98137a34 Sofia Papagiannaki
    
311 f6c0005f Sofia Papagiannaki
    f = open('fixdates.sql', 'w')
312 f6c0005f Sofia Papagiannaki
    ot = ObjectMigration(old_db, db, f)
313 f6c0005f Sofia Papagiannaki
    ot.create_objects()
314 f6c0005f Sofia Papagiannaki
    f.close()
315 98137a34 Sofia Papagiannaki
    
316 5a96180b Antony Chazapis