"""Assemble the static site into a new directory after building BentoPDF.""" import argparse import hashlib from html import escape import json from pathlib import Path import shutil import subprocess import tarfile HERE = Path(__file__).resolve().parent LOCK = json.loads((HERE / 'sources.json').read_text()) def verified(cache, download): cache.mkdir(parents=True, exist_ok=True) for name, (url, expected) in LOCK.items(): path = cache / name if not path.exists() and download: temporary = path.with_suffix(path.suffix + '.part') subprocess.run(['curl', '--fail', '--location', '--silent', '--show-error', '--compressed', '--retry', '2', '--max-time', '300', '--output', temporary, url], check=True) with temporary.open('rb') as stream: if hashlib.file_digest(stream, 'sha256').hexdigest() != expected: raise ValueError(f'Checksum mismatch for {name}') temporary.replace(path) with path.open('rb') as stream: if hashlib.file_digest(stream, 'sha256').hexdigest() != expected: raise ValueError(f'Checksum mismatch for {name}') def unpack(cache, package, output, folder, singles=()): with tarfile.open(cache / package) as archive: for member in archive: if not member.isfile() or not member.name.startswith('package/'): continue name = member.name.removeprefix('package/') if '..' in Path(name).parts or Path(name).is_absolute(): raise ValueError('Unsafe archive path') target = dict(singles).get(name) if folder and name.startswith(folder): target = name.removeprefix(folder) if target: destination = output / target destination.parent.mkdir(parents=True, exist_ok=True) with destination.open('wb') as stream: shutil.copyfileobj(archive.extractfile(member), stream) def assemble(cache, upstream, output): output.mkdir(parents=True, exist_ok=False) shutil.copytree(HERE.parent / 'site', output, dirs_exist_ok=True) shutil.copytree(upstream / 'dist', output / 'tools') rt = output / 'runtime' rt.mkdir() shutil.copy2(upstream / 'node_modules/pdf-lib/dist/pdf-lib.min.js', rt / 'pdf-lib.js') shutil.copy2(upstream / 'node_modules/pdf-lib/LICENSE.md', rt / 'pdf-lib-LICENSE.md') unpack(cache, 'pymupdf-wasm-0.11.16.tgz', rt / 'pymupdf/assets', 'assets/') unpack(cache, 'pymupdf-wasm-0.11.16.tgz', rt / 'pymupdf/dist', 'dist/') unpack(cache, 'gs-wasm-0.1.1.tgz', rt / 'gs', 'assets/') unpack(cache, 'coherentpdf-2.5.5.tgz', rt / 'cpdf', 'dist/') unpack(cache, 'tesseract.js-7.0.0.tgz', rt / 'ocr', '', [('dist/worker.min.js','worker.min.js')]) with tarfile.open(cache / 'tesseract.js-core-7.0.0.tgz') as archive: core = [(p[8:],p[8:]) for p in archive.getnames() if p.startswith('package/tesseract-core') and p.endswith(('.wasm','.js'))] unpack(cache, 'tesseract.js-core-7.0.0.tgz', rt / 'ocr/core', '', core) for lang in ('eng','rus'): name = f'{lang}.traineddata.gz' unpack(cache, f'{lang}-1.0.0.tgz', rt / 'ocr/lang', '', [('4.0.0_best_int/'+name,name)]) (rt / 'ocr/fonts').mkdir() shutil.copy2(cache / 'NotoSans-Regular.ttf', rt / 'ocr/fonts') shutil.copy2(cache / 'NotoSans-LICENSE', rt / 'ocr/fonts') unpack(cache, 'pptxgenjs-4.0.1.tgz', rt, '', [('dist/pptxgen.bundle.js','pptx.js'),('LICENSE','pptx-LICENSE')]) unpack(cache, 'bergamot.tgz', rt / 'translate/worker', 'worker/') unpack(cache, 'bergamot.tgz', rt / 'translate', '', [('translator.js','translator.js')]) shutil.copy2(HERE / 'translation-models.json', rt / 'translate/models.json') (rt / 'translate/models').mkdir() for parts in json.loads((HERE / 'translation-models.json').read_text()).values(): for item in parts.values(): name = Path(item['name']).name shutil.copy2(cache / name, rt / 'translate/models' / name) sources = output / 'sources' sources.mkdir() shutil.copy2(upstream / 'LICENSE', sources / 'BentoPDF-LICENSE.txt') shutil.copy2(cache / 'bentopdf-2.8.12.tar.gz', sources) for file in HERE.iterdir(): if file.is_file(): shutil.copy2(file, sources) links = ''.join(f'
  • {escape(name)}
  • ' for name, (url, _) in LOCK.items()) (sources / 'index.html').write_text( 'Компоненты PDF Easy' '
    PDF Easy

    Исходный код и лицензии

    ' '

    Инструменты основаны на BentoPDF 2.8.12. ' 'Исходный код BentoPDF · ' 'AGPL-3.0 · Параметры сборки · ' 'Сборка сайта · Контрольные суммы.

    ' '

    Компоненты и языковые модели

    ') if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('cache', type=Path) parser.add_argument('--download', action='store_true') parser.add_argument('--upstream', type=Path) parser.add_argument('--output', type=Path) args = parser.parse_args() if bool(args.upstream) != bool(args.output): parser.error('Supply --upstream and --output together') verified(args.cache, args.download) if args.output: assemble(args.cache, args.upstream, args.output)