filesystem.py 14 KB
Newer Older
Marcel's avatar
Marcel committed
1
# -*- coding: utf-8 -*-
2

Marcel's avatar
Marcel committed
3
# imports
4
5
from StringIO import StringIO
from distutils.spawn import find_executable
6
7
from mimetypes import guess_type
from zipfile import ZipFile
8
import json
9
10
11
12
import logging
import os
import re
import shutil
Gero Müller's avatar
Gero Müller committed
13
import stat
14
15
16
17
import subprocess

try:
    import Image
Gero Müller's avatar
Gero Müller committed
18
    import ImageFilter
19
20
21
22
23
24
25
26
27
28
    HAVE_PIL = True
except:
    HAVE_PIL = False

if not HAVE_PIL:
    convert_executable = find_executable('convert')
    if convert_executable and os.path.isfile(convert_executable):
        HAVE_CONVERT = True
    else:
        HAVE_CONVERT = False
29

30
31
logger = logging.getLogger(__name__)

32

33
34
class FileSystem(object):

35
36
    FILE_EXTENSIONS = ["png", "jpg", "jpeg", "bmp", "ps", "eps", "pdf",
                       "txt", "xml", "py", "c", "cpp", "root", "pxlio"]
Marcel's avatar
Marcel committed
37
38
39
    BROWSER_EXTENSIONS = ["png", "jpg", "jpeg", "bmp"]
    ADDITIONAL_MIMES = {
        "pxlio": "text/plain",
40
        "root": "text/plain"
Marcel's avatar
Marcel committed
41
    }
42
43
44
45
46

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

47
    def setup(self, basedir=None):
Marcel's avatar
Marcel committed
48
49
        if basedir is None:
            basedir = self.expand('~/')
murban's avatar
murban committed
50
        if not os.path.isdir(basedir):
Marcel's avatar
Marcel committed
51
            raise Exception("Basedir '%s' does not exist!" % basedir)
murban's avatar
murban committed
52
53
        # the basedir
        self.basedir = os.path.join(basedir, ".vispa")
54
55
        if os.path.isdir(self.basedir):
            return "Basedir already exists"
56
        else:
57
58
            os.makedirs(self.basedir, 0700)
            return "Basedir now exists"
59

murban's avatar
murban committed
60
    def get_mime_type(self, filepath):
Marcel's avatar
Marcel committed
61
        filepath = self.expand(filepath)
62
63
64
65
66
67
68
69
        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

murban's avatar
murban committed
70
    def check_file_extension(self, path, extensions=[]):
Marcel's avatar
Marcel committed
71
        path = self.expand(path)
72
73
74
75
76
77
78
79
        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

Marcel's avatar
Marcel committed
80
81
    def exists(self, path, type=None):
        # type may be 'f' or 'd'
Marcel's avatar
Marcel committed
82
        path = self.expand(path)
83
84
        # path exists physically?
        if not os.path.exists(path):
Marcel's avatar
Marcel committed
85
86
            return None
        # type correct?
87
88
89
90
91
92
93
        target_type = 'd' if os.path.isdir(path) else 'f'
        if not type:
            return target_type
        type = type.lower()
        if type not in ['f', 'd']:
            return None
        return target_type if target_type == type else None
94

Gero Müller's avatar
Gero Müller committed
95
96
97
    def get_file_list(self, path, deep=False,
                      filter=None, reverse=False,
                      hide_hidden=True, encode_json=True):
98
        filter = filter or []
99
        filelist = []
Marcel's avatar
Marcel committed
100
        path_expand = self.expand(path)
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        try:
            for elem in os.listdir(path_expand):
                # hide hidden files?
                if elem.startswith('.') and hide_hidden:
                    continue

                # excluded by filters?
                match = False
                for filter_elem in filter:
                    if re.search(filter_elem, elem):
                        match = True
                if match != reverse:
                    continue

                fullpath = os.path.join(path_expand, elem)

