filesystem.py 12.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# @package FileSystem

# Imports
import os
from datetime import datetime
from distutils import dir_util
import locale
import shutil
import random
import cStringIO
from mimetypes import guess_type
import shutil
from zipfile import ZipFile
import xmlrpclib

class FileSystem(object):

    FILE_EXTENSIONS = ["png", "jpg", "jpeg", "bmp", "ps", "eps", "pdf", "txt", "xml", "py", "c", "cpp", "root", "pxlio"]
    BROWSER_EXTENSIONS = ["png", "jpg", "jpeg", "bmp", "pdf"]
    ADDITIONAL_MIMES = {"pxlio": "text/plain",
                        "root" : "text/plain"}

    def __init__(self):
        # allowed extensions
        self.allowed_extensions = FileSystem.FILE_EXTENSIONS

    def setup(self, basedir, username):
        # the data path
        self.data_path = os.path.join(basedir, "data")
        # the main paths
        self.user_path = os.path.join(self.data_path, "user")
        self.public_path = os.path.join(self.data_path, "public")
        self.shared_path = os.path.join(self.data_path, "shared")
        self.examples_path = os.path.join(self.data_path, "examples")
        self.exercises_path = os.path.join(self.data_path, "exercises")

        paths = [self.data_path, self.user_path, self.public_path, self.shared_path, self.examples_path, self.exercises_path]
        for path in paths:
            if not os.path.isdir(path):
                os.makedirs(path, 0700)

        if username is None or username == "":
            return
        upath = os.path.join(self.user_path, username)
        if not os.path.isdir(upath):
            os.makedirs(upath, 0700)
        upath = os.path.join(self.public_path, username)
        if not os.path.isdir(upath):
            os.makedirs(upath, 0700)

    def getMimeType(self, filepath):
        mime, encoding = guess_type(filepath)
        if mime is not None:
            return mime
        ext = filepath.split(".")[-1]
        if ext is not None and ext != "" and ext.lower() in FileSystem.ADDITIONAL_MIMES.keys():
            return FileSystem.ADDITIONAL_MIMES[ext]
        return None

    def checkFileExtension(self, path, extensions=[]):
        if (len(extensions) == 0):
            return True
        for elem in extensions:
            elem = elem if elem.startswith(".") else "." + elem
            if path.lower().endswith(elem.lower()):
                return True
        return False

    def exists(self, username, path, pathtype=None, fileextension=None, permissionmode="r"):
        if isinstance(permissionmode, str) and permissionmode.lower() not in ["r", "w"]:
            return False

        # path exists physically?
        if not os.path.exists(path):
            return False

        # pathtype correct? (file, folder, or None)
        if pathtype is not None:
            pathtype = pathtype.lower()
            if not pathtype in ["file", "folder"]:
                return False
            if os.path.isdir(path) and pathtype == "file":
                return False
            elif not os.path.isdir(path) and pathtype == "folder":
                return False

        # file extension correct?
        if fileextension is not None:
            fileextension = fileextension if isinstance(fileextension, list) else [fileextension]
            hit = False
            for fileext in fileextension:
                fileext = fileext if fileext.startswith(".") else "." + fileext
                if path.endswith(fileext):
                    hit = True
            if not hit:
                return False

        # permission?
        if permissionmode is not None:
            return False

        return True


    def getFileList(self, username, path, deep, ext_filter=[], reverse=False):
        dirlist = os.listdir(path)
        returnlist = []
        for elem in dirlist:
            fullPath = os.path.join(path, elem)
            locale.setlocale(locale.LC_ALL, '')
            stats = os.stat(path + ('' if path.endswith('/') else '/') + elem)
            size = stats.st_size
            size = locale.format("%d", size, grouping=True)
            mtime = stats.st_mtime
            mtime = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S")
            if os.path.isdir(fullPath):
                if elem.startswith("."):
                    continue
                returnlist.append({'name': elem, 'type': 'folder', 'parent': path + '/', 'extension': '', 'mtime': mtime, 'size': size, "path": os.path.join(path, elem)})
                if deep:
                    returnlist.extend(self.getFileList(username, fullPath, deep, ext_filter, reverse))
            else:
                if elem.startswith("."):
                    continue
                extension = elem.split(".")[-1]
                returnlist.append({'name': elem, 'type': 'file', 'parent': path + '/', 'extension': extension, 'mtime': mtime, 'size': size, "path": os.path.join(path, elem)})
        # apply filters
        filelist = []
        # delete "." in file extensions
        ext_filter = map(lambda ext: ext if not ext.startswith(".") else ext[1:], ext_filter)
        for elem in returnlist:
            if (elem["type"] == "folder"):
                filelist.append(elem)
                continue
            if not reverse:
                if str(elem["extension"]) not in ext_filter:
                    filelist.append(elem)
            else:
                if str(elem["extension"]) in ext_filter:
                    filelist.append(elem)
        return filelist


    def cutSlashs(self, path):
        path = path[1:] if path.startswith("/") else path
        if path == "":
            return path
        path = path[:-1] if path.endswith("/") else path
        return path

    def createFolder(self, path, name):
        # folder with the same name existent?
        fullpath = os.path.join(path, name)
        if os.path.isdir(fullpath):
            raise Exception("Name already in use!")
        try:
            os.mkdir(fullpath)
        except Exception as e:
            #raise Exception("You don't have the permission to create this folder!")
            raise Exception(str(e))

    def createFile(self, path, name):
        # file with the same name existent?
        fullpath = os.path.join(path, name)
        if os.path.exists(fullpath):
            raise Exception("Name already in use!")
        try:
            f = file(fullpath, "w")
            f.close()
        except Exception as e:
            raise Exception(str(e))
        

    def renameFolder(self, path, name):
        # file or folder
        if not os.path.isdir(path):
            raise Exception("Renaming file with folder function!")

        # folder with the same name existent?
        path = path if not path.endswith("/") else path[:-1]
        fullpath = os.path.join("/".join(path.split("/")[:-1]), name)
        if os.path.exists(fullpath):
            raise Exception("Name already in use!")

        os.renames(path, fullpath)

    def renameFile(self, path, name):
        # file or folder
        if os.path.isdir(path):
            raise Exception("Renaming folder with file function!")

        # file with the same name existent?
        fullpath = os.path.join("/".join(path.split("/")[:-1]), name)
        if os.path.exists(fullpath):
            raise Exception("Name already in use!")

        os.renames(path, fullpath)

    def remove(self, vpath):
        if isinstance(vpath, list):
            for p in path:
                self.remove(p)
            return True

        if os.path.isdir(path):
            shutil.rmtree(path)
        else:
            os.remove(path)

    def compress(self, path, paths, name):
        # paths has to be a list of strings
        paths = paths if isinstance(paths, list) else [paths]

        path = path if not path.endswith("/") else path[:-1]

        fullpath = os.path.join(path, "%s.zip" % name)

        if os.path.exists(fullpath):
            raise Exception("Name already in use!")

        archive = ZipFile(fullpath, "w")

        vpath = vpath[1:] if vpath.startswith("/") else vpath
        for p in paths:
            if p is None or p == "":
                continue
            p = p[1:] if vp.startswith("/") else p
            if os.path.isdir(p):
                for elem in os.listdir(p):
                    fullp = os.path.join(p, elem)
                    if os.path.isdir(fullp):
                        paths.append(fullp)
                    else:
                        ap = fullp[len(path):] if fullp.startswith(path) else fullp
                        archive.write(fullpp, ap)

            ap = p[len(path):] if p.startswith(path) else p
            archive.write(p, ap)

        archive.close()

    def paste(self, vpath, vtarget, cut):
        if isinstance(vtarget, list):
            for vp in vtarget:
                self.paste(username, vpath, vp, cut)
            return True

        # permission?
        if not self.checkPermission(username, vpath, 'w'):
            raise Exception("You don't have the permission in this folder!")

        ppath = self.translateVirtualPath(username, vpath)
        ptarget = self.translateVirtualPath(username, vtarget)
        fulltarget = os.path.join(ppath, ptarget.split("/")[-1])

        if os.path.exists(fulltarget):
            raise Exception("Name already in use!")

        if os.path.isdir(ptarget):
            shutil.copytree(ptarget, fulltarget)
            if cut:
                shutil.rmtree(ptarget)
        else:
            shutil.copy2(ptarget, ppath)
            if cut:
                os.remove(ptarget)


    def saveFileContent(self, username, vPath, content, force=True):
        #check write permissions    
        if not self.checkPermission(username, vPath, 'w'):
            return False, "Permission denied!"

        pPath = self.translateVirtualPath(username, vPath)

        #check if file already exists
        if os.path.exists(pPath) and not force:
            return False, "The file '%s' already exists!" % vPath

        out = open(pPath, "w")
        for line in content.data:
            out.write(line)
        out.close()

        return True, "File saved!"

    def getFileContent(self, username, vPath):
        # permissions?
        if not self.checkPermission(username, vPath, 'r'):
            return False, "Permission denied!"

        pPath = self.translateVirtualPath(username, vPath)

        f = open(pPath, "r")
        content = f.read()
        f.close()
        content = " " if content == "" else content
        return xmlrpclib.Binary(content), "File opened!"

    def isbrowserfile(self, path):
        extension = path.split(".")[-1]
        return extension in VirtualFileSystem.BROWSER_EXTENSIONS

    def handleFileNameCollision(self, name, pPath):
        # collision?
        files = os.listdir(pPath)
        if name not in files:
            return name

        # when this line is reached, there is a collision!

        # cut the file extension
        extension = name.split(".")[-1]
        prename = None
        if name == extension:
            extension = ""
            prename = name
        else:
            prename = name.split("." + extension)[0]

        # has the name already a counter at its end?
        hasCounter = False
        preprename = None
        counter = prename.split("_")[-1]

        if counter != prename:
            try:
                counter = int(counter)
                hasCounter = True
                preprename = "_".join(prename.split("_")[:-1])
            except:
                pass

        if hasCounter:
            # increment and try again
            counter += 1
            newname = "%s_%d%s" % (preprename, counter, "" if extension == "" else "." + extension)
        else:
            newname = "%s_1%s" % (prename, "" if extension == "" else "." + extension)

        # return
        return self.handleFileNameCollision(newname, pPath)

    """
    def handleDownload(self, username, vPath):
        if not self.exists(username, vPath, pathtype="file"):
            raise Exception("The file '%s' does not exist or<br />you don't have the permission to read this file."%vPath)
            return None
        
        pPath = self.translateVirtualPath(username, vPath)
        payload = open(pPath, "r")
        output = cStringIO.StringIO()
        for line in payload:
            output.write(line)
        
        output.seek(0)
        data = output.read()
        output.close()
        
        # get the content type depending on the file extension
        ext = vPath.split(".")[-1]
        mimetype = self.getMimeType(vPath)
        if mimetype is None:
            raise Exception("The file extension '%s' is not supported by this server"%ext)
            return None
        
        return data, mimetype, self.isbrowserfile(vPath)
    """

##
# Creates a unique ID
# p = 1e-18
def uniqueId(l=10):
    chars = "abcdefghijklmnopqrstuvwxyz"
    chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    chars += "0123456789"
    code = ""
    for i in range(l):
        rnd = int(round(random.uniform(0, len(chars) - 1)))
        code += chars[rnd]
    return code