summaryrefslogtreecommitdiff
path: root/fs/osfs.py
blob: 9fc7342dddf3d1cf217383672a85944d77231da2 (plain)
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
#!/usr/bin/env python

from base import *
from helpers import *

try:
    import xattr
except ImportError:
    xattr = None

class OSFS(FS):

    """The most basic of filesystems. Simply shadows the underlaying filesytem
    of the Operating System.

    """

    def __init__(self, root_path, thread_syncronize=True):
        FS.__init__(self, thread_syncronize=thread_syncronize)

        expanded_path = normpath(os.path.expanduser(os.path.expandvars(root_path)))

        if not os.path.exists(expanded_path):
            raise ResourceNotFoundError("NO_DIR", expanded_path, msg="Root directory does not exist: %(path)s")
        if not os.path.isdir(expanded_path):
            raise ResourceNotFoundError("NO_DIR", expanded_path, msg="Root path is not a directory: %(path)s")

        self.root_path = normpath(os.path.abspath(expanded_path))

    def __str__(self):
        return "<OSFS: %s>" % self.root_path

    __repr__ = __str__

    def getsyspath(self, path, allow_none=False):
        sys_path = os.path.join(self.root_path, makerelative(self._resolve(path))).replace('/', os.sep)
        return sys_path

    def open(self, path, mode="r", **kwargs):
        try:
            f = open(self.getsyspath(path), mode, kwargs.get("buffering", -1))
        except IOError, e:
            if e.errno == 2:
                raise ResourceNotFoundError("NO_FILE", path)
            raise OperationFailedError("OPEN_FAILED", path, details=e, msg=str(e))

        return f

    def exists(self, path):
        path = self.getsyspath(path)
        return os.path.exists(path)

    def isdir(self, path):
        path = self.getsyspath(path)
        return os.path.isdir(path)

    def isfile(self, path):
        path = self.getsyspath(path)
        return os.path.isfile(path)

    def ishidden(self, path):
        return path.startswith('.')

    def listdir(self, path="./", wildcard=None, full=False, absolute=False, hidden=True, dirs_only=False, files_only=False):
        try:
            paths = os.listdir(self.getsyspath(path))
        except (OSError, IOError), e:
            raise OperationFailedError("LISTDIR_FAILED", path, details=e, msg="Unable to get directory listing: %(path)s - (%(details)s)")

        return self._listdir_helper(path, paths, wildcard, full, absolute, hidden, dirs_only, files_only)

    def makedir(self, path, mode=0777, recursive=False, allow_recreate=False):
        sys_path = self.getsyspath(path)

        try:
            if recursive:
                os.makedirs(sys_path, mode)
            else:
                if not allow_recreate and self.exists(path):
                    raise OperationFailedError("MAKEDIR_FAILED", dirname, msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s")
                try:
                    os.mkdir(sys_path, mode)
                except OSError, e:
                    if allow_recreate:
                        if e.errno != 17:
                            raise OperationFailedError("MAKEDIR_FAILED", path)
                    else:
                        raise OperationFailedError("MAKEDIR_FAILED", path)
                except WindowsError, e:
                    if allow_recreate:
                        if e.errno != 183:
                            raise OperationFailedError("MAKEDIR_FAILED", path)
                    else:
                        raise OperationFailedError("MAKEDIR_FAILED", path)

        except OSError, e:
            if e.errno == 17:
                return
            else:
                raise OperationFailedError("MAKEDIR_FAILED", path, details=e)

    def remove(self, path):
        sys_path = self.getsyspath(path)
        try:
            os.remove(sys_path)
        except OSError, e:
            raise OperationFailedError("REMOVE_FAILED", path, details=e)

    def removedir(self, path, recursive=False,force=False):
        sys_path = self.getsyspath(path)
        #  Don't remove the root directory of this FS
        if path in ("","/"):
            return
        if force:
            for path2 in self.listdir(path,absolute=True,files_only=True):
                self.remove(path2)
            for path2 in self.listdir(path,absolute=True,dirs_only=True):
                self.removedir(path2,force=True)
        try:
            os.rmdir(sys_path)
        except OSError, e:
            raise OperationFailedError("REMOVEDIR_FAILED", path, details=e)
        #  Using os.removedirs() for this can result in dirs being
        #  removed outside the root of this FS, so we recurse manually.
        if recursive:
            try:
                self.removedir(dirname(path),recursive=True)
            except OperationFailedError:
                pass

    def rename(self, src, dst):
        if not issamedir(src, dst):
            raise ValueError("Destination path must the same directory (user the move method for moving to a different directory)")
        path_src = self.getsyspath(src)
        path_dst = self.getsyspath(dst)

        try:
            os.rename(path_src, path_dst)
        except OSError, e:
            raise OperationFailedError("RENAME_FAILED", src)

    def getinfo(self, path):
        sys_path = self.getsyspath(path)

        try:
            stats = os.stat(sys_path)
        except OSError, e:
            raise FSError("UNKNOWN_ERROR", path, details=e)

        info = dict((k, getattr(stats, k)) for k in dir(stats) if not k.startswith('__') )

        info['size'] = info['st_size']

        ct = info.get('st_ctime', None)
        if ct is not None:
            info['created_time'] = datetime.datetime.fromtimestamp(ct)

        at = info.get('st_atime', None)
        if at is not None:
            info['accessed_time'] = datetime.datetime.fromtimestamp(at)

        mt = info.get('st_mtime', None)
        if mt is not None:
            info['modified_time'] = datetime.datetime.fromtimestamp(at)

        return info


    def getsize(self, path):
        sys_path = self.getsyspath(path)
        try:
            stats = os.stat(sys_path)
        except OSError, e:
            raise FSError("UNKNOWN_ERROR", path, details=e)

        return stats.st_size


    def setxattr(self, path, key, value):
        self._lock.acquire()
        try:
            if xattr is None:
                return FS.setxattr(self, path, key, value)
            try:
                xattr.xattr(self.getsyspath(path))[key]=value
            except IOError, e:
                if e.errno == 95:
                    return FS.setxattr(self, path, key, value)
                else:
                    raise OperationFailedError('XATTR_FAILED', path, details=e)
        finally:
            self._lock.release()

    def getxattr(self, path, key, default=None):
        self._lock.acquire()
        try:
            if xattr is None:
                return FS.getxattr(self, path, key, default)
            try:
                return xattr.xattr(self.getsyspath(path)).get(key)
            except IOError, e:
                if e.errno == 95:
                    return FS.getxattr(self, path, key, default)
                else:
                    raise OperationFailedError('XATTR_FAILED', path, details=e)
        finally:
            self._lock.release()

    def removexattr(self, path, key):
        self._lock.acquire()
        try:
            if xattr is None:
                return FS.removexattr(self, path, key)
            try:
                del xattr.xattr(self.getsyspath(path))[key]
            except KeyError:
                pass
            except IOError, e:
                if e.errono == 95:
                    return FS.removexattr(self, path, key)
                else:
                    raise OperationFailedError('XATTR_FAILED', path, details=e)
        finally:
            self._lock.release()

    def listxattrs(self, path):
        self._lock.acquire()
        try:
            if xattr is None:
                return FS.listxattrs(self, path)
            try:
                return xattr.xattr(self.getsyspath(path)).keys()
            except IOError, e:
                if errono == 95:
                    return FS.listxattrs(self, path)
                else:
                    raise OperationFailedError('XATTR_FAILED', path, details=e)
        finally:
            self._lock.release()



if __name__ == "__main__":


    osfs = OSFS('testfs')




    #a = xattr.xattr('/home/will/projects/pyfilesystem/fs/testfs/test.txt')
    #a['user.comment'] = 'world'

    #print xattr.xattr('/home/will/projects/pyfilesystem/fs/testfs/test.txt').keys()

    print osfs.listxattrs('test.txt')
    osfs.removexattr('test.txt', 'user.foo')
    #print osfs.listxattrs('test.txt')
    osfs.setxattr('test.txt', 'user.foo', 'bar')
    print osfs.getxattr('test.txt', 'user.foo')
    print osfs.listxattrs('test.txt')
    print osfs.getxattrs('test.txt')

    #
    #osfs = OSFS("~/projects")
    #
    #
    ##for p in osfs.walk("tagging-trunk", search='depth'):
    ##    print p
    #
    #import browsewin
    #browsewin.browse(osfs)
    #
    #print_fs(osfs)
    #
    ##print osfs.listdir("/projects/fs")
    #
    ##sub_fs = osfs.open_dir("projects/")
    #
    ##print sub_fs
    #
    ##sub_fs.open('test.txt')
    #
    ##print sub_fs.listdir(dirs_only=True)
    ##print sub_fs.listdir()
    ##print_fs(sub_fs, max_levels=2)
    #
    ##for f in osfs.listdir():
    ##    print f
    #
    ##print osfs.listdir('projects', dirs_only=True, wildcard="d*")
    #
    ##print_fs(osfs, 'projects/')
    #
    #print pathjoin('/', 'a')
    #
    #print pathjoin('a/b/c', '../../e/f')