File: vfs.py

package info (click to toggle)
python-bumps 1.0.0b2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,144 kB
  • sloc: python: 23,941; xml: 493; ansic: 373; makefile: 209; sh: 91; javascript: 90
file content (551 lines) | stat: -rw-r--r-- 16,643 bytes parent folder | download
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"""
Redirect calls to open, etc. to a virtual file system.

Use this to mount a zip file as a file system, and then all subsequent calls
to chdir, open, etc. will reference files in the zip file instead of the disk.

This will only work for packages which do all their I/O in python, and not
those which use direct calls to the C library, so for example, it will not
work with h5py. XML parsing from a zip file is also unlikely to work since
expat uses the C library directly for parsing.

Usage::

    # Do this before importing any other modules!  It sets up hooks for
    # redirecting filesystem access even if the module imports the symbol
    # directly as "from os import getcwd".
    import vfs
    vfs.vfs_init()
    ...
    with vfs.ZipFS('data.zip'):
        data = np.loadtxt('file1.dat')

Filesystems available:

* `RealFS` - uses the builtin python functions to access the O/S.

* `ZipFS` - opens a zip file as the filesystem root.

Calls redirected::

    __builtin__.open (python 2 only)
    builtins.open (python 2 and 3)
    io.open (python 2 and 3)
    os, nt, posix:
        chdir, getcwd, listdir
    os.path, ntpath, posixpath:
        exists, isfile, isir, abspath, realpath

You can also use the file systems directly without using the `vfs_init`
hook or the with statement.  Just call `fs.chdir`, etc. on the file system
object.

*file* in python 2.x is a type as well as constructor, so a simple redirect
to a replacement constructor will not work. Don't try to support it since
it is gone in python 3.

Works with numpy.loadtxt and pandas parsers.

For pandas, either need to specify *engine="python"* or pass an open call
to the reader; if you just pass a filename, then it will try opening it
with the libc open function and fail.  Could potentially monkeypatch
pandas to pre-open the file.
"""

import sys
import os
import os.path
import io
from functools import wraps

try:
    import __builtin__
    from __builtin__ import open as _py2_open
except ImportError:
    __builtin__ = _py2_open = None

try:
    import builtins
except ImportError:
    builtins = None

try:
    from pathlib import PurePath
except ImportError:

    class PurePath:
        pass


# Sphinx hack: in order to avoid having sphinx pick up the docstrings for the
# builtin functions (chdir and listdir don't format properly), simply suppress
# all classes for the module.
__all__ = []

# TODO: restructure according to pathlib interface
# Looking at pathlib, it already provides methods close to what we implement
# for our virtual file system.  By following the pathlib interface directly,
# then any code that is set up to use pathlib can use our virtual file systems
# without change (in particular, without monkeypatching python builtins).

# for functions that work for read-only filesystems, use *fn
# for functions implemented as python, use -fn
#
# os functions
#   *chdir, *getcwd, *listdir, rmdir, mkdir, chroot
#   *open, *stat, *access, rename, link, unlink, remove chmod, chown, chflags
#   *getcwdb, *getcwdu  # bytes getcwd (py3), unicode getcwd (py2)
#   *scandir
#   -makedirs   # uses exists, split, mkdir
#   -removedirs # uses split, rmdir
#   -walk       # uses islink, join, isdir, listdir
#   -glob.glob  # uses lexists, isdir, join, scandir

# symbolic link functions
#   *readlink, *lstat, symlink, lchflags, lchmod, lchown
#   *os.path.lexists

# os.path functions
#   *exists, *isfile, *isdir
#   *get[acm]time, *getsize
#   samefile    # uses stat, samestat
#   samestat    # uses filestat.dev and filestat.inode; compares device/inode
#   -isabs      # uses str; returns s.startswith('/')
#   -normpath   # uses str; pure path manipulation
#   -abspath    # uses isabs, normpath, getcwd/getcwdu
#   -realpath   # uses isabs, split, join, islink, readlink
#   -renames    # uses exists, split, rename, makedirs, removedirs
#   -walk       # uses listdir, lstat;  deprecated in favour of os.walk

# os/os.path constants
#   curdir, pardir, sep, pathsep, defpath, extsep, altsep, linesep

