Statistics
| Branch: | Tag: | Revision:

root / contrib / migrate-db @ faad3c72

History | View | Annotate | Download (13.4 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 56f3c759 Sofia Papagiannaki
                self.backend.permissions.public_set(
170 56f3c759 Sofia Papagiannaki
                    object,
171 4a105ce2 Sofia Papagiannaki
                    self.backend.public_url_security,
172 56f3c759 Sofia Papagiannaki
                    self.backend.public_url_alphabet
173 56f3c759 Sofia Papagiannaki
                )
174 5fdaf331 Sofia Papagiannaki
            #set object's permissions
175 93297831 Sofia Papagiannaki
            self.create_permissions(headerid, object, username, is_folder=False)
176 f6c0005f Sofia Papagiannaki
    
177 5fdaf331 Sofia Papagiannaki
    def build_path(self, child_id):
178 5fdaf331 Sofia Papagiannaki
        folder = Table('folder', self.metadata, autoload=True)
179 5fdaf331 Sofia Papagiannaki
        user = Table('gss_user', self.metadata, autoload=True)
180 5fdaf331 Sofia Papagiannaki
        j = folder.join(user, folder.c.owner_id == user.c.id)
181 5fdaf331 Sofia Papagiannaki
        s = select([folder, user.c.username], from_obj=j)
182 5fdaf331 Sofia Papagiannaki
        s = s.where(folder.c.id == child_id)
183 5fdaf331 Sofia Papagiannaki
        s.order_by(folder.c.modificationdate)
184 f6c0005f Sofia Papagiannaki
        rp = self.conn.execute(s)
185 5fdaf331 Sofia Papagiannaki
        t = rp.fetchone()
186 5fdaf331 Sofia Papagiannaki
        md5 = hashlib.md5()
187 5fdaf331 Sofia Papagiannaki
        hash = md5.hexdigest().lower()
188 5fdaf331 Sofia Papagiannaki
        size = 0
189 5fdaf331 Sofia Papagiannaki
        if not t[PARENT_ID]:
190 f6c0005f Sofia Papagiannaki
            return ''
191 f6c0005f Sofia Papagiannaki
        else:
192 5fdaf331 Sofia Papagiannaki
            container_path = t[USER]
193 5fdaf331 Sofia Papagiannaki
            container_path += '/trash' if t[DELETED] else '/pithos'
194 5fdaf331 Sofia Papagiannaki
            parent_node = self.backend.node.node_lookup(container_path)
195 5fdaf331 Sofia Papagiannaki
            if not parent_node:
196 5fdaf331 Sofia Papagiannaki
                raise Exception('Missing node:', container_path)
197 5fdaf331 Sofia Papagiannaki
            parent_path = self.build_path(t[PARENT_ID])
198 5fdaf331 Sofia Papagiannaki
            path = '%s/%s/%s' %(container_path, parent_path, t[NAME]) if parent_path else '%s/%s' %(container_path, t[NAME])
199 5fdaf331 Sofia Papagiannaki
            node = self.backend.node.node_lookup(path)
200 5fdaf331 Sofia Papagiannaki
            if not node:
201 5fdaf331 Sofia Papagiannaki
                node = self.backend.node.node_create(parent_node, path)
202 5fdaf331 Sofia Papagiannaki
                if not node:
203 5fdaf331 Sofia Papagiannaki
                    raise Exception('Unable to create node:', path)
204 5fdaf331 Sofia Papagiannaki
                
205 5fdaf331 Sofia Papagiannaki
                #create versions
206 5fdaf331 Sofia Papagiannaki
                v = self.create_version(node, hash, size, t[USER], CLUSTER_NORMAL, 'application/directory', t[CREATIONDATE])
207 5fdaf331 Sofia Papagiannaki
                if t[CREATIONDATE] != t[MODIFICATIONDATE]:
208 5fdaf331 Sofia Papagiannaki
                    self.backend.node.version_recluster(v, CLUSTER_HISTORY)
209 5fdaf331 Sofia Papagiannaki
                    self.create_version(node, hash, size, t[USER], CLUSTER_NORMAL, 'application/directory', t[MODIFICATIONDATE])
210 5fdaf331 Sofia Papagiannaki
                
211 5fdaf331 Sofia Papagiannaki
                #set permissions
212 93297831 Sofia Papagiannaki
                self.create_permissions(t[ID], path, t[USER], is_folder=True)
213 5fdaf331 Sofia Papagiannaki
            return '%s/%s' %(parent_path, t[NAME]) if parent_path else t[NAME]
214 f6c0005f Sofia Papagiannaki
    
215 f6c0005f Sofia Papagiannaki
    def retrieve_current_nodes(self):
216 98137a34 Sofia Papagiannaki
        fileheader = Table('fileheader', self.metadata, autoload=True)
217 98137a34 Sofia Papagiannaki
        filebody = Table('filebody', self.metadata, autoload=True)
218 98137a34 Sofia Papagiannaki
        folder = Table('folder', self.metadata, autoload=True)
219 98137a34 Sofia Papagiannaki
        gss_user = Table('gss_user', self.metadata, autoload=True)
220 2db16f05 Sofia Papagiannaki
        j = filebody.join(fileheader, filebody.c.id == fileheader.c.currentbody_id)
221 98137a34 Sofia Papagiannaki
        j = j.join(folder, fileheader.c.folder_id == folder.c.id)
222 98137a34 Sofia Papagiannaki
        j = j.join(gss_user, fileheader.c.owner_id == gss_user.c.id)
223 2db16f05 Sofia Papagiannaki
        s = select([gss_user.c.username,  fileheader.c.id, fileheader.c.folder_id,
224 5fdaf331 Sofia Papagiannaki
                    fileheader.c.name,  fileheader.c.deleted,
225 5fdaf331 Sofia Papagiannaki
                    filebody.c.storedfilepath, filebody.c.mimetype,
226 5fdaf331 Sofia Papagiannaki
                    fileheader.c.readforall, fileheader.c.owner_id], from_obj=j)
227 98137a34 Sofia Papagiannaki
        rp = self.conn.execute(s)
228 f6c0005f Sofia Papagiannaki
        object = rp.fetchone()
229 f6c0005f Sofia Papagiannaki
        while object:
230 f6c0005f Sofia Papagiannaki
            yield object
231 f6c0005f Sofia Papagiannaki
            object = rp.fetchone()
232 f6c0005f Sofia Papagiannaki
        rp.close()
233 98137a34 Sofia Papagiannaki
    
234 f6c0005f Sofia Papagiannaki
    def retrieve_node_versions(self, header_id):
235 f6c0005f Sofia Papagiannaki
        filebody = Table('filebody', self.metadata, autoload=True)
236 f6c0005f Sofia Papagiannaki
        gss_user = Table('gss_user', self.metadata, autoload=True)
237 f6c0005f Sofia Papagiannaki
        j = filebody.join(gss_user, filebody.c.modifiedby_id == gss_user.c.id)
238 f6c0005f Sofia Papagiannaki
        s = select([filebody.c.filesize, gss_user.c.username,
239 f6c0005f Sofia Papagiannaki
                    filebody.c.storedfilepath, filebody.c.mimetype,
240 f6c0005f Sofia Papagiannaki
                    filebody.c.modificationdate], from_obj=j)
241 f6c0005f Sofia Papagiannaki
        s = s.where(filebody.c.header_id == header_id)
242 f6c0005f Sofia Papagiannaki
        s = s.order_by(filebody.c.version)
243 f6c0005f Sofia Papagiannaki
        rp = self.conn.execute(s)
244 f6c0005f Sofia Papagiannaki
        version = rp.fetchone()
245 f6c0005f Sofia Papagiannaki
        while version:
246 f6c0005f Sofia Papagiannaki
            yield version, rp.rowcount
247 f6c0005f Sofia Papagiannaki
            version = rp.fetchone()
248 f6c0005f Sofia Papagiannaki
        rp.close()
249 98137a34 Sofia Papagiannaki
    
250 5fdaf331 Sofia Papagiannaki
    def retrieve_tags(self, header_id):
251 f6c0005f Sofia Papagiannaki
        filetag = Table('filetag', self.metadata, autoload=True)
252 5fdaf331 Sofia Papagiannaki
        s = select([filetag.c.tag], filetag.c.fileid == header_id)
253 f6c0005f Sofia Papagiannaki
        rp = self.conn.execute(s)
254 5fdaf331 Sofia Papagiannaki
        tags = rp.fetchall() if rp.returns_rows else []
255 5fdaf331 Sofia Papagiannaki
        tags = [elem[0] for elem in tags]
256 f6c0005f Sofia Papagiannaki
        rp.close()
257 5fdaf331 Sofia Papagiannaki
        return ','.join(tags) if tags else ''
258 98137a34 Sofia Papagiannaki
    
259 5fdaf331 Sofia Papagiannaki
    def retrieve_permissions(self, id, is_folder=True):
260 5fdaf331 Sofia Papagiannaki
        permissions = {}
261 5fdaf331 Sofia Papagiannaki
        if is_folder:
262 5fdaf331 Sofia Papagiannaki
            ftable = Table('folder_permission', self.metadata, autoload=True)
263 5fdaf331 Sofia Papagiannaki
        else:
264 5fdaf331 Sofia Papagiannaki
            ftable = Table('fileheader_permission', self.metadata, autoload=True)
265 5fdaf331 Sofia Papagiannaki
        permission = Table('permission', self.metadata, autoload=True)
266 5fdaf331 Sofia Papagiannaki
        group = Table('gss_group', self.metadata, autoload=True)
267 5fdaf331 Sofia Papagiannaki
        user = Table('gss_user', self.metadata, autoload=True)
268 5fdaf331 Sofia Papagiannaki
        j = ftable.join(permission, ftable.c.permissions_id == permission.c.id)
269 5fdaf331 Sofia Papagiannaki
        j1 = j.join(group, group.c.id == permission.c.group_id)
270 5fdaf331 Sofia Papagiannaki
        j2 = j.join(user, user.c.id == permission.c.user_id)
271 5fdaf331 Sofia Papagiannaki
        
272 5fdaf331 Sofia Papagiannaki
        permissions = defaultdict(list)
273 5fdaf331 Sofia Papagiannaki
        
274 93297831 Sofia Papagiannaki
        def _get_permissions(self, action='read', get_groups=True):
275 93297831 Sofia Papagiannaki
            if get_groups:
276 93297831 Sofia Papagiannaki
                col, j = group.c.name, j1
277 93297831 Sofia Papagiannaki
                cond2 = permission.c.group_id != None
278 93297831 Sofia Papagiannaki
            else:
279 93297831 Sofia Papagiannaki
                col, j = user.c.username, j2
280 93297831 Sofia Papagiannaki
                cond2 = permission.c.user_id != None
281 93297831 Sofia Papagiannaki
            s = select([col], from_obj=j)
282 93297831 Sofia Papagiannaki
            if is_folder:
283 93297831 Sofia Papagiannaki
                s = s.where(ftable.c.folder_id == id)
284 93297831 Sofia Papagiannaki
            else:
285 93297831 Sofia Papagiannaki
                s = s.where(ftable.c.fileheader_id == id)
286 93297831 Sofia Papagiannaki
            if action == 'read':
287 93297831 Sofia Papagiannaki
                cond1 = permission.c.read == True
288 93297831 Sofia Papagiannaki
            else:
289 93297831 Sofia Papagiannaki
                cond1 = permission.c.write == True
290 93297831 Sofia Papagiannaki
            s = s.where(and_(cond1, cond2))
291 93297831 Sofia Papagiannaki
            print '>', s, s.compile().params
292 93297831 Sofia Papagiannaki
            rp = self.conn.execute(s)
293 93297831 Sofia Papagiannaki
            p = permissions[action].extend([e[0] for e in rp.fetchall()])
294 93297831 Sofia Papagiannaki
            rp.close()
295 93297831 Sofia Papagiannaki
            return p
296 5fdaf331 Sofia Papagiannaki
        
297 93297831 Sofia Papagiannaki
        #get object read groups
298 93297831 Sofia Papagiannaki
        _get_permissions(self, action='read', get_groups=True)
299 5fdaf331 Sofia Papagiannaki
        
300 93297831 Sofia Papagiannaki
        #get object read users
301 93297831 Sofia Papagiannaki
        _get_permissions(self, action='read', get_groups=False)
302 93297831 Sofia Papagiannaki
        
303 93297831 Sofia Papagiannaki
        #get object write groups
304 93297831 Sofia Papagiannaki
        _get_permissions(self, action='write', get_groups=True)
305 93297831 Sofia Papagiannaki
        
306 93297831 Sofia Papagiannaki
        #get object write groups
307 93297831 Sofia Papagiannaki
        _get_permissions(self, action='write', get_groups=False)
308 5fdaf331 Sofia Papagiannaki
        
309 5fdaf331 Sofia Papagiannaki
        return permissions
310 5fdaf331 Sofia Papagiannaki
    
311 98137a34 Sofia Papagiannaki
if __name__ == "__main__":
312 2db16f05 Sofia Papagiannaki
    old_db = ''
313 f6c0005f Sofia Papagiannaki
    db = ''
314 98137a34 Sofia Papagiannaki
    
315 f6c0005f Sofia Papagiannaki
    f = open('fixdates.sql', 'w')
316 f6c0005f Sofia Papagiannaki
    ot = ObjectMigration(old_db, db, f)
317 f6c0005f Sofia Papagiannaki
    ot.create_objects()
318 f6c0005f Sofia Papagiannaki
    f.close()
319 98137a34 Sofia Papagiannaki
    
320 5a96180b Antony Chazapis