Gero Müller's avatar
Gero Müller committed
117
                stats = os.lstat(fullpath)
Gero Müller's avatar
Gero Müller committed
118
119
120
                is_symlink = stat.S_ISLNK(stats.st_mode)
                if is_symlink:
                    stats = os.stat(fullpath)
121
122
                size = stats.st_size
                mtime = stats.st_mtime
123

Gero Müller's avatar
Gero Müller committed
124
                if stat.S_ISDIR(stats.st_mode):
Gero Müller's avatar
Gero Müller committed
125
                    filelist.append({'name': elem, 'symlink': is_symlink, 'type': 'd', 'mtime': mtime, 'size': size})
126
127
128
                    if deep:
                        filelist.extend(self.get_file_list(fullpath, deep, filter, reverse))
                else:
Gero Müller's avatar
Gero Müller committed
129
                    filelist.append({'name': elem, 'symlink': is_symlink, 'type': 'f', 'mtime': mtime, 'size': size})
130
131
132
133
        except Exception, e:
            filelist.append({'warning': str(e)})

        # Determine the parent
134
        parentpath = path_expand[:-1] if path_expand.endswith(os.sep) and path_expand != os.sep else path_expand
135
        parentpath = os.path.dirname(path_expand)
136
137
138
        data = {'filelist': filelist, 'parentpath': parentpath}
        if encode_json:
            return json.dumps(data)
139
140
        else:
            return data
141

142
    def get_suggestions(self, path, length=1, append_hidden=True, encode_json=True):
Marcel's avatar
Marcel committed
143
144
145
        suggestions = []
        source, filter = None, None
        # does the path exist?
Marcel's avatar
Marcel committed
146
147
        path_expanded = self.expand(path)
        if os.path.exists(path_expanded):
Marcel's avatar
Marcel committed
148
            # dir case
Marcel's avatar
Marcel committed
149
            if os.path.isdir(path_expanded):
Marcel's avatar
Marcel committed
150
151
152
153
154
155
156
157
158
159
160
                if path.endswith('/'):
                    source = path
                else:
                    suggestions.append(path + os.sep)
                    return suggestions
            # file case
            else:
                return suggestions
        else:
            # try to estimate source and filter
            head, tail = os.path.split(path)
Marcel's avatar
Marcel committed
161
            if os.path.isdir(os.path.expanduser(os.path.expandvars(head))):
Marcel's avatar
Marcel committed
162
163
164
165
166
167
168
                source = head
                filter = tail

        # return empty suggestions when source is not set
        if not source:
            return suggestions

Marcel's avatar
Marcel committed
169
        files = os.listdir(os.path.expanduser(os.path.expandvars(source)))
Marcel's avatar
Marcel committed
170
171
        # resort?
        if append_hidden:
172
            files = sorted(map(lambda f: str(f), files), cmp=file_compare, key=str.lower)
173
        while (len(suggestions) < length or length == 0) and len(files):
Marcel's avatar
Marcel committed
174
175
176
177
            file = files.pop(0)
            if filter and not file.startswith(filter):
                continue
            suggestion = os.path.join(source, file)
Marcel's avatar
Marcel committed
178
            if not suggestion.endswith('/') and os.path.isdir(os.path.expanduser(os.path.expandvars(suggestion))):
Marcel's avatar
Marcel committed
179
180
181
                suggestion += '/'
            suggestions.append(suggestion)

182
        return suggestions if not encode_json else json.dumps(suggestions)
183

murban's avatar
murban committed
184
    def cut_slashs(self, path):
Marcel's avatar
Marcel committed
185
        path = self.expand(path)
murban's avatar
murban committed
186
        path = path[1:] if path.startswith(os.sep) else path
187
188
        if path == "":
            return path
murban's avatar
murban committed
189
        path = path[:-1] if path.endswith(os.sep) else path
190
191
        return path

murban's avatar
murban committed
192
    def create_folder(self, path, name):
Marcel's avatar
Marcel committed
193
        path = self.expand(path)