# file descriptor operations in os
#   fchdir, fchmod, fchown, fdopen, close, fstat, fstatvfs, fpathconf,
#   lseek, read, dup, dup2, errno, error, closerange, isatty, openpty,
#   mknod


# Protect against reload(vfs). Assume that if the symbol is already a
# wrapped symbol that we want to retrieve the original unwrapped symbol
# rather than our wrapper. Additionally, assume nobody else is foolish
# enough to be wrapping such low-level symbols...
def _unwrap(fn):
    return getattr(fn, "__wrapped__", fn)


_py2_open = _unwrap(_py2_open)
_py3_open = _unwrap(io.open)
_chdir = _unwrap(os.chdir)
_getcwd = _unwrap(os.getcwd)
_exists = _unwrap(os.path.exists)
_isfile = _unwrap(os.path.isfile)
_isdir = _unwrap(os.path.isdir)
_listdir = _unwrap(os.listdir)
# TODO: maybe use builtin versions?
_abspath = _unwrap(os.path.abspath)
_realpath = _unwrap(os.path.realpath)


class RealFS(object):
    def __enter__(self):
        pushfs(RealFS)

    def __exit__(self, *args, **kw):
        popfs()

    open = _py3_open
    py2_open = _py2_open
    getcwd = _getcwd
    chdir = _chdir
    listdir = _listdir
    abspath = _abspath
    realpath = _realpath
    isfile = _isfile
    isdir = _isdir
    exists = _exists


class ZipFS(object):
    """
    Opens a zip file as the root file system.
    """

    def __init__(self, path):
        import zipfile

        # TODO: can we open a zip within a zip?
        # Apparently yes, but only if we read the file into a byte stream and
        # then work from that file.  See the following stackoverflow answer:
        #    https://stackoverflow.com/questions/12025469/how-to-read-from-a-zip-file-within-zip-file-in-python
        self._path = _realpath(path)
        self._wd = "/"
        self._zip = zipfile.ZipFile(path)

    def __enter__(self):
        pushfs(self)
        return self._zip

    def __exit__(self, *args, **kw):
        popfs()

    def open(self, file, mode="r", buffering=-1, encoding=None, errors=None, newline=None, **kw):
        # abspath handles pathlib
        # Note: python 3 zipfile only supports mode rb; to get unicode
        # decoding, need to wrap the binary stream in a text I/O wrapper.
        zipmode = "r"
        with RealFS():
            fd = self._zip.open(self.abspath(file)[1:], mode=zipmode)
        if "b" in mode:
            return fd
        else:
            return io.TextIOWrapper(fd, encoding=encoding, errors=errors, newline=newline)

    def py2_open(self, name, mode="r", buffering=-1):
        # abspath handles pathlib
        # Note: python 2 zipfile supports modes r, rU, and U, but not rb
        zipmode = "r" if mode == "rb" else mode
        with RealFS():
            fd = self._zip.open(self.abspath(name)[1:], mode=zipmode)
        return fd

    def chdir(self, path):
        # abspath handles pathlib
        if self.isdir(path):
            self._wd = self.abspath(path) + "/"

    def _iter_dir(self, path=None):
        # TODO: turn this into a scandir interface
        # abspath handles pathlib
        path = self._wd if path is None else self.abspath(path)
        path = path[1:]
        n = len(path)
        seen = {}
        for f in self._zip.filelist:
            if not f.filename.startswith(path):
                # it is not part of the tree
                continue
            parts = f.filename.split("/")
            if len(parts) == 1:
                # it is a leaf so report it
                yield parts[0]
            elif parts[0] not in seen:
                # it is a directory, so only report it if it has not already
                # been reported
                seen.add(parts[0])
                yield parts[0]

    def listdir(self, path=None):
        # abspath handles pathlib
        return [f for f in self._iter_dir(path)]

    def abspath(self, path):
        if isinstance(path, PurePath):
            path = path.as_posix()
        if hasattr(path, "decode"):  # CRUFT: python 2
            path = path.decode()
        if path[0] != "/":
            path = "/".join((self._wd[:-1], path))
        return os.path.normpath(path)

    def realpath(self, filename):
        # abspath handles pathlib
        return os.path.join(self._path, self.abspath(filename))

    def isfile(self, path):
        # abspath handles pathlib
        path = self.abspath(path)[1:]
        return any(path == f.filename for f in self._zip.filelist)

    def isdir(self, s):
        # abspath handles pathlib
        path = self.abspath(s)[1:] + "/"
        for f in self._zip.filelist:
            if f.filename.startswith(path):
                return True
        return False

    def exists(self, path):
        # abspath handles pathlib
        return self.isfile(path) or self.isdir(path)


