|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +""" |
| 19 | +Rewrite java-sdk/gradle/verification-metadata.xml from an empty component list. |
| 20 | +
|
| 21 | +Gradle only ever appends to that file, so a version bump leaves the superseded |
| 22 | +entries behind and they stay trusted for good. Starting from an empty list drops |
| 23 | +them, which is what turns a stale checksum into a visible diff. |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import pathlib |
| 29 | +import subprocess |
| 30 | +import sys |
| 31 | +import time |
| 32 | +from collections.abc import Callable |
| 33 | + |
| 34 | +REPO_ROOT = pathlib.Path(__file__).resolve().parents[3] |
| 35 | +JAVA_SDK = REPO_ROOT / "java-sdk" |
| 36 | +METADATA = JAVA_SDK / "gradle" / "verification-metadata.xml" |
| 37 | + |
| 38 | +GRADLE_TASKS = [ |
| 39 | + "build", |
| 40 | + ":sdk:dokkaGeneratePublicationHtml", |
| 41 | + ":sdk:dokkaGeneratePublicationJavadoc", |
| 42 | + "sourceTarball", |
| 43 | + "checksumSourceTarball", |
| 44 | + "publishToMavenLocal", |
| 45 | +] |
| 46 | + |
| 47 | +MAX_ATTEMPTS = 3 |
| 48 | +RETRY_DELAY_SECONDS = 30 |
| 49 | + |
| 50 | +LICENSE_HEADER = """<!-- |
| 51 | + Licensed to the Apache Software Foundation (ASF) under one |
| 52 | + or more contributor license agreements. See the NOTICE file |
| 53 | + distributed with this work for additional information |
| 54 | + regarding copyright ownership. The ASF licenses this file |
| 55 | + to you under the Apache License, Version 2.0 (the |
| 56 | + "License"); you may not use this file except in compliance |
| 57 | + with the License. You may obtain a copy of the License at |
| 58 | +
|
| 59 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 60 | +
|
| 61 | + Unless required by applicable law or agreed to in writing, |
| 62 | + software distributed under the License is distributed on an |
| 63 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 64 | + KIND, either express or implied. See the License for the |
| 65 | + specific language governing permissions and limitations |
| 66 | + under the License. |
| 67 | +-->""" |
| 68 | + |
| 69 | +ADVISORY = """ |
| 70 | +The trust list changed. Entries that disappeared are checksums nothing resolves |
| 71 | +any more - usually versions superseded by a dependency bump. They stayed |
| 72 | +trusted, so a future direct or transitive dependency could have pulled that |
| 73 | +exact version back in with nobody reviewing its checksum. |
| 74 | +
|
| 75 | +Review every entry in the diff before staging it: generating the file records |
| 76 | +what the repositories served, it does not make those bytes trustworthy. |
| 77 | +""" |
| 78 | + |
| 79 | + |
| 80 | +class RegenerationFailedError(RuntimeError): |
| 81 | + """Gradle could not regenerate the metadata within the attempt budget.""" |
| 82 | + |
| 83 | + |
| 84 | +def build_empty_metadata(committed: str) -> str: |
| 85 | + """Return the committed metadata with its component list emptied.""" |
| 86 | + kept: list[str] = [] |
| 87 | + for line in committed.splitlines(): |
| 88 | + if "<components>" in line: |
| 89 | + kept += [" <components/>", "</verification-metadata>"] |
| 90 | + break |
| 91 | + kept.append(line) |
| 92 | + return "\n".join(kept) + "\n" |
| 93 | + |
| 94 | + |
| 95 | +def insert_license_header(metadata: str) -> str: |
| 96 | + """Put the ASF header back after the XML declaration, unless Gradle kept one.""" |
| 97 | + if "Licensed to the Apache Software Foundation" in metadata: |
| 98 | + return metadata |
| 99 | + declaration, *rest = metadata.splitlines() |
| 100 | + return "\n".join([declaration, LICENSE_HEADER, *rest]) + "\n" |
| 101 | + |
| 102 | + |
| 103 | +def run_gradle() -> bool: |
| 104 | + """Both properties are needed for the run to reach the end: signing has no key |
| 105 | + here, and sourceTarball has no default ref.""" |
| 106 | + result = subprocess.run( |
| 107 | + [ |
| 108 | + "./gradlew", |
| 109 | + "--no-daemon", |
| 110 | + "--write-verification-metadata", |
| 111 | + "sha256", |
| 112 | + "--refresh-dependencies", |
| 113 | + *GRADLE_TASKS, |
| 114 | + "-PskipSigning=true", |
| 115 | + "-PgitRef=HEAD", |
| 116 | + ], |
| 117 | + cwd=JAVA_SDK, |
| 118 | + check=False, |
| 119 | + ) |
| 120 | + return result.returncode == 0 |
| 121 | + |
| 122 | + |
| 123 | +def regenerate( |
| 124 | + metadata: pathlib.Path, |
| 125 | + gradle: Callable[[], bool], |
| 126 | + sleep: Callable[[float], None] = time.sleep, |
| 127 | +) -> None: |
| 128 | + """Rewrite the metadata in place, putting the committed file back if the run never succeeds.""" |
| 129 | + committed = metadata.read_text() |
| 130 | + restore = True |
| 131 | + try: |
| 132 | + for attempt in range(1, MAX_ATTEMPTS + 1): |
| 133 | + print(f"==> Regenerating verification metadata (attempt {attempt}/{MAX_ATTEMPTS})", flush=True) |
| 134 | + metadata.write_text(build_empty_metadata(committed)) |
| 135 | + if gradle(): |
| 136 | + metadata.write_text(insert_license_header(metadata.read_text())) |
| 137 | + restore = False |
| 138 | + return |
| 139 | + if attempt < MAX_ATTEMPTS: |
| 140 | + print(f"Regeneration failed, retrying in {RETRY_DELAY_SECONDS}s", file=sys.stderr, flush=True) |
| 141 | + sleep(RETRY_DELAY_SECONDS) |
| 142 | + raise RegenerationFailedError(f"Regeneration failed after {MAX_ATTEMPTS} attempts") |
| 143 | + finally: |
| 144 | + if restore: |
| 145 | + metadata.write_text(committed) |
| 146 | + |
| 147 | + |
| 148 | +def metadata_changed(metadata: pathlib.Path) -> bool: |
| 149 | + result = subprocess.run(["git", "diff", "--quiet", "--", str(metadata)], cwd=REPO_ROOT, check=False) |
| 150 | + return result.returncode != 0 |
| 151 | + |
| 152 | + |
| 153 | +def main() -> int: |
| 154 | + try: |
| 155 | + regenerate(METADATA, run_gradle) |
| 156 | + except RegenerationFailedError as error: |
| 157 | + print(f"ERROR: {error}", file=sys.stderr) |
| 158 | + return 1 |
| 159 | + if metadata_changed(METADATA): |
| 160 | + print(ADVISORY, file=sys.stderr) |
| 161 | + return 0 |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + sys.exit(main()) |
0 commit comments