#!/usr/bin/env python3
from pathlib import Path
from typing import Iterable
import argparse
import re
import sys
COPYRIGHT_LINE_RE = re.compile(r"^// Copyright 2\d\d\d Signal Messenger, LLC\n$")
SPDX_LINE = "// SPDX-License-Identifier: AGPL-3.0-only\n"
def has_shebang(line: str) -> bool:
return line.startswith("#!")
def has_valid_license_header(path: Path) -> bool:
copyright_len = 0
license_len = 0
with open(path, "rt", encoding="utf8") as file:
for line in file:
if has_shebang(line):
continue
if len(line.rstrip()) == 0:
continue
if COPYRIGHT_LINE_RE.match(line) is not None:
copyright_len += 1
continue
if line == SPDX_LINE:
license_len += 1
continue
if line.startswith("//"):
continue
break
return copyright_len == 1 and license_len == 1
def main() -> None:
parser = argparse.ArgumentParser(
description="Check license headers across the project"
)
parser.add_argument("path", nargs="*", help="A path to process")
ns = parser.parse_args()
all_good = True
for path in ns.path:
path = Path(path)
if not has_valid_license_header(path):
print(f"{path} has an invalid license header", file=sys.stderr)
all_good = False
if not all_good:
sys.exit(1)
if __name__ == "__main__":
main()