1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#!/usr/bin/env python3
# SPDX-License-Identifier: MPL-2.0
# Copyright ijl (2026)
import asyncio
import datetime
import re
import subprocess
from collections import defaultdict
from pathlib import Path
REPOSITORY = Path(__file__).parent.parent
LICENSE_APACHE = "(Apache-2.0 OR MIT)"
LICENSE_MPL2 = "MPL-2.0"
SPACES = re.compile(r"[ ]{2,}")
TO_INCLUDE = {
".github/**/*.yaml",
"bench/*.py",
"bench/run",
"integration/*",
"pysrc/orjson/*",
"script/*",
"src/**/*.rs",
"test/**/*.py",
}
TO_EXCLUDE = {
"integration/http",
"script/cargo",
"script/debug",
"script/develop",
"script/install-fedora",
"script/lint",
"script/profile",
"script/pybench",
"script/pytest",
"script/valgrind",
"src/ffi/atomiculong.rs",
}
def aggregate_files() -> list[Path]:
files = []
files.append(REPOSITORY / Path("build.rs"))
for pattern in TO_INCLUDE:
files.extend(REPOSITORY.glob(pattern))
files = {
each
for each in files
if not str(each).endswith(("py.typed", "__pycache__", ".txt"))
}
for filename in TO_EXCLUDE:
files.remove(REPOSITORY / Path(filename))
return sorted(list(files))
SKIP_FRAGMENTS = (
"# Copyright",
"# SPDX",
"#!/usr",
"// Copyright",
"// SPDX",
)
def get_contributor_and_date(line: str) -> tuple[str, datetime.date] | None:
if not line or "Not Committed Yet" in line:
return None
end = line.index(")")
diff = line[end + 2 :]
diff = SPACES.sub(r" ", diff)
# skip blank and ' };' etc
if len(diff) <= 3:
return None
# skip headers and imports
for fragment in SKIP_FRAGMENTS:
if diff.startswith(fragment):
return None
line = line[line.index("(") + 1 : end]
line = SPACES.sub(r" ", line)
segments = line.split(" ")[0:-2]
contributor = " ".join(segments[:-1])
date = datetime.date.fromtimestamp(int(segments[-1])).year
return (contributor, date)
def process_blame(filename: Path, blame: str) -> list[str, list[str]] | None:
file_license = "(Apache-2.0 OR MIT)"
contributors = defaultdict(list)
document = blame.split("\n")
for line in document:
ret = get_contributor_and_date(line)
if ret:
contributors[ret[0]].append(ret[1])
overall_earliest = 9999
overall_latest = 0
file_credit = []
for contributor, dates in contributors.items():
earliest = min(dates)
latest = max(dates)
overall_latest = max((latest, overall_latest))
overall_earliest = min((latest, overall_earliest))
num_lines = len(dates)
if earliest == latest:
file_credit.append((num_lines, f"{contributor} ({earliest})"))
else:
file_credit.append((num_lines, f"{contributor} ({earliest}-{latest})"))
if (len(contributors) == 1 and "ijl" in contributors) or overall_earliest == 2026:
file_license = LICENSE_MPL2
file_credit.sort(reverse=True)
return [file_license, file_credit]
async def handle_file(filename: str):
blame = await asyncio.create_subprocess_shell(
f"git blame -C -C -C -M --date=raw {filename}",
shell=True,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
header = process_blame(filename, (await blame.stdout.read()).decode("utf-8"))
if header is None:
print(f"{filename.relative_to(REPOSITORY)} skipping")
return
if not header[1] and str(filename).endswith("__init__.py"):
return
if str(filename).endswith(".rs"):
prefix = "//"
else:
prefix = "#"
spdx = f"{prefix} SPDX-License-Identifier: {header[0]}"
credit = f"{prefix} Copyright {', '.join(each[1] for each in header[1])}"
contents = filename.read_bytes().decode("utf-8").split("\n")
if contents[0].startswith("#!"):
start_idx = 1
else:
start_idx = 0
if contents[start_idx].startswith(f"{prefix} SPDX-License-Identifier"):
contents[start_idx] = spdx
else:
contents.insert(start_idx, spdx)
if contents[start_idx + 1].startswith(f"{prefix} Copyright"):
contents[start_idx + 1] = credit
else:
contents.insert(start_idx + 1, credit)
# separate by blank line
first_line = start_idx + 2
while contents[first_line].startswith(prefix):
first_line += 1
if len(contents[first_line]) > 0:
contents.insert(first_line, "")
print(f"{filename.relative_to(REPOSITORY)} {spdx}")
filename.write_bytes("\n".join(contents).encode("utf-8"))
async def main():
async with asyncio.TaskGroup() as tg:
for filename in aggregate_files():
tg.create_task(handle_file(filename))
if __name__ == "__main__":
asyncio.run(main())
|