"""Writes native/LICENSE.deepfilternet-crates: the notices of every Rust crate linked into libdf (scripts/build-deepfilter.sh), for the Mac app to carry. Run after build-deepfilter.sh, which leaves the pinned source and toolchain behind, and again whenever its commit changes: python3 native/scripts/deepfilter-notices.py The crate set is what the static library links for the Mac, the only app that links it: deep_filter with its C API, followed over normal dependencies only. Build scripts and proc-macros run at compile time and are not in the binary, so they and whatever only they depend on are left out. Where a crate offers a choice of licences it is used under MIT, as DeepFilterNet itself is (see LICENSE.deepfilternet); otherwise under the most permissive it offers. Each crate's own licence file is reproduced, since that is where its copyright line is, with any NOTICE file it ships. """ from pathlib import Path import json import os import re import subprocess import sys native = Path(__file__).resolve().parent.parent src = native / 'build/deepfilter/src' out = native / 'LICENSE.deepfilternet-crates' toolchain = re.search(r'^toolchain=(\S+)', (native / 'scripts/build-deepfilter.sh').read_text(), re.M).group(1) target = 'aarch64-apple-darwin' if not (src / 'Cargo.toml').exists(): sys.exit('deepfilter-notices: run scripts/build-deepfilter.sh first') env = dict(os.environ, PATH=str(Path.home() / '.cargo/bin') + os.pathsep + os.environ['PATH']) def crates(target): """name version -> (source directory, licence expression). cargo tree and not cargo metadata: metadata resolves features across DeepFilterNet's whole workspace, and so names crates (hdf5 among them) that only the other members use; tree resolves them as `cargo build -p deep_filter` does.""" listing = subprocess.check_output( ['cargo', f'+{toolchain}', 'tree', '--locked', '-p', 'deep_filter', '--features', 'capi', '-e', 'normal,no-proc-macro', '--target', target, '--prefix', 'none', '--no-dedupe', '-f', '{p}|{l}'], cwd=src, env=env, text=True) registry = sorted((Path.home() / '.cargo/registry/src').glob('*')) found = {} for line in sorted(set(listing.splitlines())): package, licence = line.split('|', 1) m = re.match(r'(\S+) v(\S+)(?: \((.+)\))?', package) name, version, source = m.group(1), m.group(2), m.group(3) if source and not source.startswith(('registry', 'proxy')) and Path(source).is_dir(): root = Path(source) else: root = next((r / f'{name}-{version}' for r in registry if (r / f'{name}-{version}').is_dir()), None) if root is None: sys.exit(f'deepfilter-notices: no source for {name} {version}') found[f'{name} {version}'] = {'name': name, 'root': root, 'license': licence} return found MIT = """Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.""" # Most permissive first. A crate offering MIT is used under MIT. PREFERENCE = ['MIT', '0BSD', 'Unlicense', 'CC0-1.0', 'Zlib', 'BSD-2-Clause', 'BSD-3-Clause', 'Apache-2.0'] FILES = { 'MIT': ['LICENSE-MIT', 'LICENSE-MIT.md', 'LICENSE.MIT', 'LICENSE', 'LICENSE.md', 'COPYING', 'LICENSE.txt'], 'Apache-2.0': ['LICENSE-APACHE', 'LICENSE-APACHE.md', 'LICENSE.Apache-2.0', 'LICENSE', 'LICENSE.md', 'LICENSE.txt'], 'Unlicense': ['UNLICENSE', 'LICENSE-UNLICENSE'], 'Zlib': ['LICENSE-ZLIB', 'LICENSE-ZLIB.md', 'LICENSE'], 'BSD-2-Clause': ['LICENSE-BSD', 'LICENSE'], 'BSD-3-Clause': ['LICENSE', 'LICENSE.md', 'COPYING'], 'CC0-1.0': ['LICENSE', 'LICENSE-CC0'], '0BSD': ['LICENSE-0BSD', 'LICENSE-0BSD.md', 'LICENSE'], } def choose(expression): offered = set(re.findall(r'[A-Za-z0-9.\-]+', expression.replace('/', ' OR '))) - {'OR', 'AND', 'WITH'} required = re.findall(r'AND\s+([A-Za-z0-9.\-]+)', expression) chosen = next((l for l in PREFERENCE if l in offered), None) return chosen, required mac = crates(target) sections, missing = [], [] for key in sorted(mac, key=str.lower): package = mac[key] if package['name'] == 'deep_filter': continue # LICENSE.deepfilternet root = package['root'] expression = package.get('license') or '' chosen, required = choose(expression) texts = [] for licence in [chosen, *required]: for name in FILES.get(licence, []) + ['LICENSE']: path = root / name if path.is_file(): texts.append(path.read_text(errors='replace').strip()) break else: if licence and licence != 'MIT': missing.append(f'{key} ({licence})') for notice in sorted(root.glob('NOTICE*')): texts.append(notice.read_text(errors='replace').strip()) if not texts and chosen == 'MIT': authors = re.search(r'^authors\s*=\s*\[(.*?)\]', (root / 'Cargo.toml').read_text(), re.M | re.S) holders = ', '.join(re.findall(r'"([^"]+)"', authors.group(1))) if authors else f'the {package["name"]} authors' texts.append('(The crate is published under MIT without a licence file; the standard\n' 'MIT text follows, naming the authors its Cargo.toml gives.)\n\n' f'Copyright (c) {holders}\n\n' + MIT) elif not texts: texts.append(f'(The crate ships no licence file; it is published under {expression}.)') heading = f'{key} — used under {chosen or expression}' + (f' AND {" AND ".join(required)}' if required else '') sections.append(heading + '\n' + '-' * len(heading) + '\n\n' + '\n\n'.join(dict.fromkeys(texts))) intro = f'''Rust crates in DeepFilterNet's libDF, as linked into Calls for Mac (scripts/build-deepfilter.sh; generated by scripts/deepfilter-notices.py from the pinned DeepFilterNet commit with Rust {toolchain}). Where a crate is offered under a choice of licences it is used under the one named in its heading. DeepFilterNet's own notice is LICENSE.deepfilternet. ''' out.write_text(intro + '\n\n' + '\n\n\n'.join(sections) + '\n') print(f'Wrote {out} ({len(sections)} crates)') if missing: print('No licence file found for: ' + ', '.join(missing), file=sys.stderr)