|
|
@@ -32,11 +32,14 @@ from twisted.web import http |
|
|
|
from twisted.web import server |
|
|
|
from twisted.web import resource |
|
|
|
|
|
|
|
def inserthierdict(d, name, obj): |
|
|
|
def inserthierdict(d, name, obj, sep): |
|
|
|
if not name: |
|
|
|
return |
|
|
|
i = name.find('/') |
|
|
|
if i == -1: |
|
|
|
|
|
|
|
if sep is not None: |
|
|
|
i = name.find(sep) |
|
|
|
|
|
|
|
if sep is None or i == -1: |
|
|
|
d[name] = obj |
|
|
|
return |
|
|
|
|
|
|
@@ -44,15 +47,18 @@ def inserthierdict(d, name, obj): |
|
|
|
rname = name[i + 1:] |
|
|
|
# remaining path components |
|
|
|
try: |
|
|
|
inserthierdict(d[dname], rname, obj) |
|
|
|
inserthierdict(d[dname], rname, obj, sep) |
|
|
|
except KeyError: |
|
|
|
d[dname] = {} |
|
|
|
inserthierdict(d[dname], rname, obj) |
|
|
|
inserthierdict(d[dname], rname, obj, sep) |
|
|
|
|
|
|
|
def buildNameHier(names, objs): |
|
|
|
def buildNameHier(names, objs, sep): |
|
|
|
ret = {} |
|
|
|
for n, o in itertools.izip(names, objs): |
|
|
|
inserthierdict(ret, n, o) |
|
|
|
#Skip directories in a TarFile or RarFile |
|
|
|
if hasattr(o, 'isdir') and o.isdir(): |
|
|
|
continue |
|
|
|
inserthierdict(ret, n, o, sep) |
|
|
|
|
|
|
|
return ret |
|
|
|
|
|
|
@@ -176,7 +182,8 @@ class ZipChildDir(ZipItem, StorageFolder): |
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
self.hier = kwargs['hier'] |
|
|
|
del kwargs['hier'] |
|
|
|
self.sep = kwargs['sep'] |
|
|
|
del kwargs['hier'], kwargs['sep'] |
|
|
|
ZipItem.__init__(self, *args, **kwargs) |
|
|
|
del kwargs['zf'], kwargs['zo'], kwargs['name'] |
|
|
|
StorageFolder.__init__(self, *args, **kwargs) |
|
|
@@ -194,17 +201,19 @@ class ZipChildDir(ZipItem, StorageFolder): |
|
|
|
self.cd.delItem(self.pathObjmap[i]) |
|
|
|
del self.pathObjmap[i] |
|
|
|
|
|
|
|
cursep = self.sep |
|
|
|
for i in children: |
|
|
|
if i in self.pathObjmap: |
|
|
|
continue |
|
|
|
|
|
|
|
# new object |
|
|
|
pathname = os.path.join(self.name, i) |
|
|
|
pathname = cursep.join((self.name, i)) |
|
|
|
if isinstance(self.hier[i], dict): |
|
|
|
# must be a dir |
|
|
|
self.pathObjmap[i] = self.cd.addItem(self.id, |
|
|
|
ZipChildDir, i, zf = self.zf, zo = self, |
|
|
|
name = pathname, hier = self.hier[i]) |
|
|
|
ZipChildDir, i, zf=self.zf, zo=self, |
|
|
|
name=pathname, hier=self.hier[i], |
|
|
|
sep=cursep) |
|
|
|
else: |
|
|
|
klass, mt = FileDIDL.buildClassMT(ZipFile, i) |
|
|
|
if klass is None: |
|
|
@@ -257,6 +266,8 @@ def genZipFile(path): |
|
|
|
raise |
|
|
|
|
|
|
|
class ZipObject(FSObject, StorageFolder): |
|
|
|
seps = [ '/', '\\' ] |
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
'''If a zip argument is passed it, use that as the zip archive.''' |
|
|
|
path = kwargs['path'] |
|
|
@@ -271,7 +282,16 @@ class ZipObject(FSObject, StorageFolder): |
|
|
|
def doUpdate(self): |
|
|
|
# open the zipfile as necessary. |
|
|
|
self.zip = genZipFile(self.FSpath) |
|
|
|
hier = buildNameHier(self.zip.namelist(), self.zip.infolist()) |
|
|
|
nl = self.zip.namelist() |
|
|
|
cnt = 0 |
|
|
|
cursep = None |
|
|
|
for i in self.seps: |
|
|
|
newsum = sum([ j.count(i) for j in nl ]) |
|
|
|
if newsum > cnt: |
|
|
|
cursep = i |
|
|
|
cnt = newsum |
|
|
|
self.sep = cursep |
|
|
|
hier = buildNameHier(nl, self.zip.infolist(), cursep) |
|
|
|
|
|
|
|
doupdate = False |
|
|
|
children = sets.Set(hier.keys()) |
|
|
@@ -290,8 +310,8 @@ class ZipObject(FSObject, StorageFolder): |
|
|
|
if isinstance(hier[i], dict): |
|
|
|
# must be a dir |
|
|
|
self.pathObjmap[i] = self.cd.addItem(self.id, |
|
|
|
ZipChildDir, i, zf = self.zip, zo = self, |
|
|
|
name = i, hier = hier[i]) |
|
|
|
ZipChildDir, i, zf=self.zip, zo=self, |
|
|
|
name=i, hier=hier[i], sep=cursep) |
|
|
|
else: |
|
|
|
klass, mt = FileDIDL.buildClassMT(ZipFile, i) |
|
|
|
if klass is None: |
|
|
|