|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import email |
| 6 | +import zipfile |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from packaging.requirements import Requirement |
| 10 | + |
| 11 | + |
| 12 | +EXPECTED_STANDALONE_DEPENDENCIES = { |
| 13 | + "opentelemetry-api", |
| 14 | + "opentelemetry-propagator-aws-xray", |
| 15 | + "opentelemetry-sdk", |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def check_otel_wheel_dependencies(wheel: Path) -> None: |
| 20 | + """Validate that OpenTelemetry dependencies are layer-provided by default.""" |
| 21 | + requirements = _read_requirements(wheel) |
| 22 | + base_otel_dependencies = { |
| 23 | + requirement.name |
| 24 | + for requirement in requirements |
| 25 | + if requirement.name.startswith("opentelemetry-") |
| 26 | + and (requirement.marker is None or requirement.marker.evaluate({"extra": ""})) |
| 27 | + } |
| 28 | + if base_otel_dependencies: |
| 29 | + names = ", ".join(sorted(base_otel_dependencies)) |
| 30 | + raise ValueError( |
| 31 | + f"{wheel.name} installs OpenTelemetry dependencies by default: {names}" |
| 32 | + ) |
| 33 | + |
| 34 | + standalone_dependencies = { |
| 35 | + requirement.name |
| 36 | + for requirement in requirements |
| 37 | + if requirement.name.startswith("opentelemetry-") |
| 38 | + and requirement.marker is not None |
| 39 | + and requirement.marker.evaluate({"extra": "standalone"}) |
| 40 | + } |
| 41 | + if standalone_dependencies != EXPECTED_STANDALONE_DEPENDENCIES: |
| 42 | + expected = ", ".join(sorted(EXPECTED_STANDALONE_DEPENDENCIES)) |
| 43 | + actual = ", ".join(sorted(standalone_dependencies)) or "none" |
| 44 | + raise ValueError( |
| 45 | + f"{wheel.name} standalone OpenTelemetry dependencies must be " |
| 46 | + f"{expected}; found {actual}" |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def _read_requirements(wheel: Path) -> list[Requirement]: |
| 51 | + if not wheel.is_file(): |
| 52 | + raise FileNotFoundError(wheel) |
| 53 | + |
| 54 | + with zipfile.ZipFile(wheel) as archive: |
| 55 | + metadata_files = [ |
| 56 | + name for name in archive.namelist() if name.endswith(".dist-info/METADATA") |
| 57 | + ] |
| 58 | + if len(metadata_files) != 1: |
| 59 | + raise ValueError( |
| 60 | + f"{wheel.name} must contain exactly one dist-info/METADATA file" |
| 61 | + ) |
| 62 | + metadata = email.message_from_bytes(archive.read(metadata_files[0])) |
| 63 | + |
| 64 | + return [ |
| 65 | + Requirement(value) for value in metadata.get_all("Requires-Dist", failobj=[]) |
| 66 | + ] |
| 67 | + |
| 68 | + |
| 69 | +def main(argv: list[str] | None = None) -> int: |
| 70 | + parser = argparse.ArgumentParser( |
| 71 | + description="Validate the OTel plugin wheel dependency contract." |
| 72 | + ) |
| 73 | + parser.add_argument("wheel", type=Path) |
| 74 | + args = parser.parse_args(argv) |
| 75 | + |
| 76 | + check_otel_wheel_dependencies(args.wheel) |
| 77 | + print(args.wheel) |
| 78 | + return 0 |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + raise SystemExit(main()) |
0 commit comments