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.
10 from pathlib
import Path
12 import xml
.etree
.ElementTree
as ET
18 print(f
"Usage: {sys.argv[0]} DIRECTORY")
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
)))