Files
serena/scripts/add_spdx_headers.py
T
Dominik Jain 6707cd9b7e Introduce component-based licensing: Serena GPL-3.0-or-later, SolidLSP MIT
The repository is licensed per component. SolidLSP (src/solidlsp,
test/solidlsp, test/resources) remains MIT-licensed and independently
reusable; the Serena application (src/serena, src/interprompt, scripts,
test/serena, docs) is licensed under GPL-3.0-or-later starting with the v2
licensing transition. The change is not retroactive: all releases and
commits up to v1.7.0 / 74c38a65 (tag mit-final) remain available under MIT.

Since MIT is GPL-compatible, a distribution combining both (such as the
serena-agent package) is as a whole subject to GPL-3.0-or-later, while the
SolidLSP files themselves stay MIT and can be extracted and used separately
under MIT terms. The distribution metadata therefore declares
GPL-3.0-or-later, with both license texts shipped alongside it.

Serena originally began under the GPL (v2) and was switched to MIT in
May 2025 following community requests. We consider that change a mistake;
the substantial changes in v2 make this the appropriate time to revert it.
We want the best version of Serena to remain free.

Changes:
* LICENSE is now the licensing overview; canonical license texts live in
  LICENSES/ (MIT.txt is the previous LICENSE verbatim, GPL-3.0-or-later.txt
  is the unmodified FSF text)
* pyproject.toml declares the PEP 639 license expression
  "GPL-3.0-or-later" and bundles LICENSE and LICENSES/* as license files;
  the deprecated MIT classifier is dropped and flake.nix declares gpl3Plus;
  README has per-component license badges and a License section
* SPDX-License-Identifier headers in all Python sources under src/ and
  scripts/, added by the new idempotent scripts/add_spdx_headers.py, which
  gen_prompt_factory.py also uses to keep the header on the generated
  module; existing third-party notices are preserved
* CLA.md: Contributor License Agreement (contributor retains copyright;
  grants a perpetual, irrevocable license including relicensing under any
  terms, incl. proprietary/commercial; patent grant; authority
  representations), to be enforced repository-wide via cla-assistant.io
* CONTRIBUTING.md, PR template and a new docs page explain the licensing
  boundary and the CLA workflow
2026-09-14 21:33:18 +02:00

96 lines
2.9 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
"""
Adds SPDX-License-Identifier headers to Python source files according to the component
licensing structure documented in LICENSE.
Idempotent: files that already contain an SPDX identifier are left untouched.
Existing copyright notices and module docstrings are never modified; the identifier is
inserted after any shebang/encoding lines and after a leading module docstring (if present).
"""
import ast
import sys
from dataclasses import dataclass
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SPDX_PREFIX = "# SPDX-License-Identifier:"
@dataclass(frozen=True)
class LicensedTree:
"""A directory tree whose Python files fall under a single license."""
relative_path: str
spdx_id: str
TREES = [
LicensedTree("src/solidlsp", "MIT"),
LicensedTree("src/serena", "GPL-3.0-or-later"),
LicensedTree("src/interprompt", "GPL-3.0-or-later"),
LicensedTree("scripts", "GPL-3.0-or-later"),
]
def _header_end_line(source: str) -> int:
"""
Determines the 0-based line index at which the SPDX line is to be inserted, i.e. after
shebang/encoding lines and after a leading module docstring.
"""
lines = source.splitlines(keepends=True)
idx = 0
while idx < len(lines) and (lines[idx].startswith("#!") or (lines[idx].startswith("#") and "coding" in lines[idx])):
idx += 1
try:
module = ast.parse(source)
except SyntaxError:
return idx
if (
module.body
and isinstance(module.body[0], ast.Expr)
and isinstance(module.body[0].value, ast.Constant)
and isinstance(module.body[0].value.value, str)
):
return max(idx, module.body[0].end_lineno)
return idx
def add_header(path: Path, spdx_id: str) -> bool:
"""Adds the SPDX header to the given file; returns whether the file was modified."""
with path.open(encoding="utf-8", newline="") as f:
source = f.read()
if SPDX_PREFIX in source:
return False
# keep empty files (e.g. package markers) untouched
if not source.strip():
return False
# insert header, keeping the file's newline convention
newline = "\r\n" if "\r\n" in source else "\n"
lines = source.splitlines(keepends=True)
idx = _header_end_line(source)
header = f"{SPDX_PREFIX} {spdx_id}{newline}"
if idx < len(lines) and lines[idx].strip() != "":
header += newline
lines.insert(idx, header)
path.write_text("".join(lines), encoding="utf-8", newline="")
return True
def main() -> None:
modified = 0
for tree in TREES:
for path in sorted((ROOT / tree.relative_path).rglob("*.py")):
if "__pycache__" in path.parts:
continue
if add_header(path, tree.spdx_id):
modified += 1
print(f"{tree.spdx_id:18} {path.relative_to(ROOT)}")
print(f"{modified} file(s) modified", file=sys.stderr)
if __name__ == "__main__":
main()