2021-10-09 21:39:16 +00:00
|
|
|
import io
|
2021-10-09 23:18:03 +00:00
|
|
|
import os
|
2021-10-09 21:39:16 +00:00
|
|
|
import tarfile
|
|
|
|
|
|
|
|
import package
|
|
|
|
|
2021-10-09 23:18:03 +00:00
|
|
|
FILES = 'files'
|
|
|
|
PACKAGE = 'desc'
|
|
|
|
|
2021-10-09 21:39:16 +00:00
|
|
|
|
|
|
|
class Catalog:
|
|
|
|
def __init__(self, binary):
|
|
|
|
self.binary = binary
|
|
|
|
self.load()
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
f = io.BytesIO(self.binary)
|
|
|
|
archive = tarfile.open(fileobj=f)
|
2021-10-09 23:18:03 +00:00
|
|
|
m = {}
|
|
|
|
packages = {}
|
2021-10-09 21:39:16 +00:00
|
|
|
for member in archive.getmembers():
|
2021-10-09 23:18:03 +00:00
|
|
|
directory, *file = member.name.split(os.sep)
|
|
|
|
if file:
|
|
|
|
d = m[directory]
|
|
|
|
d[file[0]] = archive.extractfile(member).read()
|
|
|
|
if len(d) == 2:
|
|
|
|
p = package.Package(d[PACKAGE], d[FILES])
|
|
|
|
packages[p.name] = p
|
|
|
|
else:
|
|
|
|
m[directory] = {}
|
2021-10-09 21:39:16 +00:00
|
|
|
archive.close()
|
2021-10-09 23:18:03 +00:00
|
|
|
self.packages = packages
|