mrmp/catalog.py

37 lines
1,003 B
Python
Raw Normal View History

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-10 19:30:40 +00:00
CATALOG = '.files'
2021-10-09 23:18:03 +00:00
FILES = 'files'
PACKAGE = 'desc'
2021-10-09 21:39:16 +00:00
class Catalog:
2021-10-10 19:30:40 +00:00
def __init__(self, subsystem):
self.subsystem = subsystem
self.path = os.path.join(self.subsystem.path,
f'{self.subsystem.name}{CATALOG}')
2021-10-09 21:39:16 +00:00
self.load()
def load(self):
2021-10-10 19:30:40 +00:00
binary = self.subsystem.architecture.repository.get_file(self.path)
f = io.BytesIO(binary)
2021-10-09 21:39:16 +00:00
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