dummy.py 10.5 KB
Newer Older
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
1
2
3
import os
import sqlite3
import json
4
import logging
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
5

6
7
8
9
10
11
12
13
14
15
16
17
class BackEnd:
    def __init__(self, basepath, log_file='backend.out', log_level=logging.DEBUG):
        self.basepath = basepath
        logging.basicConfig(filename=log_file,level=log_level,)
        if not os.path.exists(basepath):
            os.makedirs(basepath)
        db = '/'.join([basepath, 'db'])
        con = sqlite3.connect(db)
        # Create tables
        sql = '''create table if not exists objects(name text)'''
        con.execute(sql)
        con.commit()
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    def get_account_meta(self, account):
        """ returns a dictionary with the container metadata """
        logging.info("get_account_meta: %s %s", account)
        fullname = '/'.join([self.basepath, account])
        if not os.path.exists(fullname):
            raise NameError('Account does not exist')
        contents = os.listdir(fullname) 
        count = len(contents)
        size = sum(os.path.getsize('/'.join([self.basepath, account, objectname])) for objectname in contents)
        return {'name': name, 'count': count, 'bytes': size}
        
    def create_container(self, account, name):
        """ creates a new container with the given name
        if it doesn't exists under the basepath """
        logging.debug("create_container: %s %s", account, name)
        fullname = '/'.join([self.basepath, account, name])    
        if not os.path.exists(fullname):
            os.chdir(self.basepath)
            os.mkdir(name)
        else:
            raise NameError('Container already exists')
        return
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
    def delete_container(self, account, name):
        """ deletes the container with the given name
        if it exists under the basepath
        and it's empty"""
        logging.debug("delete_container: %s %s", params.items()[1:])
        fullname = '/'.join([self.basepath, account, name])    
        if not os.path.exists(fullname):
            raise NameError('Container does not exist')
        if not list_objects(name):
            raise Error('Container is not empty')
        else:
            os.chdir(self.basepath)
            os.rmdir(name)
        return
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
56
    
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    def get_container_meta(self, account, name):
        """ returns a dictionary with the container metadata """
        fullname = '/'.join([self.basepath, account, name])
        if not os.path.exists(fullname):
            raise NameError('Container does not exist')
        contents = os.listdir(fullname) 
        count = len(contents)
        size = sum(os.path.getsize('/'.join([self.basepath, account, name, objectname])) for objectname in contents)
        return {'name': name, 'count': count, 'bytes': size}
    
    def list_containers(self, marker = None, limit = 10000):
        return os.listdir(self.basepath)[:limit]
    
    def list_objects(self, account, container, prefix='', delimiter=None, marker = None, limit = 10000):
        #dir = '/'.join([self.basepath, account, container])
        #if not os.path.exists(dir):
        #    raise NameError('Container does not exist')
        #search_str = ''
        #if prefix:
        #    search_str = '/'.join([search_str, prefix])
        ##if delimiter:
        #if None:
        #    search_str = ''.join(['%', search_str, '%', delimiter])
        #    print search_str
        #    c = con.execute('select * from objects where name like ''?'' order by name', (search_str,))
        #else:
        #    search_str = ''.join(['%', search_str, '%'])
        #    print search_str
        #    c = con.execute('select * from objects where name like ''?'' order by name', (search_str,))
        #l = []
        #for row in c.fetchall():
        #    s = ''
        #    print row[0]
        #    rest = str(row[0]).split(prefix)[1]
        #    print rest
        #    #if delimiter:
        #    #    rest = rest.partition(delimiter)[0]
        #    #print rest
        #    folders = rest.split('/')[:-1]
        #    for folder in folders:
        #        path = ''.join([s, folder, '/'])
        #        if path not in l:
        #            l.append(path)
        #        s = ''.join([s, folder, '/'])
        #    l.append(rest)
        logging.info("list_objects: %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit)
        if prefix or delimiter:
            if prefix:
                objects = [x for x in objects if x['name'].startswith(prefix)]
            if delimiter:
                pseudo_objects = {}
                for x in objects:
                    pseudo_name = x['name'][len(prefix):]
                    i = pseudo_name.find(delimiter)
                    if i != -1:
                        pseudo_name = pseudo_name[:i]
                    # TODO: Virtual directories.
                    if pseudo_name not in pseudo_objects:
                        pseudo_objects[pseudo_name] = x
                objects = sorted(pseudo_objects.values(), key=lambda o: o['name'])
            
        start = 0
        if marker:
            try:
                start = binary_search_name(objects, marker) + 1
            except ValueError:
                pass
        if not limit or limit > 10000:
            limit = 10000
        return objects[start:start + limit]
    
    def get_object_meta(self, account, container, name, keys):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        else:
            os.chdir(dir)
        location = self.__get_object_linkinfo('/'.join([account, container, name]))
        location = '.'.join([location, 'meta'])
        f = open(location, 'r')
        data = json.load(f)
        f.close()
        return data
    
    def get_object_data(self, account, container, name, offset=0, length=-1):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        else:
            os.chdir(dir)
        location = self.__get_object_linkinfo('/'.join([account, container, name]))
        f = open(location, 'r')
        if offset:
            f.seek(offset)
        data = f.read(length)
        f.close()
        return data
    
    def update_object(self, account, container, name, data):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        try:
            location = self.__get_object_linkinfo('/'.join([account, container, name]))
        except NameError:
            # new object
            location = str(self.__save_linkinfo('/'.join([account, container, name])))
            print ':'.join(['Creating new location', location])
        self.__store_data(location, container, data)
        return
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
167
    
168
169
170
171
172
173
174
175
176
177
178
179
    def update_object_meta(self, account, container, name, meta):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        try:
            location = self.__get_object_linkinfo('/'.join([account, container, name]))
        except NameError:
            # new object
            location = str(self.__save_linkinfo('/'.join([account, container, name])))
            print ':'.join(['Creating new location', location])
        self.__store_metadata(location, container, meta)
        return
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
180
    
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    def copy_object(self, account, src_container, src_name, dest_container, dest_name, meta):
        fullname = '/'.join([self.basepath, account, dest_container])    
        if not os.path.exists(fullname):
            raise NameError('Destination container does not exist')
        self.update_object(account, dest_container, dest_name, get_object_data(account, src_container, src_name))
        src_object_meta = self.get_object_meta(account, src_container, src_name)
        if (type(src_object_meta) == types.DictType):
            distinct_keys = [k for k in src_object_meta.keys() if k not in meta.keys()]
            for k in distinct_keys:
                meta[k] = src_object_meta[k]
                self.update_object_meta(account, dest_container, dest_name, meta)
        else:
            self.update_object_meta(account, dest_container, dest_name, meta)
        return
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
195
    
196
197
198
199
200
201
202
203
204
205
206
207
208
    def delete_object(self, account, container, name):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        else:
            os.chdir(dir)
        location = self.__get_object_linkinfo('/'.join([account, container, name]))
        # delete object data
        self.__delete_data(location, account, container)
        # delete object metadata
        location = '.'.join([location, 'meta'])
        self.__delete_data(location, account, container)
        return
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
209
    
210
211
212
213
214
215
216
217
218
219
220
    def __store_metadata(self, location, account, container, meta):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        else:
            os.chdir(dir)
        location = '.'.join([location, 'meta'])
        f = open(location, 'w')
        data = json.dumps(meta)
        f.write(data)
        f.close()
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
221
    
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    def __store_data(self, location, account, container, data):
        dir = '/'.join([self.basepath, account, container])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        else:
            os.chdir(dir)
        f = open(location, 'w')
        f.write(data)
        f.close()
        
    def __delete_data(self, location, account, container):
        file = '/'.join([self.basepath, account, container, location])
        if not os.path.exists(dir):
            raise NameError('Container does not exist')
        else:
            os.remove(file)
        
    def __get_object_linkinfo(self, name):
        c = con.execute('select rowid from objects where name=''?''', (name,))
        row = c.fetchone()
        if row:
            return str(row[0])
        else:
            raise NameError('Object not found')
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
246
    
247
248
249
250
    def __save_linkinfo(self, name):
        id = con.execute('insert into objects(name) values(?)', (name,)).lastrowid
        con.commit()
        return id
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
251
    
252
253
254
255
    def __delete_linkinfo(self, name):
        con.execute('delete from objects where name = ?', (name,))
        cont.commit()
        return