| 1 | """실제 두 패키지의 고지 원문과 Cargo 항목 일치를 확인함.""" |
| 2 | |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | import subprocess |
| 8 | import tarfile |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[2] |
| 11 | |
| 12 | |
| 13 | def deb_notice(path): |
| 14 | matches = [] |
| 15 | with subprocess.Popen(["dpkg-deb", "--fsys-tarfile", str(path)], stdout=subprocess.PIPE) as process: |
| 16 | with tarfile.open(fileobj=process.stdout, mode="r|") as archive: |
| 17 | for member in archive: |
| 18 | if member.name.removeprefix("./") == "usr/share/doc/zipelo/THIRD-PARTY-LICENSES.txt": |
| 19 | if not member.isfile(): |
| 20 | raise ValueError("deb 고지가 일반 파일이 아님") |
| 21 | matches.append(archive.extractfile(member).read()) |
| 22 | if process.wait() != 0: |
| 23 | raise ValueError("deb 읽기 실패") |
| 24 | if len(matches) != 1: |
| 25 | raise ValueError("deb 고지 누락 또는 중복") |
| 26 | return matches[0] |
| 27 | |
| 28 | |
| 29 | def windows_notice(path, sevenzip): |
| 30 | listing = subprocess.check_output([sevenzip, "l", "-slt", "-ba", str(path)], text=True) |
| 31 | paths = [line.removeprefix("Path = ") for line in listing.splitlines() if line.startswith("Path = ")] |
| 32 | matches = [name for name in paths if name.replace("\\", "/").split("/")[-1] == "THIRD-PARTY-LICENSES.txt"] |
| 33 | if len(matches) != 1: |
| 34 | raise ValueError("NSIS 고지 누락 또는 중복") |
| 35 | return subprocess.check_output([sevenzip, "x", "-so", str(path), matches[0]]) |
| 36 | |
| 37 | |
| 38 | def main(): |
| 39 | parser = argparse.ArgumentParser(description=__doc__) |
| 40 | for name in ["windows", "ubuntu", "notice", "inventory", "output"]: |
| 41 | parser.add_argument("--" + name, type=Path, required=True) |
| 42 | parser.add_argument("--sevenzip", required=True) |
| 43 | args = parser.parse_args() |
| 44 | if not args.output.resolve().is_relative_to(ROOT / "tests"): |
| 45 | raise ValueError("검증 결과는 tests 안에 저장해야 함") |
| 46 | expected = args.notice.read_bytes() |
| 47 | inventory = json.loads(args.inventory.read_text()) |
| 48 | headings = {line.removeprefix("Crate: ") for line in expected.decode("utf-8").splitlines() |
| 49 | if line.startswith("Crate: ")} |
| 50 | expected_headings = {row["name"] + " " + row["version"] for row in inventory} |
| 51 | if headings != expected_headings or not all(row["files"] for row in inventory): |
| 52 | raise ValueError("고지 항목과 inventory 불일치") |
| 53 | if expected.count(b"\nCrate: ") != len(inventory): |
| 54 | raise ValueError("고지 항목 중복") |
| 55 | for label, actual in [("windows", windows_notice(args.windows, args.sevenzip)), |
| 56 | ("ubuntu", deb_notice(args.ubuntu))]: |
| 57 | if actual != expected: |
| 58 | raise ValueError(label + " 패키지 고지 원문 불일치") |
| 59 | report = { |
| 60 | "crates": len(inventory), |
| 61 | "missing": 0, |
| 62 | "notice_sha256": hashlib.sha256(expected).hexdigest(), |
| 63 | "notice_bytes": len(expected), |
| 64 | "standard_supplements": expected.decode("utf-8").count("upstream에 원문이 없어 표준 원문으로 보충함"), |
| 65 | "packages": {label: {"path": str(path), "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), |
| 66 | "bytes": path.stat().st_size} |
| 67 | for label, path in [("windows", args.windows), ("ubuntu", args.ubuntu)]}, |
| 68 | } |
| 69 | args.output.write_text(json.dumps(report, ensure_ascii=False, indent=4) + "\n") |
| 70 | print(json.dumps(report, ensure_ascii=False)) |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | main() |