Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions scripts/devstream/license_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ def match(name):
("good, not evil", "JSON License"),
]

# Restrictions people tack onto a standard license text ("MIT, but you may not use this commercially").
# Kept to wording that restricts; plenty of OSI texts talk about commercial use without limiting it.
_MAY_NOT = r"\b(?:(?:may|must|shall|can|will)(?: not|not| never)|can't|cannot|won't|mustn't|shan't)"
_USE = r"(?:use|used|usage|purposes?|projects?|products?)"
# "No commercial use restrictions" lifts a limit rather than adding one.
_NOT_A_LIMIT = r"(?![^.;]{0,30}?\b(?:restrictions?|limits?|limitations?|fees?)\b)"
RESTRICTION_RE = re.compile("|".join([
rf"{_MAY_NOT} (?:be )?(?:used?|sold|sell|resold|resell|redistributed?|distributed?)\b[^.;]{{0,40}}\bcommercial",
rf"{_MAY_NOT} (?:be )?(?:sold|sell|resold|resell)\b",
r"\bcommercial (?:use|usage|purposes?|distribution|exploitation)\b[^.;]{0,40}\b(?:is|are) "
r"(?:not (?:permitted|allowed)|prohibited|forbidden)",
rf"\b(?:no|without) commercial (?:use|usage|purposes?)\b{_NOT_A_LIMIT}",
rf"\b(?:personal|private|non-?commercial|educational|academic) {_USE} only\b",
rf"\bonly (?:for|be used for|be used in) (?:personal|private|non-?commercial|educational|academic) {_USE}",
# ...unless all it requires is the attribution or notice the license already asks for.
rf"\bcommercial (?:use|usage|purposes?)\b[^.;]{{0,40}}\brequires?\b{_NOT_A_LIMIT}"
r"(?![^.;]{0,40}?\b(?:attribution|notice|credit)\b)",
]))

# (SPDX ID, phrases that must all appear, phrases that must not). The first phrase is the
# license's opening, the rest are standard text from deeper in the same license, so a
# look-alike that borrows only the opening doesn't match. First match wins, so more
Expand Down Expand Up @@ -217,8 +236,30 @@ def _spdx_scan(text: str, allowed: frozenset = frozenset()) -> tuple[str | None,
return found, bad


# A limit on using someone's trademark (MIT's Kerberos notice, bundled in MySQL's LICENSE) isn't a license term.
TRADEMARK_RE = re.compile(r"\btrade ?marks?\b|\btrade names?\b|\bservice marks?\b|\blogos?\b")


def _restricted(text: str) -> bool:
t = _norm(text).replace("\u2019", "'")
for m in RESTRICTION_RE.finditer(t):
start = max(t.rfind(".", 0, m.start()), t.rfind(";", 0, m.start())) + 1
end = min((i for i in (t.find(".", m.end()), t.find(";", m.end())) if i != -1), default=len(t))
if not TRADEMARK_RE.search(t[start:end]):
return True
return False


def _detect_text(text: str, allowed: frozenset = frozenset()) -> tuple[str | None, str | None]:
"""(osi_id, non_osi_name) for one license file."""
found, bad = _detect_known(text, allowed)
if found and _restricted(text):
# A fixed name, never the file's own words: this reason is posted publicly.
return None, f"{found} with an added restriction"
return found, bad


def _detect_known(text: str, allowed: frozenset = frozenset()) -> tuple[str | None, str | None]:
bad = _non_osi(text)
if bad:
return None, bad
Expand Down Expand Up @@ -260,6 +301,8 @@ def _readme_license(readme: str, allowed: frozenset = frozenset()) -> tuple[str
m = NAME_RE.search(low)
if m and not NEGATION_RE.search(low) and not NC_ND.intersection(re.findall(r"[a-z0-9]+", low)) \
and _id_ok(NAMES[m.group(1)], allowed):
if _restricted(section):
return None, f"{NAMES[m.group(1)]} with an added restriction"
return NAMES[m.group(1)], None
return None, None

Expand Down
45 changes: 45 additions & 0 deletions scripts/tests/test_license_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ def test_spdx_and_needs_all(self):
g = lic_gh({"LICENSE": "SPDX-License-Identifier: MIT AND Proprietary"})
self.assertIn("isn't an open-source license", run(g).failures[0])

def test_added_restriction_after_a_license_text_fails(self):
msg = MSG_NON_OSI.format("MIT with an added restriction")
canonical = (FIXTURES / "MIT.txt").read_text()
for extra in ("You may not use this commercially.", "This software may not be sold.",
"Commercial use is prohibited.", "For personal use only.",
"Commercial use requires a paid license from the author.",
"You can\u2019t sell this software.", "It can only be used for non-commercial projects."):
with self.subTest(extra):
self.assertEqual(run(lic_gh({"LICENSE": canonical + "\n\n" + extra})).failures, [msg])
g = lic_gh({"LICENSE": "SPDX-License-Identifier: MIT\nNo commercial use."})
self.assertEqual(run(g).failures, [msg])
g = lic_gh({}, readme=README + "\n## License\n\nMIT\n\nFor personal use only.\n")
self.assertEqual(run(g).failures, [msg])

def test_readme_prose_that_lifts_a_limit_is_not_a_restriction(self):
for prose in ("There are no commercial use restrictions.", "No commercial use limits apply.",
"Without commercial use fees of any kind.",
"Commercial use requires nothing more than the notice.",
"Commercial use requires attribution, like any other use.",
"Commercial use requires no fee or other limitation."):
with self.subTest(prose):
g = lic_gh({}, readme=README + f"\n## License\n\nMIT\n\n{prose}\n")
self.assertEqual(run(g).failures, [])
for prose in ("No commercial use.", "Commercial use requires a paid license."):
with self.subTest(prose):
g = lic_gh({}, readme=README + f"\n## License\n\nMIT\n\n{prose}\n")
self.assertEqual(run(g).failures, [MSG_NON_OSI.format("MIT with an added restriction")])

def test_trademark_notice_is_not_a_restriction(self):
# From the MIT Kerberos notice bundled in mysql/mysql-server's LICENSE.
notice = ("Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira, and Zephyr are "
"trademarks of the Massachusetts Institute of Technology (MIT). No commercial use of these "
"trademarks may be made without prior written permission of MIT.")
canonical = (FIXTURES / "MIT.txt").read_text()
self.passes(lic_gh({"LICENSE": canonical + "\n\n" + notice}))
g = lic_gh({"LICENSE": canonical + "\n\n" + notice + " No commercial use."})
self.assertEqual(run(g).failures, [MSG_NON_OSI.format("MIT with an added restriction")])

def test_canonical_texts_carry_no_restriction(self):
from scripts.devstream.license_detect import _restricted
for f in sorted(FIXTURES.glob("*.txt")):
with self.subTest(f.name):
# CC BY-NC is the one text that really does restrict commercial use.
self.assertEqual(_restricted(f.read_text()), f.name == "CC-BY-NC-4.0.txt")

def test_sspl_rejected_by_name(self):
r = run(lic_gh({"LICENSE": SSPL + MIT}))
self.assertEqual(
Expand Down
Loading