anvil-editor: init at 0.4
[NixPkgs.git] / pkgs / build-support / dotnet / make-nuget-source / extract-licenses-from-nupkgs.py
blob22564b0bb2bc853efe439a612a85e9e2f4d3c181
1 #!/usr/bin/env python3
2 """
3 Opens each .nupkg file in a directory, and extracts the SPDX license identifiers
4 from them if they exist. The SPDX license identifier is stored in the
5 '<license type="expression">...</license>' tag in the .nuspec file.
6 All found license identifiers will be printed to stdout.
7 """
9 from glob import glob
10 from pathlib import Path
11 import sys
12 import xml.etree.ElementTree as ET
13 import zipfile
15 all_licenses = set()
17 if len(sys.argv) < 2:
18 print(f"Usage: {sys.argv[0]} DIRECTORY")
19 sys.exit(1)
21 nupkg_dir = Path(sys.argv[1])
22 for nupkg_name in glob("*.nupkg", root_dir=nupkg_dir):
23 with zipfile.ZipFile(nupkg_dir / nupkg_name) as nupkg:
24 for nuspec_name in [name for name in nupkg.namelist() if name.endswith(".nuspec")]:
25 with nupkg.open(nuspec_name) as nuspec_stream:
26 nuspec = ET.parse(nuspec_stream)
27 licenses = nuspec.findall(".//{*}license[@type='expression']")
28 all_licenses.update([license.text for license in licenses])
30 print("\n".join(sorted(all_licenses)))