skrawcz commented on code in PR #741:
URL: https://github.com/apache/burr/pull/741#discussion_r3108345094
##########
scripts/verify_apache_artifacts.py:
##########
@@ -87,354 +237,445 @@ def _verify_artifact_signature(artifact_path: str,
signature_path: str) -> bool:
capture_output=True,
check=False,
)
- if result.returncode == 0:
- print(" ✓ GPG signature is valid")
- return True
- else:
- print(" ✗ GPG signature verification failed")
- if result.stderr:
- print(f" Error: {result.stderr.decode()}")
- return False
- except subprocess.CalledProcessError:
- print(" ✗ Error running GPG")
+ except OSError as exc:
+ print(f" ✗ Error running GPG: {exc}")
+ summary.fail(check_name, f"gpg unavailable: {exc}")
return False
+ if result.returncode == 0:
+ print(" ✓ GPG signature is valid")
+ summary.pass_(check_name)
+ return True
+
+ print(" ✗ GPG signature verification failed")
+ if result.stderr:
+ print(f" Error: {result.stderr.decode()}")
+ summary.fail(check_name, result.stderr.decode().strip() or "verification
failed")
+ return False
+
-def _verify_artifact_checksum(artifact_path: str, checksum_path: str) -> bool:
- """Verify SHA512 checksum of artifact."""
+def _verify_artifact_checksum(
+ artifact_path: str,
+ checksum_path: str,
+ summary: VerificationSummary,
+) -> bool:
+ check_name = f"SHA512 checksum: {os.path.basename(artifact_path)}"
print(f" Verifying SHA512 checksum: {os.path.basename(checksum_path)}")
if not os.path.exists(checksum_path):
print(" ✗ Checksum file not found")
+ summary.fail(check_name, "missing .sha512")
return False
- # Read expected checksum
- with open(checksum_path, "r", encoding="utf-8") as f:
- expected_checksum = f.read().strip().split()[0]
-
- # Calculate actual checksum
- sha512_hash = hashlib.sha512()
- with open(artifact_path, "rb") as f:
- while chunk := f.read(65536):
- sha512_hash.update(chunk)
-
- actual_checksum = sha512_hash.hexdigest()
+ with open(checksum_path, "r", encoding="utf-8") as handle:
+ expected_checksum = handle.read().strip().split()[0]
+ actual_checksum = _sha512_for_file(artifact_path)
if actual_checksum == expected_checksum:
print(" ✓ SHA512 checksum is valid")
+ summary.pass_(check_name)
return True
- else:
- print(" ✗ SHA512 checksum mismatch!")
- print(f" Expected: {expected_checksum}")
- print(f" Actual: {actual_checksum}")
- return False
+
+ print(" ✗ SHA512 checksum mismatch!")
+ print(f" Expected: {expected_checksum}")
+ print(f" Actual: {actual_checksum}")
+ summary.fail(check_name, "checksum mismatch")
+ return False
-def _verify_tar_gz_readable(artifact_path: str) -> bool:
- """Verify tar.gz archive can be read and contains files."""
+def _verify_tar_gz_readable(artifact_path: str, summary: VerificationSummary)
-> bool:
+ check_name = f"Readable tar.gz: {os.path.basename(artifact_path)}"
print(f" Checking archive readability: {os.path.basename(artifact_path)}")
try:
with tarfile.open(artifact_path, "r:gz") as tar:
members = tar.getmembers()
-
- if len(members) == 0:
+ if not members:
print(" ✗ Archive is empty (no files)")
+ summary.fail(check_name, "archive is empty")
return False
-
print(f" ✓ Archive is readable and contains {len(members)}
files")
+ summary.pass_(check_name, f"{len(members)} members")
return True
- except tarfile.TarError as e:
- print(f" ✗ Archive is corrupted or unreadable: {e}")
+ except tarfile.TarError as exc:
+ print(f" ✗ Archive is corrupted or unreadable: {exc}")
+ summary.fail(check_name, str(exc))
return False
- except Exception as e:
- print(f" ✗ Error reading archive: {e}")
+ except Exception as exc: # pragma: no cover - defensive
+ print(f" ✗ Error reading archive: {exc}")
+ summary.fail(check_name, str(exc))
return False
-def _verify_wheel_readable(wheel_path: str) -> bool:
- """Verify wheel can be read and contains expected structure."""
+def _verify_wheel_readable(wheel_path: str, summary: VerificationSummary) ->
bool:
+ check_name = f"Readable wheel: {os.path.basename(wheel_path)}"
print(f" Checking wheel readability: {os.path.basename(wheel_path)}")
try:
- with zipfile.ZipFile(wheel_path, "r") as whl:
- file_list = whl.namelist()
-
- if len(file_list) == 0:
+ with zipfile.ZipFile(wheel_path, "r") as wheel:
+ file_list = wheel.namelist()
+ if not file_list:
print(" ✗ Wheel is empty (no files)")
+ summary.fail(check_name, "wheel is empty")
return False
- # Check for metadata
- metadata_files = [f for f in file_list if "METADATA" in f or
"WHEEL" in f]
+ metadata_files = [name for name in file_list if "METADATA" in name
or "WHEEL" in name]
if not metadata_files:
print(" ✗ Wheel missing required metadata files")
+ summary.fail(check_name, "missing METADATA/WHEEL")
return False
print(f" ✓ Wheel is readable and contains {len(file_list)}
files")
+ summary.pass_(check_name, f"{len(file_list)} members")
return True
- except zipfile.BadZipFile:
+ except zipfile.BadZipFile as exc:
print(" ✗ Wheel is corrupted or not a valid ZIP file")
+ summary.fail(check_name, str(exc))
return False
- except Exception as e:
- print(f" ✗ Error reading wheel: {e}")
+ except Exception as exc: # pragma: no cover - defensive
+ print(f" ✗ Error reading wheel: {exc}")
+ summary.fail(check_name, str(exc))
return False
-def _verify_artifact_exists(artifact_path: str, min_size: int = 1000) -> bool:
- """Verify artifact exists and has reasonable size."""
- if not os.path.exists(artifact_path):
- print(f" ✗ Artifact not found: {os.path.basename(artifact_path)}")
- return False
+def _verify_required_text_files(
+ artifact_name: str,
+ file_bytes: dict[str, bytes],
+ required_files: tuple[str, ...],
+ expected_files: dict[str, bytes],
+ summary: VerificationSummary,
+) -> bool:
+ all_valid = True
+ for required_name in required_files:
+ check_name = f"{artifact_name} contains {required_name}"
+ matches = _find_files_by_basename(file_bytes, required_name)
+ if not matches:
+ print(f" ✗ Missing {required_name}")
+ summary.fail(check_name, "missing")
+ all_valid = False
+ continue
- file_size = os.path.getsize(artifact_path)
- if file_size < min_size:
- print(
- f" ✗ Artifact is suspiciously small ({file_size} bytes):
{os.path.basename(artifact_path)}"
- )
+ matched_path = matches[0]
+ if file_bytes[matched_path] != expected_files[required_name]:
+ print(f" ✗ {required_name} content mismatch ({matched_path})")
+ summary.fail(check_name, f"content mismatch at {matched_path}")
+ all_valid = False
+ continue
+
+ print(f" ✓ {required_name} present and matches repository copy
({matched_path})")
+ summary.pass_(check_name, matched_path)
+ return all_valid
+
+
+def verify_artifact_contents(artifacts_dir: str, summary: VerificationSummary
| None = None) -> bool:
+ _print_section("Verifying Artifact Metadata Files")
+
+ if summary is None:
+ summary = VerificationSummary()
+
+ if not os.path.exists(artifacts_dir):
+ _fail(f"Artifacts directory not found: {artifacts_dir}")
+
+ artifacts = _artifact_files(artifacts_dir)
+ if not artifacts:
+ print(f"⚠️ No artifacts found in {artifacts_dir}")
+ summary.fail("Artifact metadata verification", "no artifacts found")
return False
- print(f" ✓ Artifact exists: {os.path.basename(artifact_path)}
({file_size:,} bytes)")
- return True
+ expected_files = _read_expected_text_files()
+ all_valid = True
+ for artifact_name in artifacts:
+ artifact_path = os.path.join(artifacts_dir, artifact_name)
+ print(f"Inspecting metadata files: {artifact_name}")
+ print("-" * 80)
-def verify_signatures(artifacts_dir: str) -> bool:
- """Verify all signatures and checksums in artifacts directory."""
+ if artifact_name.endswith(".tar.gz"):
+ file_bytes = _tar_file_bytes(artifact_path)
+ if not _verify_required_text_files(
+ artifact_name,
+ file_bytes,
+ REQUIRED_TEXT_FILES,
+ expected_files,
+ summary,
+ ):
+ all_valid = False
+ elif artifact_name.endswith(".whl"):
+ file_bytes = _wheel_file_bytes(artifact_path)
+ if not _verify_required_text_files(
+ artifact_name,
+ file_bytes,
+ REQUIRED_TEXT_FILES[1:] + WHEEL_LICENSE_FILES,
Review Comment:
Nit: REQUIRED_TEXT_FILES[1:] silently skips LICENSE for wheels. A named
constant (e.g., WHEEL_REQUIRED_FILES = ("NOTICE", "DISCLAIMER") +
WHEEL_LICENSE_FILES) would make the intent clearer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]