"""Writes requirements-licenses.json: each package pinned in requirements.lock with its license from PyPI. The frontend build reads it for the third-party notices in Settings > About. """ import json import re import urllib.error import urllib.request from pathlib import Path # Packages that publish no license on PyPI, taken from their license files and repositories. UNDECLARED_LICENSES = { 'aiocache': 'BSD-3-Clause', 'azure-core': 'MIT', 'azure-identity': 'MIT', 'azure-search-documents': 'MIT', 'colbert-ai': 'MIT', 'docx2txt': 'MIT', 'google-crc32c': 'Apache-2.0', 'pgvector': 'MIT', 'playwright': 'Apache-2.0', 'sentencepiece': 'Apache-2.0', } here = Path(__file__).parent pins = re.findall(r'^([\w.-]+)(?:\[[^\]]*\])?==([^\s;\\]+)', (here / 'requirements.lock').read_text(), re.M) # The Dockerfile installs these from the PyTorch index instead of requirements.lock (torch<=2.9.1). pins += [('torch', '2.9.1'), ('torchvision', '0.24.1'), ('torchaudio', '2.9.1')] def notice(pin): name, version = pin # PyPI occasionally drops a TLS handshake; retry as often as urllib3 does by default. for attempt in range(3): try: with urllib.request.urlopen(f'https://pypi.org/pypi/{name}/{version}/json') as response: info = json.load(response)['info'] break except urllib.error.URLError: if attempt == 2: raise license = ( info.get('license_expression') or next((c.split(' :: ')[-1] for c in info.get('classifiers') or [] if c.startswith('License :: ')), None) or (info.get('license') or '').split('\n')[0] or UNDECLARED_LICENSES.get(name.lower(), '') ) return {'name': info['name'], 'version': version, 'license': license, 'url': f'https://pypi.org/project/{name}/'} notices = [notice(pin) for pin in pins] missing = [n['name'] for n in notices if not n['license']] if missing: raise SystemExit(f'No license found for {", ".join(missing)}; add them to UNDECLARED_LICENSES') (here / 'requirements-licenses.json').write_text(json.dumps(notices, indent=1) + '\n')