Coverage for rust2rpm/licensing.py: 75%
47 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-27 15:21 +0100
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-27 15:21 +0100
1import os
2import csv
3import functools
4from typing import Optional
6from rust2rpm import log
8SPDX_TO_FEDORA_CSV = os.path.dirname(__file__) + "/spdx_to_fedora.csv"
11def translate_slashes(license: str) -> str:
12 "Replace all slashes with OR, emit warning"
13 split = [part.strip() for part in license.split("/")]
14 if len(split) > 1: 14 ↛ 15line 14 didn't jump to line 15 because the condition on line 14 was never true
15 log.info('Upstream uses deprecated "/" syntax. Replacing with "OR"')
16 return " OR ".join(split)
19@functools.lru_cache()
20def spdx_to_fedora_map() -> dict[str, str]:
21 with open(SPDX_TO_FEDORA_CSV, newline="") as f:
22 reader = csv.DictReader(f)
23 return {
24 line["SPDX License Identifier"]: line["Fedora Short Name"]
25 for line in reader
26 if line["SPDX License Identifier"]
27 }
30def dump_sdpx_to_fedora_map(file):
31 for k, v in spdx_to_fedora_map().items():
32 print(f"{k} → {v}", file=file)
35def translate_license_fedora(license: str) -> tuple[str, Optional[str]]:
36 comments = []
37 final = []
38 for tag in license.split():
39 # We accept all variant cases, but output lowercase which is what Fedora LicensingGuidelines specify
40 if tag.upper() == "OR":
41 final.append("or")
42 elif tag.upper() == "AND": 42 ↛ 43line 42 didn't jump to line 43 because the condition on line 42 was never true
43 final.append("and")
44 else:
45 if tag.endswith("+"): 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true
46 key = tag[:-1] + "-or-later"
47 fulltag = f"{tag} ({key})"
48 else:
49 key = fulltag = tag
51 mapped = spdx_to_fedora_map().get(key, None)
52 if mapped is None: 52 ↛ 53line 52 didn't jump to line 53 because the condition on line 52 was never true
53 comments += [f"# FIXME: Upstream uses unknown SPDX tag {fulltag}!"]
54 final.append(tag)
55 elif mapped == "": 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true
56 comments += [
57 f"# FIXME: Upstream SPDX tag {fulltag} not listed in Fedora's good licenses list.",
58 "# FIXME: This package might not be allowed in Fedora!",
59 ]
60 final.append(tag)
61 else:
62 final.append(mapped)
63 if mapped != tag:
64 log.info(f"Upstream license tag {fulltag!r} translated to {mapped!r}.")
65 return (" ".join(final), "\n".join(comments) or None)
68def translate_license(target: str, license: str) -> tuple[str, Optional[str]]:
69 license = translate_slashes(license)
70 if target in {"mageia"}:
71 return translate_license_fedora(license)
72 return license, None