import gzip import json import lzma import re import sys from pathlib import Path def key_for(value): value = re.sub(r'^(?:RELOC|texmf-dist)/', '', value) name = value.rsplit('/', 1)[-1] groups = [('tex/', 26), ('fonts/tfm/', 3), ('fonts/afm/', 4), ('bibtex/bst/', 7), ('fonts/map/', 11), ('fonts/type1/', 32), ('fonts/vf/', 33), ('fonts/enc/', 44), ('fonts/truetype/', 36), ('fonts/opentype/', 47), ('dvips/', 11 if name.endswith('.map') else 30)] return next((f'pdftex/{group}/{name}' for prefix, group in groups if value.startswith(prefix)), None) def build(source, destination): raw = Path(source).read_bytes() text = lzma.decompress(raw).decode('utf-8') if raw.startswith(b'\xfd7zXZ') else raw.decode('utf-8') packages = {} files = {} for block in text.split('\n\n'): lines = block.splitlines() def field(name): return next((line[len(name) + 1:] for line in lines if line.startswith(name + ' ')), None) name = field('name') size = field('containersize') checksum = field('containerchecksum') revision = field('revision') if not all([name, size, checksum, revision]) or not re.fullmatch(r'[a-z0-9][a-z0-9+_.-]*', name) or int(size) > 80 * 1024 * 1024: continue runtime = [] active = False for line in lines: if line.startswith('runfiles '): active = True continue if not line.startswith(' '): active = False if active: path = line.strip().split(' ', 1)[0] key = key_for(path) if key: runtime.append((path, key)) if not runtime: continue probes = sorted(runtime, key=lambda item: (not item[0].endswith('/' + name + '.sty'), not item[0].endswith('.sty'), not item[0].endswith('.cls'), item[0]))[:2] packages[name] = {'revision': int(revision), 'size': int(size), 'sha512': checksum, 'dependencies': [line[7:] for line in lines if line.startswith('depend ') and '.ARCH' not in line], 'probes': [item[1] for item in probes]} for path, key in runtime: filename = path.rsplit('/', 1)[-1] if '.' in filename: files.setdefault(filename, []).append(name) files = {name: sorted(set(owners)) for name, owners in files.items()} catalogue = {'version': 1, 'snapshot': 'texlive-2019-final', 'packages': packages, 'files': files} output = Path(destination) output.mkdir(parents=True, exist_ok=True) data = json.dumps(catalogue, separators=(',', ':'), ensure_ascii=False).encode('utf-8') (output / 'catalogue.json.gz').write_bytes(gzip.compress(data, mtime=0)) (output / 'repository.json').write_text(json.dumps({'version': 1, 'snapshot': catalogue['snapshot'], 'packages': packages}, separators=(',', ':')), encoding='utf-8') print(f'{len(packages)} paquets et {len(files)} noms de fichiers indexés.') if __name__ == '__main__': if len(sys.argv) != 3: raise SystemExit('Usage : python catalogue_paquets.py texlive.tlpdb.xz dossier_destination') build(sys.argv[1], sys.argv[2])