licenses.py

clew/licenses.py

Each file's declared licence: its `SPDX-License-Identifier` header, read into `file_licenses` (S5).

ingest_file_licenses

int clew.licenses.ingest_file_licenses(Path db_path, Path repo_root, IndexCache|None cache=None)

Record every indexed file's declared licence.

clew/licenses.py:86

Show on the map →

Called by _build_stages, run_real_stages

Tested by

1 teststest_the_layer_records_each_file_and_exists_when_empty

Calls license_of, within_repo

Source · 34 lines
def ingest_file_licenses(db_path: Path, repo_root: Path, cache: IndexCache | None = None) -> int:
    """MEMOIZED ON CONTENT like `file_docs`, so an unchanged file is not re-read. THE TABLE IS
    ALWAYS CREATED: empty means "no file declares one", absent would mean "this layer never ran".

    @brief Build the licence layer.
    @return Row count.
    """
    from .filedocs import _indexed_files

    conn = sqlite3.connect(str(db_path))
    try:
        conn.executescript(_DDL)
        conn.execute("DELETE FROM file_licenses")
        resolved: dict[str, str] = {}
        rows = []
        for rel in _indexed_files(conn):
            if (
                within_repo(repo_root / rel, repo_root, setting=f"licence of {rel}", memo=resolved)
                is None
            ):
                continue
            found = license_of(cache, repo_root, rel)
            if found:
                rows.append((rel, found[0], found[1]))
        conn.executemany(
            "INSERT INTO file_licenses (file_path, license, line) VALUES (?, ?, ?)", rows
        )
        conn.commit()
    finally:
        conn.close()
    logger.info("file_licenses: %d file(s) declare an SPDX licence", len(rows))
    return len(rows)

Activity · 17 steps
lines 93–93from .filedocs import _indexed_fileslines 95–95conn = sqlite3.connect(str(db_path))lines 96–96trylines 97–97conn.executescript(_DDL)lines 98–98conn.execute("DELETE FROM file_licenses")lines 99–99resolved: dict[str, str] = {}lines 100–100rows = []lines 101–101◇ for rel in _indexed_files(conn)lines 102–104◇ if (lines 106–106continuelines 107–107found = license_of(cache, repo_root, rel)lines 108–108◇ if foundlines 109–109rows.append((rel, found[0], found[1]))lines 110–112conn.executemany( "INSERT INTO file_licenses (file_path, license, line) VALUES (?, ?, ?)", rows )lines 113–113conn.commit()lines 116–116logger.info("file_licenses: %d file(s) declare an SPDX licence", len(rows))lines 117–117return len(rows)

Requirements

REQ-DDB-INDEX-008

refresh_file_license

None clew.licenses.refresh_file_license(sqlite3.Connection conn, Path repo_root, str rel, IndexCache cache)

Rewrite one edited file's licence row (the fast tier).

clew/licenses.py:149

Show on the map →

Called by refresh_text_layers

Calls license_of

Source · 12 lines
def refresh_file_license(
    conn: sqlite3.Connection, repo_root: Path, rel: str, cache: IndexCache
) -> None:
    """@brief The same row `ingest_file_licenses` writes, for one file. @return None."""
    if conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'file_licenses'").fetchone() is None:
        return
    conn.execute("DELETE FROM file_licenses WHERE file_path = ?", (rel,))
    found = license_of(cache, repo_root, rel)
    if found:
        conn.execute(
            "INSERT INTO file_licenses (file_path, license, line) VALUES (?, ?, ?)", (rel, *found)
        )

1 more lines, not shown

Activity · 6 steps
lines 153–153◇ if conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'file_licenses'").fetchone() is Nonelines 154–154returnlines 155–155conn.execute("DELETE FROM file_licenses WHERE file_path = ?", (rel,))lines 156–156found = license_of(cache, repo_root, rel)lines 157–157◇ if foundlines 158–160conn.execute( "INSERT INTO file_licenses (file_path, license, line) VALUES (?, ?, ?)", (rel, *found) )

Requirements

REQ-DDB-INDEX-006

spdx_of

tuple[str, int] | None clew.licenses.spdx_of(str text)

The SPDX expression a file's header declares, and its line.

clew/licenses.py:50

Source · 14 lines
def spdx_of(text: str) -> tuple[str, int] | None:
    """Any comment style — `//`, `#`, `/* */`, `--`, `<!-- -->`, `;` — because the tag is matched
    where it stands, not after a comment prefix this module would have to know per language.

    @brief Read the SPDX identifier.
    @return Expression and line, or None.
    """
    for number, line in enumerate(text.splitlines()[:_HEAD_LINES], start=1):
        match = _SPDX.search(line)
        if match and match.group(1).strip():
            return " ".join(match.group(1).split()), number
    return None

Activity · 5 steps
lines 57–57◇ for number, line in enumerate(text.splitlines()[:_HEAD_LINES], start=1)lines 58–58match = _SPDX.search(line)lines 59–59◇ if match and match.group(1).strip()lines 60–60return " ".join(match.group(1).split()), numberlines 61–61return None

Requirements

REQ-DDB-INDEX-008