| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""Copyright-holder drift detection (explain mode, --group-by license). |
| 18 |
|
| 19 |
These exercise the copyright_processor side of the drift signal: the declared |
| 20 |
holders snapshot taken in _process_groups and (later) the drift recording in |
| 21 |
remove_misplaced_files. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import annotations |
| 25 |
|
| 26 |
import tempfile |
| 27 |
import unittest |
| 28 |
from pathlib import Path |
| 29 |
|
| 30 |
from decopy import cmdoptions |
| 31 |
from decopy.dep5doc import dep5 |
| 32 |
from decopy.dep5doc.delta import ( |
| 33 |
COPYRIGHT_DRIFT, |
| 34 |
DRIFT_ADDED, |
| 35 |
DRIFT_REMOVED, |
| 36 |
RELICENSED, |
| 37 |
STALE_PARAGRAPH, |
| 38 |
DeltaReport, |
| 39 |
) |
| 40 |
from decopy.fstree import nodes, tree |
| 41 |
|
| 42 |
_APACHE_TAIL = """\ |
| 43 |
// |
| 44 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 45 |
// you may not use this file except in compliance with the License. |
| 46 |
// You may obtain a copy of the License at |
| 47 |
// |
| 48 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 49 |
""" |
| 50 |
|
| 51 |
# A file detected as Apache-2.0 carrying the holder "Foo Bar". |
| 52 |
_UTIL_C = "// Copyright 2020 Foo Bar\n" + _APACHE_TAIL |
| 53 |
|
| 54 |
# Same, but carrying two holders. |
| 55 |
_UTIL_C_TWO = ( |
| 56 |
"// Copyright 2020 Foo Bar\n// Copyright 2010 Someone Else\n" + _APACHE_TAIL |
| 57 |
) |
| 58 |
|
| 59 |
# The paragraph matches util.c by license (Apache-2.0) so the file is kept, but |
| 60 |
# declares a different holder ("Someone Else") than the file carries. |
| 61 |
_DRIFT_COPYRIGHT = """\ |
| 62 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 63 |
Source: TODO |
| 64 |
|
| 65 |
Files: u*.c |
| 66 |
Copyright: 2010, Someone Else |
| 67 |
License: Apache-2.0 |
| 68 |
""" |
| 69 |
|
| 70 |
|
| 71 |
def _build_processed(root: Path, *, group_by: str = "license"): |
| 72 |
"""Run the pipeline stages up to remove_misplaced_files, returning copyright_. |
| 73 |
|
| 74 |
Mirrors run_pipeline's stage order: copyright_.process matches files and |
| 75 |
builds groups (declared snapshot taken here), filetree.process parses files, |
| 76 |
then remove_misplaced_files merges kept files' holders. |
| 77 |
""" |
| 78 |
(root / "util.c").write_text(_UTIL_C, encoding="utf-8") |
| 79 |
debian = root / "debian" |
| 80 |
debian.mkdir() |
| 81 |
(debian / "copyright").write_text(_DRIFT_COPYRIGHT, encoding="utf-8") |
| 82 |
|
| 83 |
options = cmdoptions.process_options( |
| 84 |
[ |
| 85 |
"--no-progress", |
| 86 |
"--no-expand-archives", |
| 87 |
"--group-by", |
| 88 |
group_by, |
| 89 |
"--root", |
| 90 |
str(root), |
| 91 |
] |
| 92 |
) |
| 93 |
filetree = tree.RootInfo.build(options) |
| 94 |
copyright_ = dep5.read_copyright(options) |
| 95 |
copyright_.process(filetree) |
| 96 |
filetree.process(options) |
| 97 |
copyright_.remove_misplaced_files(options) |
| 98 |
return copyright_, options |
| 99 |
|
| 100 |
|
| 101 |
def _util_group(copyright_): |
| 102 |
"""The group that ends up holding util.c after remove_misplaced_files.""" |
| 103 |
for group in copyright_.groups or (): |
| 104 |
if "util.c" in {f.fullname for f in group.files}: |
| 105 |
return group |
| 106 |
msg = "util.c was not kept in any group" |
| 107 |
raise AssertionError(msg) |
| 108 |
|
| 109 |
|
| 110 |
def _drift_report( |
| 111 |
root: Path, util_c: str, copyright_text: str, *, group_by: str = "license" |
| 112 |
) -> DeltaReport: |
| 113 |
"""Build a tree and run remove_misplaced_files with an injected report.""" |
| 114 |
(root / "util.c").write_text(util_c, encoding="utf-8") |
| 115 |
debian = root / "debian" |
| 116 |
debian.mkdir() |
| 117 |
(debian / "copyright").write_text(copyright_text, encoding="utf-8") |
| 118 |
|
| 119 |
options = cmdoptions.process_options( |
| 120 |
[ |
| 121 |
"--no-progress", |
| 122 |
"--no-expand-archives", |
| 123 |
"--group-by", |
| 124 |
group_by, |
| 125 |
"--root", |
| 126 |
str(root), |
| 127 |
] |
| 128 |
) |
| 129 |
filetree = tree.RootInfo.build(options) |
| 130 |
copyright_ = dep5.read_copyright(options) |
| 131 |
report = DeltaReport() |
| 132 |
copyright_.process(filetree) |
| 133 |
filetree.process(options) |
| 134 |
copyright_.remove_misplaced_files(options, None, report) |
| 135 |
return report |
| 136 |
|
| 137 |
|
| 138 |
def _drift_entries(report: DeltaReport): |
| 139 |
return report.by_kind().get(COPYRIGHT_DRIFT, []) |
| 140 |
|
| 141 |
|
| 142 |
_DECLARES_SOMEONE = """\ |
| 143 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 144 |
Source: TODO |
| 145 |
|
| 146 |
Files: u*.c |
| 147 |
Copyright: 2010, Someone Else |
| 148 |
License: Apache-2.0 |
| 149 |
""" |
| 150 |
|
| 151 |
_DECLARES_BOTH = """\ |
| 152 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 153 |
Source: TODO |
| 154 |
|
| 155 |
Files: u*.c |
| 156 |
Copyright: 2020, Foo Bar |
| 157 |
2010, Someone Else |
| 158 |
License: Apache-2.0 |
| 159 |
""" |
| 160 |
|
| 161 |
_DECLARES_FOO = """\ |
| 162 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 163 |
Source: TODO |
| 164 |
|
| 165 |
Files: u*.c |
| 166 |
Copyright: 2020, Foo Bar |
| 167 |
License: Apache-2.0 |
| 168 |
""" |
| 169 |
|
| 170 |
|
| 171 |
class IsPackagingTest(unittest.TestCase): |
| 172 |
"""FileInfo.is_packaging identifies the debian/ packaging directory.""" |
| 173 |
|
| 174 |
def setUp(self): |
| 175 |
self.root = nodes.DirInfo(parent=None, name="") |
| 176 |
self.debian = nodes.DirInfo(parent=self.root, name="debian") |
| 177 |
self.src = nodes.DirInfo(parent=self.root, name="src") |
| 178 |
|
| 179 |
def test_debian_dir_is_packaging(self): |
| 180 |
self.assertTrue(self.debian.is_packaging()) |
| 181 |
|
| 182 |
def test_file_under_debian_is_packaging(self): |
| 183 |
rules = nodes.FileInfo(parent=self.debian, name="rules") |
| 184 |
self.assertTrue(rules.is_packaging()) |
| 185 |
|
| 186 |
def test_upstream_file_is_not_packaging(self): |
| 187 |
util = nodes.FileInfo(parent=self.src, name="util.c") |
| 188 |
self.assertFalse(util.is_packaging()) |
| 189 |
|
| 190 |
def test_debian_prefix_without_slash_is_not_packaging(self): |
| 191 |
# "debianish.c" starts with "debian" but is not under debian/. |
| 192 |
debianish = nodes.FileInfo(parent=self.root, name="debianish.c") |
| 193 |
self.assertFalse(debianish.is_packaging()) |
| 194 |
|
| 195 |
|
| 196 |
class RecordCopyrightDriftTest(unittest.TestCase): |
| 197 |
"""remove_misplaced_files records drift for kept files under license grouping.""" |
| 198 |
|
| 199 |
def test_added_holder_is_drift_added(self): |
| 200 |
# File carries {Foo Bar, Someone Else}; paragraph declares only Someone Else. |
| 201 |
with tempfile.TemporaryDirectory() as tmp: |
| 202 |
report = _drift_report(Path(tmp), _UTIL_C_TWO, _DECLARES_SOMEONE) |
| 203 |
entries = _drift_entries(report) |
| 204 |
self.assertEqual(len(entries), 1) |
| 205 |
self.assertEqual(entries[0].sub_kind, DRIFT_ADDED) |
| 206 |
self.assertIn("Foo Bar", " ".join(entries[0].now_copyright or ())) |
| 207 |
|
| 208 |
def test_orphaned_holder_is_drift_removed(self): |
| 209 |
# File carries only Foo Bar; paragraph declares Foo Bar and Someone Else. |
| 210 |
with tempfile.TemporaryDirectory() as tmp: |
| 211 |
report = _drift_report(Path(tmp), _UTIL_C, _DECLARES_BOTH) |
| 212 |
entries = _drift_entries(report) |
| 213 |
self.assertEqual(len(entries), 1) |
| 214 |
self.assertEqual(entries[0].sub_kind, DRIFT_REMOVED) |
| 215 |
self.assertIn("Someone Else", " ".join(entries[0].was_copyright or ())) |
| 216 |
|
| 217 |
def test_matching_holders_record_nothing(self): |
| 218 |
with tempfile.TemporaryDirectory() as tmp: |
| 219 |
report = _drift_report(Path(tmp), _UTIL_C, _DECLARES_FOO) |
| 220 |
self.assertEqual(_drift_entries(report), []) |
| 221 |
|
| 222 |
def test_copyright_grouping_records_no_drift(self): |
| 223 |
# The same added-holder fixture under --group-by copyright: holder changes |
| 224 |
# surface via the relicensed path, so drift is gated off (D5). |
| 225 |
with tempfile.TemporaryDirectory() as tmp: |
| 226 |
report = _drift_report( |
| 227 |
Path(tmp), _UTIL_C_TWO, _DECLARES_SOMEONE, group_by="copyright" |
| 228 |
) |
| 229 |
self.assertEqual(_drift_entries(report), []) |
| 230 |
|
| 231 |
|
| 232 |
def _multi_report( |
| 233 |
root: Path, |
| 234 |
files: dict[str, str], |
| 235 |
copyright_text: str, |
| 236 |
*, |
| 237 |
group_by: str = "license", |
| 238 |
extra_args: list[str] | None = None, |
| 239 |
) -> DeltaReport: |
| 240 |
"""Write several files plus debian/copyright, run remove_misplaced_files.""" |
| 241 |
for relpath, content in files.items(): |
| 242 |
path = root / relpath |
| 243 |
path.parent.mkdir(parents=True, exist_ok=True) |
| 244 |
path.write_text(content, encoding="utf-8") |
| 245 |
debian = root / "debian" |
| 246 |
debian.mkdir(exist_ok=True) |
| 247 |
(debian / "copyright").write_text(copyright_text, encoding="utf-8") |
| 248 |
|
| 249 |
options = cmdoptions.process_options( |
| 250 |
[ |
| 251 |
"--no-progress", |
| 252 |
"--no-expand-archives", |
| 253 |
"--group-by", |
| 254 |
group_by, |
| 255 |
"--root", |
| 256 |
str(root), |
| 257 |
*(extra_args or []), |
| 258 |
] |
| 259 |
) |
| 260 |
filetree = tree.RootInfo.build(options) |
| 261 |
copyright_ = dep5.read_copyright(options) |
| 262 |
report = DeltaReport() |
| 263 |
copyright_.process(filetree) |
| 264 |
filetree.process(options) |
| 265 |
copyright_.remove_misplaced_files(options, None, report) |
| 266 |
return report |
| 267 |
|
| 268 |
|
| 269 |
# src/* and debian/* both declared GPL-2+, but the files below detect Apache-2.0, |
| 270 |
# so each would relicense were it not for the packaging suppression. |
| 271 |
_SRC_AND_DEBIAN_GPL = """\ |
| 272 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 273 |
Source: TODO |
| 274 |
|
| 275 |
Files: src/* |
| 276 |
License: GPL-2+ |
| 277 |
|
| 278 |
Files: debian/* |
| 279 |
License: GPL-2+ |
| 280 |
""" |
| 281 |
|
| 282 |
|
| 283 |
class RelicenseDebianSuppressionTest(unittest.TestCase): |
| 284 |
"""A license mismatch under debian/ is not reported as relicensed.""" |
| 285 |
|
| 286 |
def test_upstream_relicensed_debian_suppressed(self): |
| 287 |
with tempfile.TemporaryDirectory() as tmp: |
| 288 |
report = _multi_report( |
| 289 |
Path(tmp), |
| 290 |
{"src/bar.c": _UTIL_C, "debian/foo.c": _UTIL_C}, |
| 291 |
_SRC_AND_DEBIAN_GPL, |
| 292 |
) |
| 293 |
paths = {e.path for e in report.by_kind().get(RELICENSED, [])} |
| 294 |
self.assertIn("src/bar.c", paths) |
| 295 |
self.assertNotIn("debian/foo.c", paths) |
| 296 |
|
| 297 |
|
| 298 |
# A packaging file carrying a contributor holder the maintainer did not declare. |
| 299 |
_PACKAGER_C = "// Copyright 2024 Packager Person\n" + _APACHE_TAIL |
| 300 |
|
| 301 |
_DEBIAN_ONLY = """\ |
| 302 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 303 |
Source: TODO |
| 304 |
|
| 305 |
Files: debian/* |
| 306 |
Copyright: 2026, The Maintainer |
| 307 |
License: Apache-2.0 |
| 308 |
""" |
| 309 |
|
| 310 |
_CATCHALL_SOMEONE = """\ |
| 311 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 312 |
Source: TODO |
| 313 |
|
| 314 |
Files: * |
| 315 |
Copyright: 2010, Someone Else |
| 316 |
License: Apache-2.0 |
| 317 |
""" |
| 318 |
|
| 319 |
|
| 320 |
class DriftDebianSuppressionTest(unittest.TestCase): |
| 321 |
"""Packaging files do not drive copyright drift; debian/copyright is truth.""" |
| 322 |
|
| 323 |
def test_pure_debian_paragraph_reports_no_drift(self): |
| 324 |
# debian/foo.c carries "Foo Bar"; the maintainer declared "The Maintainer". |
| 325 |
# Were it not packaging, this would be a both-direction drift. |
| 326 |
with tempfile.TemporaryDirectory() as tmp: |
| 327 |
report = _multi_report(Path(tmp), {"debian/foo.c": _UTIL_C}, _DEBIAN_ONLY) |
| 328 |
self.assertEqual(report.by_kind().get(COPYRIGHT_DRIFT, []), []) |
| 329 |
|
| 330 |
def test_mixed_group_drifts_on_upstream_holders_only(self): |
| 331 |
# One Files: * group holds an upstream file (adds "Foo Bar") and a |
| 332 |
# packaging file (carries "Packager Person"); only the upstream holder |
| 333 |
# may surface. --no-split-debian keeps both files in the one group. |
| 334 |
with tempfile.TemporaryDirectory() as tmp: |
| 335 |
report = _multi_report( |
| 336 |
Path(tmp), |
| 337 |
{"src/up.c": _UTIL_C_TWO, "debian/foo.c": _PACKAGER_C}, |
| 338 |
_CATCHALL_SOMEONE, |
| 339 |
extra_args=["--no-split-debian"], |
| 340 |
) |
| 341 |
entries = report.by_kind().get(COPYRIGHT_DRIFT, []) |
| 342 |
self.assertEqual(len(entries), 1) |
| 343 |
self.assertEqual(entries[0].sub_kind, DRIFT_ADDED) |
| 344 |
now = " ".join(entries[0].now_copyright or ()) |
| 345 |
self.assertIn("Foo Bar", now) |
| 346 |
self.assertNotIn("Packager Person", now) |
| 347 |
|
| 348 |
|
| 349 |
def _process_report( |
| 350 |
root: Path, files: dict[str, str], copyright_text: str |
| 351 |
) -> DeltaReport: |
| 352 |
"""Build a tree and run copyright_.process with an injected report. |
| 353 |
|
| 354 |
Stale-paragraph/pattern deltas are recorded during process(), before |
| 355 |
filetree.process, so this captures them without the later stages. |
| 356 |
""" |
| 357 |
for relpath, content in files.items(): |
| 358 |
path = root / relpath |
| 359 |
path.parent.mkdir(parents=True, exist_ok=True) |
| 360 |
path.write_text(content, encoding="utf-8") |
| 361 |
debian = root / "debian" |
| 362 |
debian.mkdir(exist_ok=True) |
| 363 |
(debian / "copyright").write_text(copyright_text, encoding="utf-8") |
| 364 |
|
| 365 |
options = cmdoptions.process_options( |
| 366 |
["--no-progress", "--no-expand-archives", "--root", str(root)] |
| 367 |
) |
| 368 |
filetree = tree.RootInfo.build(options) |
| 369 |
copyright_ = dep5.read_copyright(options) |
| 370 |
report = DeltaReport() |
| 371 |
copyright_.process(filetree, report) |
| 372 |
return report |
| 373 |
|
| 374 |
|
| 375 |
_STALE_PARA_COPYRIGHT = """\ |
| 376 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 377 |
Source: TODO |
| 378 |
|
| 379 |
Files: u*.c |
| 380 |
License: Expat |
| 381 |
|
| 382 |
Files: legacy/* |
| 383 |
License: GPL-2+ |
| 384 |
""" |
| 385 |
|
| 386 |
|
| 387 |
class StaleParagraphLicenseTest(unittest.TestCase): |
| 388 |
"""A stale-paragraph entry carries the paragraph's declared license.""" |
| 389 |
|
| 390 |
def test_stale_paragraph_carries_license(self): |
| 391 |
# legacy/* matches no files -> recorded as a stale paragraph; util.c |
| 392 |
# keeps the u*.c paragraph alive so only legacy/* is stale. |
| 393 |
with tempfile.TemporaryDirectory() as tmp: |
| 394 |
report = _process_report( |
| 395 |
Path(tmp), {"util.c": _UTIL_C}, _STALE_PARA_COPYRIGHT |
| 396 |
) |
| 397 |
entries = report.by_kind().get(STALE_PARAGRAPH, []) |
| 398 |
self.assertEqual(len(entries), 1) |
| 399 |
self.assertEqual(entries[0].paragraph_license, "GPL-2+") |
| 400 |
|
| 401 |
|
| 402 |
class DeclaredCopyrightKeyTest(unittest.TestCase): |
| 403 |
"""_process_groups snapshots the paragraph's declared holders before merge.""" |
| 404 |
|
| 405 |
def test_declared_key_predates_kept_file_merge(self): |
| 406 |
with tempfile.TemporaryDirectory() as tmp: |
| 407 |
copyright_, _ = _build_processed(Path(tmp)) |
| 408 |
group = _util_group(copyright_) |
| 409 |
declared = group.declared_copyright_key |
| 410 |
merged = group.copyrights.key() |
| 411 |
|
| 412 |
# The snapshot reflects only the paragraph's Copyright field... |
| 413 |
self.assertIn("Someone Else", " ".join(declared)) |
| 414 |
self.assertNotIn("Foo Bar", " ".join(declared)) |
| 415 |
# ...while the live holders absorbed the kept file's "Foo Bar". |
| 416 |
self.assertIn("Foo Bar", " ".join(merged)) |
| 417 |
|
| 418 |
|
| 419 |
if __name__ == "__main__": |
| 420 |
unittest.main() |