FS = RealFS
FS_STACK = []  # type: List[RealFS]


def pushfs(fs):
    global FS
    FS_STACK.append(FS)
    FS = fs


def popfs():
    global FS
    FS = FS_STACK.pop()


# Note: We need fs redirection functions as bound methods since pathlib uses
# them as class attributes. The usual filesystem methods are all builtin
# functions so they act as static methods when used as part of a class
# definition. However, when python functions are used as class attributes
# they get turned into bound methods when an object is created with self
# as the first parameter. But if the class attribute is already a bound method
# then it is left alone when the object is created.  We still get the extra
# self parameter, but we get it whether we access it as a class attribute
# or as an object attribute so it is easy to ignore.
class VFS(object):
    if _py3_open is not None:

        @wraps(_py3_open)
        def fs_py3_open(self, *args, **kw):
            return FS.open(*args, **kw)

    if _py2_open is not None:

        @wraps(_py2_open)
        def fs_py2_open(self, *args, **kw):
            return FS.py2_open(*args, **kw)

    @wraps(_chdir)
    def fs_chdir(self, *args, **kw):
        return FS.chdir(*args, **kw)

    @wraps(_getcwd)
    def fs_getcwd(self, *args, **kw):
        return FS.getcwd(*args, **kw)

    @wraps(_listdir)
    def fs_listdir(self, *args, **kw):
        return FS.listdir(*args, **kw)

    @wraps(_exists)
    def fs_exists(self, *args, **kw):
        return FS.exists(*args)

    @wraps(_isfile)
    def fs_isfile(self, *args, **kw):
        return FS.isfile(*args, **kw)

    @wraps(_isdir)
    def fs_isdir(self, *args, **kw):
        return FS.isdir(*args, **kw)

    @wraps(_abspath)
    def fs_abspath(self, *args, **kw):
        return FS.abspath(*args, **kw)

    @wraps(_realpath)
    def fs_realpath(self, *args, **kw):
        return FS.realpath(*args, **kw)


vfs = VFS()


def vfs_init():
    """
    Call this very early in your program so that various filesystem functions
    will be redirected even if they are expressed as "from module import fn"
    """
    if __builtin__ is not None:
        __builtin__.open = vfs.fs_py2_open
    if builtins is not None:
        builtins.open = vfs.fs_py3_open
    io.open = vfs.fs_py3_open
    os.chdir = vfs.fs_chdir
    os.getcwd = vfs.fs_getcwd
    os.listdir = vfs.fs_listdir
    os.path.abspath = vfs.fs_abspath
    os.path.realpath = vfs.fs_realpath
    os.path.exists = vfs.fs_exists
    os.path.isfile = vfs.fs_isfile
    os.path.isdir = vfs.fs_isdir

    try:
        import nt, ntpath

        nt.chdir = vfs.fs_chdir
        nt.listdir = vfs.fs_listdir
        nt.getcwd = vfs.fs_getcwd
        ntpath.abspath = vfs.fs_abspath
        ntpath.realpath = vfs.fs_realpath
        ntpath.exists = vfs.fs_exists
        ntpath.isfile = vfs.fs_isfile
        ntpath.isdir = vfs.fs_isdir
    except ImportError:
        pass

    try:
        import posix, posixpath

        posix.chdir = vfs.fs_chdir
        posix.listdir = vfs.fs_listdir
        posix.getcwd = vfs.fs_getcwd
        posixpath.abspath = vfs.fs_abspath
        posixpath.realpath = vfs.fs_realpath
        posixpath.exists = vfs.fs_exists
        posixpath.isfile = vfs.fs_isfile
        posixpath.isdir = vfs.fs_isdir
    except ImportError:
        pass

    # Pathlib may be imported really early.  Make sure it sees the vfs.
    # TODO: with reload some isinstance tests may fail --- monkeypatch instead?
    # Should be just an update to the members of pathlib._NormalAccessor.
    # With reload, os.PathLike.register(PurePath) is being called twice but that
    # shouldn't be a problem, especially because PurePath will have changed.
    try:
        import pathlib
        from importlib import reload

        reload(pathlib)
    except ImportError:
        pass