194
        # folder with the same name existent?
195
        name = self.handle_file_name_collision(name, path)
Marcel's avatar
Marcel committed
196
        fullpath = os.path.join(path, name)
197
198
199
        try:
            os.mkdir(fullpath)
        except Exception as e:
200
            # raise Exception("You don't have the permission to create this folder!")
201
202
            raise Exception(str(e))

murban's avatar
murban committed
203
    def create_file(self, path, name):
Marcel's avatar
Marcel committed
204
        path = self.expand(path)
205
        # file with the same name existent?
206
        name = self.handle_file_name_collision(name, path)
Marcel's avatar
Marcel committed
207
        fullpath = os.path.join(path, name)
208
209
210
211
212
213
        try:
            f = file(fullpath, "w")
            f.close()
        except Exception as e:
            raise Exception(str(e))

214
215
216
217
218
219
220
221
222
223
    def rename(self, path, name, new_name, force=False):
        try:
            if force == False:
                new_name = self.handle_file_name_collision(new_name, path)
            name = os.path.join(path, name)
            new_name = os.path.join(path, new_name)
            os.renames(name, new_name)
            return
        except Exception, e:
            return str(e)
224

murban's avatar
murban committed
225
226
    def remove(self, path):
        if isinstance(path, list):
227
228
229
            for p in path:
                self.remove(p)
        else:
Marcel's avatar
Marcel committed
230
231
232
233
234
            path = self.expand(path)
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
235
236

    def compress(self, path, paths, name):
Martin Urban's avatar
Martin Urban committed
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
        try:
            # paths has to be a list of strings
            paths = paths if isinstance(paths, (list, tuple)) else [paths]

            path = path if not path.endswith(os.sep) else path[:-1]

            name = name if name.endswith(".zip") else name + ".zip"
            name = self.handle_file_name_collision(name, path)

            fullpath = os.path.join(path, name)

            with ZipFile(fullpath, "w") as archive:
            # archive =  ZipFile(fullpath, "w")
                path = path if path.startswith(os.sep) else path
                for p in paths:
                    if p is None or p == "":
                        continue
                    p = p if p.startswith(os.sep) else os.sep + path
                    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(fullp, ap)

                    ap = p[len(path):] if p.startswith(path) else p
                    archive.write(p, ap)
        # archive.close()
        except Exception, e:
            return str(e)
269

270
    def paste(self, path, fullsrc, cut):
Marcel's avatar
Marcel committed
271
        # TODO
272
273
274
275
        path = self.expand(path)
        if isinstance(fullsrc, (list, tuple)):
            for p in fullsrc:
                p = self.expand(p)
murban's avatar
murban committed
276
                self.paste(path, p, cut)
277
278
            return True

279
280
281
282
283
284
        # fulltarget = os.path.join(path, fullsrc.split(os.sep)[-1])
        logger.error("")
        target = self.handle_file_name_collision(fullsrc.split(os.sep)[-1], path)
        fulltarget = os.path.join(path, target)
        logger.error(cut)
        logger.error("")
285

286
287
        if os.path.isdir(fullsrc):
            shutil.copytree(fullsrc, fulltarget)
288
            if cut:
289
                shutil.rmtree(fullsrc)
290
        else:
291
            shutil.copy2(fullsrc, fulltarget)
292
            if cut:
293
                os.remove(fullsrc)
294

Marcel's avatar
Marcel committed
295
    def save_file(self, path, content, binary=False, force=True):
296
297
298
        logger.info(path)
        logger.info("\n\n")
        return
Marcel's avatar
Marcel committed
299
        path = self.expand(path)
Marcel's avatar
Marcel committed
300
        # check if file already exists
murban's avatar
murban committed
301
        if os.path.exists(path) and not force:
Marcel's avatar
Marcel committed
302
            return False
Marcel's avatar
Marcel committed
303
        if binary:
