| 1 | # Copyright: 2006 Brian Harring <ferringb@gmail.com> |
|---|
| 2 | # License: GPL2 |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | tar file access |
|---|
| 6 | |
|---|
| 7 | monkey patching of stdlib tarfile to reduce mem usage (33% reduction). |
|---|
| 8 | |
|---|
| 9 | note this is also racey; N threads trying an import, if they're after |
|---|
| 10 | the *original* tarfile, they may inadvertantly get ours. |
|---|
| 11 | """ |
|---|
| 12 | |
|---|
| 13 | import sys |
|---|
| 14 | t = sys.modules.pop("tarfile", None) |
|---|
| 15 | tarfile = __import__("tarfile") |
|---|
| 16 | if t is not None: |
|---|
| 17 | sys.modules["tarfile"] = t |
|---|
| 18 | else: |
|---|
| 19 | del sys.modules["tarfile"] |
|---|
| 20 | del t |
|---|
| 21 | # ok, we now have our own local copy to monkey patch |
|---|
| 22 | |
|---|
| 23 | class TarInfo(tarfile.TarInfo): |
|---|
| 24 | __slots__ = ( |
|---|
| 25 | "name", "mode", "uid", "gid", "size", "mtime", "chksum", "type", |
|---|
| 26 | "linkname", "uname", "gname", "devmajor", "devminor", "prefix", |
|---|
| 27 | "offset", "offset_data", "buf", "sparse", "_link_target") |
|---|
| 28 | |
|---|
| 29 | tarfile.TarInfo = TarInfo |
|---|
| 30 | # finished monkey patching. now to lift things out of our tarfile |
|---|
| 31 | # module into this scope so from/import behaves properly. |
|---|
| 32 | |
|---|
| 33 | for x in tarfile.__all__: |
|---|
| 34 | locals()[x] = getattr(tarfile, x) |
|---|
| 35 | del x |
|---|