# CRUFT: use old wrappers for python 2 since new wrappers don't seem to work
if sys.version_info[0] == 2:

    class RealFS(object):
        def __enter__(self):
            pushfs(self)

        def __exit__(self, *args, **kw):
            popfs()

        def open(self, *args, **kw):
            return _py3_open(*args, **kw)

        def py2_open(self, *args, **kw):
            return _py2_open(*args, **kw)

        def getcwd(self):
            return _getcwd()

        def chdir(self, path):
            return _chdir(path)

        def listdir(self, path=None):
            return _listdir(path) if path is not None else _listdir()

        def abspath(self, path):
            return _abspath(path)

        def realpath(self, path):
            return _realpath(path)

        def isfile(self, path):
            return _isfile(path)

        def isdir(self, path):
            return _isdir(path)

        def exists(self, path):
            return _exists(path)

    if _py3_open is not None:

        @wraps(_py3_open)
        def fs_py3_open(*args, **kw):
            return FS.open(*args, **kw)

    if _py2_open is not None:

        @wraps(_py2_open)
        def fs_py2_open(*args, **kw):
            return FS.py2_open(*args, **kw)

    @wraps(_chdir)
    def fs_chdir(*args, **kw):
        return FS.chdir(*args, **kw)

    @wraps(_getcwd)
    def fs_getcwd(*args, **kw):
        return FS.getcwd(*args, **kw)

    @wraps(_listdir)
    def fs_listdir(*args, **kw):
        return FS.listdir(*args, **kw)

    @wraps(_exists)
    def fs_exists(*args, **kw):
        return FS.exists(*args)

    @wraps(_isfile)
    def fs_isfile(*args, **kw):
        return FS.isfile(*args, **kw)

    @wraps(_isdir)
    def fs_isdir(*args, **kw):
        return FS.isdir(*args, **kw)

    @wraps(_abspath)
    def fs_abspath(*args, **kw):
        return FS.abspath(*args, **kw)

    @wraps(_realpath)
    def fs_realpath(*args, **kw):
        return FS.realpath(*args, **kw)

    def vfs_init():
        """
        Call this very early in your program so that various filesystem functions
        will be redirected even if they are expressed as "from module import fn"
        """
        global FS
        FS = RealFS()
        if __builtin__ is not None:
            __builtin__.open = fs_py2_open
        if builtins is not None:
            builtins.open = fs_py3_open
        io.open = fs_py3_open
        os.chdir = fs_chdir
        os.getcwd = fs_getcwd
        os.listdir = fs_listdir
        os.path.abspath = fs_abspath
        os.path.realpath = fs_realpath
        os.path.exists = fs_exists
        os.path.isfile = fs_isfile
        os.path.isdir = fs_isdir

        try:
            import nt, ntpath

            nt.chdir = fs_chdir
            nt.listdir = fs_listdir
            nt.getcwd = fs_getcwd
            ntpath.abspath = fs_abspath
            ntpath.realpath = fs_realpath
            ntpath.exists = fs_exists
            ntpath.isfile = fs_isfile
            ntpath.isdir = fs_isdir
        except ImportError:
            pass

        try:
            import posix, posixpath

            posix.chdir = fs_chdir
            posix.listdir = fs_listdir
            posix.getcwd = fs_getcwd
            posixpath.abspath = fs_abspath
            posixpath.realpath = fs_realpath
            posixpath.exists = fs_exists
            posixpath.isfile = fs_isfile
            posixpath.isdir = fs_isdir
        except ImportError:
            pass

        # Pathlib may be imported really early.  Make sure it sees the vfs.
        # TODO: with reload some isinstance tests may fail --- monkeypatch instead?
        # Should be just an update to the members of pathlib._NormalAccessor.
        # With reload, os.PathLike.register(PurePath) is being called twice but that
        # shouldn't be a problem, especially because PurePath will have changed.
        try:
            import pathlib
            from importlib import reload

            reload(pathlib)
        except ImportError:
            pass