304
305
306
307
308
            out = file(path, "wb")
        else:
            out = file(path, "w")
        logger.info("saved")
        logger.info("\n\n")
Marcel's avatar
Marcel committed
309
        out.write(content)
310
        out.close()
Marcel's avatar
Marcel committed
311
        return True
312

Gero Müller's avatar
Gero Müller committed
313
314
    def save_file_content(self, filename, content,
                        path=None, force=True, append=False):
315
316
        # check write permissions
        # if not self.checkPermission(username, vPath, 'w'):
Gero Müller's avatar
Gero Müller committed
317
318
319
320
321
322
        #    return False, "Permission denied!"

        if path:
            filename = os.path.join(path, filename)
        filename = self.expand(filename)

323
        # check if file already exists
Gero Müller's avatar
Gero Müller committed
324
325
326
327
328
329
330
331
332
        if os.path.exists(filename) and not force:
            return False, "The file '%s' already exists!" % filename

        out = open(filename, "ab+" if append else "wb")
        out.write(content)
        out.close()

        return True, "File saved!"

murban's avatar
murban committed
333
    def get_file_content(self, path):
Marcel's avatar
Marcel committed
334
        path = self.expand(path)
Gero Müller's avatar
Gero Müller committed
335
        f = open(path, "rb")
336
337
        content = f.read()
        f.close()
Marcel's avatar
Marcel committed
338
        return content
339

Marcel's avatar
Marcel committed
340
    def get_mtime(self, path):
Marcel's avatar
Marcel committed
341
        path = self.expand(path)
Marcel's avatar
Marcel committed
342
343
        return os.path.getmtime(path)

murban's avatar
murban committed
344
    def is_browser_file(self, path):
Marcel's avatar
Marcel committed
345
        path = self.expand(path)
346
        extension = path.split(".")[-1]
347
        return extension.lower() in FileSystem.BROWSER_EXTENSIONS
348

murban's avatar
murban committed
349
    def handle_file_name_collision(self, name, path):
350
        # collision?
murban's avatar
murban committed
351
        files = os.listdir(path)
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
382
383
384
385
386
        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
murban's avatar
murban committed
387
        return self.handle_file_name_collision(newname, path)
Marcel's avatar
Marcel committed
388

Marcel's avatar
Marcel committed
389
390
    def expand(self, path):
        return os.path.expanduser(os.path.expandvars(path))
Marcel's avatar
Marcel committed
391

Gero Müller's avatar
Gero Müller committed
392
    def thumbnail(self, path, width=100, height=100, sharpen=True):
393
394
395
        path = self.expand(path)
        if HAVE_PIL:
            output = StringIO()
Gero Müller's avatar
Gero Müller committed
396
397
398
            img = Image.open(path)
            img.thumbnail((width, height))
            if sharpen:
399
                img.filter(ImageFilter.SHARPEN)
Gero Müller's avatar
Gero Müller committed
400
            img.save(output, "JPEG")
401
402
403
404
405
406
407
408
409
410
411
412
413
            contents = output.getvalue()
            output.close()
            return contents
        elif HAVE_CONVERT:
            cmd = ['convert', path,
                   '-thumbnail', '%dx%d' % (width, height),
                   'jpeg:-']
            p = subprocess.Popen(cmd, stdin=None, stdout=subprocess.PIPE)
            return p.communicate()[0]
        else:
            return self.get_file_content(path)


Marcel's avatar
Marcel committed
414
415
416
417
418
419
420
421
def string_compare(a, b):
    if a == b:
        return 0
    elif a > b:
        return 1
    else:
        return -1

422

Marcel's avatar
Marcel committed
423
424
425
426
427
428
429
430
431
def file_compare(a, b):
    if not a.startswith('.') and not b.startswith('.'):
        return string_compare(a, b)
    elif a.startswith('.') and b.startswith('.'):
        return string_compare(a, b)
    elif a.startswith('.') and not b.startswith('.'):
        return 1
    elif not a.startswith('.') and b.startswith('.'):
        return -1